0% found this document useful (0 votes)
5 views

Lab - 13

Uploaded by

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

Lab - 13

Uploaded by

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

Department of Computer Science

CS220: Database Systems

Class: BESE-14AB

Lab 13: Views in MySQL


Date: Dec 16, 2024
Time: 10:00-01:00 & 02:00-05:00
Lab Engineer: Ms. Ayesha Asif

Submitted By :
Name: Muhammad Arslan
Class: BESE-14B
CMS I’d: 465428

CS220: Database Systems Page 1


Lab 13: Views in MySQL
Lab Tasks
Given the following database schema:

Student (snum: integer, sname: char(30), major: char(25), level: char(2))


Faculty (fid: integer, fname: char(30), deptid: integer)
Class (cname: char(40), meets_at: char(20), room: char(10), fid: integer | fid REFS
Faculty.fid)
Enrolled (snum: integer, cname: char(40) | snum REFS student.snum, cname REFS
class.name)

1. Create a view named v1 which has the name of faculty members who do not teach any
course.

Query:
CREATE OR REPLACE VIEW v1 AS
SELECT f.fname
FROM faculty f
left join Class c on f.fid=c.fid
WHERE c.fid is null;

Output:

CS220: Database Systems Page 2


2. Create another view named v2 which has the names of students who are enrolled in a course
taught by faculty member “Ivana Teach”.

Query:

CREATE OR REPLACE VIEW v2 AS


SELECT s.sname
FROM Student s
JOIN Enrolled e ON s.snum = e.snum
JOIN Class c ON e.cname = c.cname
JOIN Faculty f ON c.fid = f.fid
WHERE f.fname = 'Ivana Teach';

Output:

3. Create a view stdVu that is based on the student relation.

Query:

CREATE OR REPLACE VIEW stdVu AS


SELECT *FROM Student;

Output:

CS220: Database Systems Page 3


4. Alter the definition of student table. Add a column course to the student’s relation.

Query:

ALTER table Student


ADD course varchar(45);
Output:

5. Notice the changes in views which are based on student relation. Comment what happens to
view data if the base table is modified.

The data in views will change automatically if the underlying base table data (rows)
changes, such as inserting, updating, or deleting. However, if the table structure (columns)
changes, the view will not update unless its definition is explicitly modified.

6. Alter view v2 based on the definition: It has the names of all juniors (Level = JR) who are
enrolled in a class taught by ‘Ivana Teach’.

Query:
CREATE OR REPLACE VIEW v2 AS
SELECT s.sname
FROM Student s
JOIN Enrolled e ON s.snum = e.snum
JOIN Class c ON e.cname = c.cname
JOIN Faculty f ON c.fid = f.fid
WHERE f.fname = 'Ivana Teach' and s.level='JR';

Output:

CS220: Database Systems Page 4


7. Create views based on the following queries also:
a. The names of students majoring in ‘Computer Science’.

Query:

CREATE VIEW cs_students AS


SELECT s.sname
FROM Student s
WHERE s.major = 'Computer Science';

Output:

b. The names of classes taught by ‘John Williams’ in dept # 68.

Query:
CREATE VIEW john_williams_classes AS
SELECT c.cname
FROM Class c
JOIN Faculty f ON c.fid = f.fid
WHERE f.fname = 'John Williams' AND f.dept_id = 68;

Output:

c. The distinct student ages in ‘Database Systems’ class in descending order.

CS220: Database Systems Page 5


Query:

CREATE VIEW distinct_ages_in_db_systems AS


SELECT DISTINCT s.age
FROM Student s
JOIN Enrolled e ON s.snum = e.snum
JOIN Class c ON e.cname = c.cname
WHERE c.cname = 'Database Systems'
ORDER BY s.age DESC;

Output:

d. The name of ‘Christopher Garcia’s teachers.

Query:

CREATE VIEW christopher_garcia_teachers AS


SELECT f.fname
FROM Faculty f
JOIN Class c ON f.fid = c.fid
JOIN Enrolled e ON c.cname = e.cname
JOIN Student s ON e.snum = s.snum
WHERE s.sname = 'Christopher Garcia';

Output:

8. Drop view v1, v2.

CS220: Database Systems Page 6


Query:
DROP VIEW v1, v2;

Output:

CS220: Database Systems Page 7

You might also like