Database Lab 11
Database Lab 11
Lab Manual 11
Views in SQL
Objectives
After completing this lab, you should be able to do the following:
➢ Create a view.
➢ Alter/Update a view.
➢ Alter a table.
VIEW
• In SQL, a view is a virtual table based on the result set of an SQL statement.
• A view contains rows and columns, just like a real table. The fields in a view
are fields from one or more real tables in the database.
• You can add SQL statements and functions to a view and present the data as
if the data were coming from one single table.
• A view is created with the CREATE VIEW statement.
1
Now, let's create a view called employee_view that selects only the employee_id,
first_name, and department columns from the employee's table.
Once the view is created, you can query it just like a regular table:
This will return a result set with only the employee_id, first_name, and department
columns from the employee's table.
Views can also be more complex, involving joins, aggregations, and subqueries. For
example:
This view selects employees who earn more than $50,000 per year.
Alter View:
To modify an existing view in SQL, you use the ALTER VIEW statement followed
by the AS clause with the new definition of the view. Here's the syntax:
You can only alter the SELECT statement portion of the view. This means you can
change the columns selected, the tables involved, and the conditions used to filter
data.
2
Here's an example of altering an existing view:
Let's say we have the employee_view created earlier:
Now, suppose we want to include the last names of employees in our view. We can
alter the view to include the last_name column:
After executing this statement, the employee_view will include the last_name
column as well.
3
Lab Task:
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)
Student Table:
Faculty Table:
Class Table:
Enrolled Table:
4
1. Create a view named v1 which has the names 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 ‘Charlie’s teachers.
e. The snum and sname of students who have taken classes from both ‘John
Williams’ and ‘AlexBob’.