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

Lab 11

The document describes a lab on views in SQL. It covers creating, altering, and dropping views, as well as updatable views. The lab tasks involve creating views based on given schemas and queries, altering views and tables, observing the effects of changes, and exploring updatable views.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab 11

The document describes a lab on views in SQL. It covers creating, altering, and dropping views, as well as updatable views. The lab tasks involve creating views based on given schemas and queries, altering views and tables, observing the effects of changes, and exploring updatable views.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Mathematics

CS220: Database Systems

Class: BS Mathematics Semester 4


Lab 11: Views in SQL

Date: May 19, 2021


Time: 02:00-05:00
Instructor: Ms. Naheeda Parveen

Lab Engineer: Sundas Dawood

CS220: Database Systems Page 1


Lab 11: Views in MySQL
Introduction
This lab will focus on the types of database objects other than tables. In this lab you will practice
defining, modifying and using views.

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.

CREATE VIEW/ ALTER VIEW

Syntax

The syntax for the CREATE VIEW statement in MySQL is:

CREATE [OR REPLACE] VIEW view_name AS


SELECT columns
FROM tables
WHERE conditions
[WITH [CASCADED | LOCAL] CHECK OPTION];

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

CS220: Database Systems Page 2


same as CREATE VIEW. If the view does exist, CREATE OR REPLACE VIEW
is the same as ALTER VIEW.

The ALTER VIEW statement does the same thing as CREATE OR REPLACE.
The full ALTER statement looks like this:

ALTER [<algorithm attributes>] VIEW [<database>.]< name> [(<columns>)] AS


<SELECT statement> [<check options>]

The select_statement is a SELECT statement that provides the definition of the


view. (Selecting from the view selects, in effect, using the SELECT statement.)
The select_statement can select from base tables or other views. The view
definition is “frozen” at creation time. Changes to the underlying tables afterward
do not affect the view definition. For example, if a view is defined as SELECT *
on a table, new columns added to the table later do not become part of the view.

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.

CS220: Database Systems Page 3


Updatable and Insertable Views

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:

 Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth)


 DISTINCT
 GROUP BY
 HAVING
 UNION or UNION ALL
 Subquery in the select list (fails for INSERT, okay for UPDATE, DELETE)
 Certain joins (see additional join discussion later in this section)
 Reference to nonupdatable view in the FROM clause
 Subquery in the WHERE clause that refers to a table in the FROM clause
 Refers only to literal values (in this case, there is no underlying table to update)
 Multiple references to any column of a base table (fails for INSERT, okay
for UPDATE, DELETE)

The View WITH CHECK OPTION Clause

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).

CS220: Database Systems Page 4


Lab Tasks
Given the following database schema:

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’.

8. Drop view v1, v2.


9. Explore updatable views. Try to update a base table using a view (Hint: With Check Option
might be required). Write down your observations what happens. Observe when can you
update the base table and when you can not.

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.

CS220: Database Systems Page 5

You might also like