CS220 – DATABASE SYSTEMS
WEEK 11 LECTURE 2
SQL VIEWS
Ms. Hirra Anwar
Assistant Professor
Faculty of Computing
[email protected]VIEWS
2
Objectives
3
After completing this lesson, you
should be able to do the following:
Describe a view
Create, alter, and drop a view
Retrieve data through View
Insert, update, and delete data through
a view
Database Objects
4
Object Description
Table Basic unit of storage; composed of rows
and columns
View Logically represents subsets of data from
one or more tables
Sequence Generates primary key values
Index Improves the performance of some queries
Synonym Alternative name for an object
What is a View?
5
EMPLOYEES Table:
Why use Views?
6
To restrict data access
To make complex queries easy
To provide data independence
To present different views of the same
data
Simple and Complex Views
7
Feature Simple Views Complex Views
Number of tables One One or more
Contain functions No Yes
Contain groups of dataNo Yes
DML operations
through a view Yes Not always
Creating a View
8
You embed a subquery within the CREATE VIEW
statement.
CREATE
CREATE [OR
[OR REPLACE]
REPLACE] VIEW
VIEW view
view
[(alias[,
[(alias[, alias]...)]
alias]...)]
AS
AS subquery
subquery
[WITH
[WITH CHECK
CHECK OPTION
OPTION [CONSTRAINT
[CONSTRAINT constraint]]
constraint]]
[WITH
[WITH READ
READ ONLY
ONLY [CONSTRAINT
[CONSTRAINT constraint]];
constraint]];
The subquery can contain complex SELECT syntax.
With read only is not supported by MySQL.
Creating a View
9
Create a view, EMPVU80, that contains
details of employees in department 80.
CREATE VIEW empvu80
AS SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 80;
View created.
Describe the structure of the view by using
the SHOW CREATE VIEW viewname
command.
SHOW
SHOW CREATE
CREATE VIEW
VIEW empvu80
empvu80
Creating a View
10
Create a view by using column aliases in
the subquery.
CREATE VIEW salvu50
AS SELECT employee_id ID_NUMBER, last_name NAME,
salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;
View created.
Select the columns from this view by the
given alias names.
Retrieving data from view
11
SELECT *
FROM salvu50;
Querying a View
12
Database Server
SQL Developer
USER_VIEWS
SELECT * EMPVU80
FROM empvu80; SELECT employee_id,
last_name, salary
FROM employees
WHERE department_id=80;
EMPLOYEES
Modifying a View
13
Modify the EMPVU80 view by using DROP
VIEW and CREATE View clause. Add an
alias for each column name.
CREATE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' ' || last_name,
salary, department_id
FROM employees
WHERE department_id = 80;
View created.
Column aliases in the CREATE VIEW clause
are listed in the same order as the
columns in the subquery.
Creating a Complex View
14
Create a complex view that contains group
functions to display values from two tables.
Complex views are not supported in MySQL.
CREATE VIEW dept_sum_vu
(name, minsal, maxsal, avgsal)
AS SELECT d.department_name, MIN(e.salary),
MAX(e.salary),AVG(e.salary)
FROM employees e, departments d
WHERE e.department_id = d.department_id
GROUP BY d.department_name;
View created.
Rules for Performing
DML Operations on a View
15
You can perform DML operations on simple views.
You cannot remove a row if the view contains the
following:
Group functions
A GROUP BY clause
The DISTINCT keyword
Multiple tables
Rules for Performing
DML Operations on a View
16
You cannot modify data in a view if it contains:
Group functions
A GROUP BY clause
The DISTINCT keyword
The pseudocolumn ROWNUM keyword
Columns defined by expressions
Rules for Performing
DML Operations on a View
17
You cannot add data through a view if the view
includes:
Group functions
A GROUP BY clause
The DISTINCT keyword
The pseudocolumn ROWNUM keyword
Columns defined by expressions
NOT NULL columns in the base tables that
are not selected by the view
Using the WITH CHECK OPTION Clause
18
You can ensure that DML operations performed
on the view stay within the domain of the view
by using the WITH CHECK OPTION clause.
CREATE VIEW empvu20
AS SELECT *
FROM employees
WHERE department_id = 20
WITH CHECK OPTION;
View created.
Any attempt to change the department
number for any row in the view fails because it
violates the WITH CHECK OPTION constraint.
Denying DML Operations
20
CREATE VIEW empvu10
(employee_number, employee_name, job_title)
AS SELECT employee_id, last_name, job_id
FROM employees
WHERE department_id = 10
View created.
Denying DML Operations
21
Complex views
Multiple tables
Joins
Group by
Removing a View
22
You can remove a view without
losing data because a view is based
on underlying tables in the
database.
DROP
DROP VIEW
VIEW IF
IF EXISTS
EXISTS viewname;
viewname;
DROP VIEW empvu80;
View dropped.
Example
23
Appointments (Appointment_ID,
Doctor_ID, Patient_ID,
Appointment_Date, A_Status)
Doctors (Doctor_ID, Name,
Specialization)
Patients (Patient_ID, Name, Age,
Status)
Example
24
Create a view of active patients and then
insert data into it.
CREATE VIEW ActivePatients AS
SELECT PatientID, Name, Status
FROM Patients
WHERE Status = 'Active’
WITH CHECK OPTION;
25
Without WITH CHECK OPTION:
Updates and deletes can be made, even
if the record doesn't meet the view's
condition.
This may lead to inconsistent data where
the data in the view and the underlying
table do not match.
With WITH CHECK OPTION:
Any changes made through the view
must satisfy the condition in the WHERE
clause of the view. This prevents
Example
26
Create a view of active patients and then
insert data into it.
INSERT INTO ActivePatients (PatientID,
Name, Status, Age) VALUES (201,
'Amina', 'Active', 27);
INSERT INTO ActivePatients (PatientID,
Name, Status, Age) VALUES (203, Ali',
'Active', 32);
Example
27
Update as follows and then perform a
delete operation
DELETE FROM ActivePatients
WHERE PatientID = 201;
Example
28
Update data through the view:
UPDATE ActivePatients
SET Status = 'Inactive'
WHERE PatientID = 101;
Example 2
29
Create a View of Only Confirmed
Appointments.
Insert data using the view.
Update data using the view.
Delete data using the view.
Version 1
30
CREATE VIEW ConfirmedAppointments AS
SELECT
a.AppointmentID,
d.Name AS DoctorName,
p.Name AS PatientName,
a.AppointmentDate,
a.Status
FROM Appointments a
JOIN Doctors d ON a.DoctorID = d.DoctorID
JOIN Patients p ON a.PatientID = p.PatientID
WHERE a.Status = 'Confirmed';
Version 2
31
CREATE VIEW SimpleConfirmedAppointments
AS
SELECT AppointmentID, DoctorID, PatientID,
AppointmentDate, Status
FROM Appointments
WHERE Status = 'Confirmed'
WITH CHECK OPTION;
INSERT INTO SimpleConfirmedAppointments
(AppointmentID, DoctorID, PatientID,
AppointmentDate, Status)
VALUES (1004, 1, 102, '2025-04-26', 'Confirmed');
Version 2
32
UPDATE SimpleConfirmedAppointments
SET AppointmentDate = '2025-04-30'
WHERE AppointmentID = 1004;
DELETE FROM
SimpleConfirmedAppointments
WHERE AppointmentID = 1004;
Summary
33
In this lesson, you should have learned
that a view is
derived from data in other tables or
views and
provides the following advantages:
Restricts database access
Simplifies queries
Provides data independence
Provides multiple views of the same
data
Can be dropped without removing the
underlying data
34
THANK YOU