Lab 11
Lab 11
Objectives
After completing this lab, you should be able to do the following:
Create a view
Alter/Update a view
Alter a table
Tools/Software Requirement
MySQL Community Server 5.6
MySQL Workbench 6.1
Description
Views (including updatable views) are stored queries that when invoked produce a result set. A
view acts as a virtual table.
Syntax
OR REPLACE is optional. If you do not specify this clause and the VIEW already exists, the
CREATE VIEW statement will return an error.
The CREATE VIEW statement creates a new view, or replaces an existing view if the OR
REPLACE clause is given. If the view does not exist, CREATE OR REPLACE VIEW is the
The ALTER VIEW statement does the same thing as CREATE OR REPLACE.
The full ALTER statement looks like this:
The CREATE VIEW statement requires the CREATE VIEW privilege for the
view, and some privilege for each column selected by the SELECT statement. For
columns used elsewhere in the SELECT statement, you must have the SELECT
privilege. If the OR REPLACE clause is present, you must also have the DROP
privilege for the view.
Some views are updatable and references to them can be used to specify tables to be updated in
data change statements. That is, you can use them in statements such as UPDATE, DELETE,
or INSERT to update the contents of the underlying table. Derived tables can also be specified in
multiple-table UPDATE and DELETE statements, but can only be used for reading data to
specify rows to be updated or deleted. Generally, the view references must be updatable,
meaning that they may be merged and not materialized. Composite views have more complex
rules.
For a view to be updatable there must be a one-to-one relationship between the rows in the view
and the rows in the underlying table. There are also certain other constructs that make a view
nonupdatable. To be more specific, a view is not updatable if it contains any of the following:
The WITH CHECK OPTION clause can be given for an updatable view to prevent inserts to
rows for which the WHERE clause in the select_statement is not true. It also prevents updates to
rows for which the WHERE clause is true but the update would cause it to be not true (in other
words, it prevents visible rows from being updated to nonvisible rows).
Student (snum: integer, sname: char(30), major: char(25), level: char(2), age: integer)
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.
2. Create another view named v2 which has the names of students who are enrolled in a course
taught by faculty member “Alen Bob”.
3. Create a view stdVu that is based on the student relation.
4. Alter the definition of student table. Add a column course to the student’s relation.
5. Notice the changes in views which are based on student relation. Comment what happens to
view data if the base table is 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’.
7. Create views based on the following queries also:
a. The names of students majoring in ‘Computer Science’.
b. The names of classes taught by ‘John Williams’ in dept # 68.
c. The distinct student ages in ‘Database Systems’ class in descending order.
d. The name of ‘Christopher Garcia’s teachers.
e. The snum and sname of students who have taken classes from both ‘Christopher Davis’ and
‘Linda Davis’.
Deliverables
Complete your lab tasks in SQL workbench and submit a word file with queries and the
screenshots of the results to all the questions attempted. Upload it on LMS. Late submissions
will not be accepted.