0% found this document useful (0 votes)
11 views5 pages

Adbms Lab # 4 Views Task: Database Diagram

The document outlines the creation of a database for an ADBMS lab, including tables for departments, courses, instructors, sections, and teaching assignments. It provides SQL commands for creating these tables, inserting data, and defining various types of views such as simple, complex, updatable, and materialized views. Additionally, it includes examples of queries to retrieve data from these views.

Uploaded by

abdullahlahore19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Adbms Lab # 4 Views Task: Database Diagram

The document outlines the creation of a database for an ADBMS lab, including tables for departments, courses, instructors, sections, and teaching assignments. It provides SQL commands for creating these tables, inserting data, and defining various types of views such as simple, complex, updatable, and materialized views. Additionally, it includes examples of queries to retrieve data from these views.

Uploaded by

abdullahlahore19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ADBMS LAB # 4

Views Task

Database Diagram:

Quries:
CREATE DATABASE adbmsLAB4

CREATE TABLE department (


dept_name VARCHAR(20),
building VARCHAR(15),
budget DECIMAL(12,2),
PRIMARY KEY (dept_name)
);

CREATE TABLE course (


course_id VARCHAR(7),
title VARCHAR(50),
dept_name VARCHAR(20),
credits DECIMAL(2,0),
PRIMARY KEY (course_id),
FOREIGN KEY (dept_name) REFERENCES department(dept_name)
);

CREATE TABLE instructor (


ID VARCHAR(5),
name VARCHAR(20) NOT NULL,
dept_name VARCHAR(20),
salary DECIMAL(8,2),
PRIMARY KEY (ID),
FOREIGN KEY (dept_name) REFERENCES department(dept_name)
);
CREATE TABLE section (
course_id VARCHAR(7),
sec_id VARCHAR(8),
semester VARCHAR(6),
year DECIMAL(4,0),
building VARCHAR(15),
room_number VARCHAR(7),
time_slot_id VARCHAR(4),
PRIMARY KEY (course_id, sec_id, semester, year),
FOREIGN KEY (course_id) REFERENCES course(course_id)
);

CREATE TABLE teaches (


ID VARCHAR(5),
course_id VARCHAR(7),
sec_id VARCHAR(8),
semester VARCHAR(6),
year DECIMAL(4,0),
PRIMARY KEY (ID, course_id, sec_id, semester, year),
FOREIGN KEY (course_id, sec_id, semester, year)
REFERENCES section(course_id, sec_id, semester, year),
FOREIGN KEY (ID) REFERENCES instructor(ID)
);

INSERT INTO department (dept_name, building, budget)


VALUES
('Science', 'Block A', 1500000.00),
('Mathematics', 'Block B', 1200000.00),
('English', 'Block C', 1000000.00),
('Computer Science', 'Block D', 1800000.00),
('Islamic Studies', 'Block E', 800000.00),
('Urdu', 'Block F', 750000.00),
('Social Studies', 'Block G', 900000.00);

INSERT INTO course (course_id, title, dept_name, credits)


VALUES
('SCI101', 'Physics', 'Science', 4),
('SCI102', 'Chemistry', 'Science', 4),
('MTH101', 'Algebra', 'Mathematics', 3),
('ENG101', 'English Grammar', 'English', 3),
('CSC101', 'Introduction to IT', 'Computer Science', 4),
('ISL101', 'Islamic History', 'Islamic Studies', 2),
('URD101', 'Urdu Literature', 'Urdu', 3);

INSERT INTO instructor (ID, name, dept_name, salary)


VALUES
('I001', 'Ahmed Khan', 'Science', 60000.00),
('I002', 'Sara Malik', 'Mathematics', 55000.00),
('I003', 'Ali Raza', 'English', 50000.00),
('I004', 'Fatima Noor', 'Computer Science', 65000.00),
('I005', 'Zainab Javed', 'Islamic Studies', 48000.00),
('I006', 'Imran Hussain', 'Urdu', 47000.00),
('I007', 'Nadia Siddiqui', 'Social Studies', 49000.00);

INSERT INTO section (course_id, sec_id, semester, year, building, room_number,


time_slot_id)
VALUES
('SCI101', 'SEC1', 'Spring', 2024, 'Block A', 'A101', 'T1'),
('SCI102', 'SEC2', 'Fall', 2024, 'Block A', 'A102', 'T2'),
('MTH101', 'SEC1', 'Spring', 2024, 'Block B', 'B201', 'T3'),
('ENG101', 'SEC1', 'Fall', 2024, 'Block C', 'C301', 'T4'),
('CSC101', 'SEC1', 'Spring', 2024, 'Block D', 'D401', 'T5'),
('ISL101', 'SEC1', 'Fall', 2024, 'Block E', 'E501', 'T6'),
('URD101', 'SEC1', 'Spring', 2024, 'Block F', 'F601', 'T7');

INSERT INTO teaches (ID, course_id, sec_id, semester, year)


VALUES
('I001', 'SCI101', 'SEC1', 'Spring', 2024),
('I001', 'SCI102', 'SEC2', 'Fall', 2024),
('I002', 'MTH101', 'SEC1', 'Spring', 2024),
('I003', 'ENG101', 'SEC1', 'Fall', 2024),
('I004', 'CSC101', 'SEC1', 'Spring', 2024),
('I005', 'ISL101', 'SEC1', 'Fall', 2024),
('I006', 'URD101', 'SEC1', 'Spring', 2024);

Simple View:

CREATE VIEW physics_spring_2024_BlockA AS


SELECT s.course_id, s.room_number
FROM course c
JOIN section s ON c.course_id = s.course_id
WHERE c.title = 'Physics'
AND s.semester = 'Spring'
AND s.year = 2024
AND s.building = 'Block A';

SELECT * FROM physics_spring_2024_BlockA

Complex View:
CREATE VIEW instructor_course_details AS
SELECT
i.ID AS instructor_id,
i.name AS instructor_name,
d.dept_name AS department,
c.title AS course_title,
s.semester,
s.year,
s.building,
s.room_number
FROM instructor i
JOIN department d ON i.dept_name = d.dept_name
JOIN teaches t ON i.ID = t.ID
JOIN course c ON t.course_id = c.course_id
JOIN section s ON c.course_id = s.course_id AND t.sec_id = s.sec_id;

SELECT * FROM instructor_course_details


Updatable View:
CREATE VIEW cs_instructor_salaries AS
SELECT ID, name, salary
FROM instructor
WHERE dept_name = 'Computer Science';

SELECT * FROM cs_instructor_salaries

UPDATE cs_instructor_salaries
SET salary = salary + 5000
WHERE name = 'Fatima Noor';

SELECT * FROM cs_instructor_salaries

Materialized view:
CREATE VIEW Department_budget_summary1
WITH SCHEMABINDING
AS
SELECT
dept_name,
SUM(budget) AS total_budget,
COUNT_BIG(*) AS row_count
FROM dbo.department
GROUP BY dept_name;

CREATE UNIQUE CLUSTERED INDEX idx_department_budget

ON Department_budget_summary1 (dept_name);
SELECT * FROM Department_budget_summary1;

You might also like