Lecture - SQL Views
Lecture - SQL Views
WEEK 11 LECTURE 2
SQL VIEWS
2
Objectives
3
a view
Database Objects
4
Object Description
EMPLOYEES Table:
Why use Views?
6
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.
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
A GROUP BY clause
A GROUP BY clause
Complex views
Multiple tables
Joins
Group by
Removing a View
22
Appointments (Appointment_ID,
Doctor_ID, Patient_ID,
Appointment_Date, A_Status)
Doctors (Doctor_ID, Name,
Specialization)
Patients (Patient_ID, Name, Age,
Status)
Example
24
Example
27
UPDATE ActivePatients
SET Status = 'Inactive'
WHERE PatientID = 101;
Example 2
29
UPDATE SimpleConfirmedAppointments
SET AppointmentDate = '2025-04-30'
WHERE AppointmentID = 1004;
DELETE FROM
SimpleConfirmedAppointments
WHERE AppointmentID = 1004;
Summary
33
Simplifies queries
data
Can be dropped without removing the
underlying data
34
THANK YOU