Database Systems
Lab Manual 11
Views in SQL
SCHOOL OF INTERDISCIPLINARY ENGINEERING AND SCIENCES NATIONAL UNIVERSITY
OF SCIENCES AND TECHNOLOGY (NUST)
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.
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.
CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column1, column2,--
FROM table_name
WHERE condition;
Example:
Here's a basic example of how to create a view in SQL:
Let's say we have a table called employees with columns employee_id, first_name,
last_name, department, and salary.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
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.
CREATE VIEW employee_view AS
SELECT employee_id, first_name, department
FROM employees;
Once the view is created, you can query it just like a regular table:
SELECT * FROM employee_view;
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:
CREATE VIEW high_earners AS
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE salary > 50000;
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:
ALTER VIEW view_name AS
SELECT new_column_list
FROM new_table_list
WHERE new_conditions;
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:
CREATE VIEW employee_view AS
SELECT employee_id, first_name, department
FROM employees;
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:
ALTER VIEW employee_view AS
SELECT employee_id, first_name, last_name, department
FROM employees;
After executing this statement, the employee_view will include the last_name
column as well.
SQL Dropping a View:
A view is deleted with the DROP VIEW statement.
DROP VIEW view_name;
DROP VIEW employee_name;
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’.
8. Drop view v1, v2.