SQL View
SQL View
1
Learning Outcomes
2
Outline Materi
• Purpose of views.
• How to create and delete views using SQL.
• How the DBMS performs operations on
views.
Under what conditions views are
updatable.
• Advantages and disadvantages of
views.
• How the ISO transaction model works.
• How to use the GRANT and
REVOKE statements as a level of security. 3
Views
View
Dynamic result of one or more relational
operations operating on base relations to
produce another relation.
8
Example 6.4 - Create
Vertical View
Create view of staff details at
branch B003 excluding salaries.
CREATE VIEW Staff3
AS SELECT staffNo, fName, lName, position,
sex FROM Staff
WHERE branchNo = ‘B003’;
9
Example 6.5 - Grouped
and Joined Views
Create view of staff who manage
properties for rent, branch
number
including
they work at, staff number, and
number of properties they manage.
CREATE VIEWStaffPropCnt (branchNo,
staffNo, cnt)
AS SELECT s.branchNo, s.staffNo, COUNT(*)
FROM Staff s, PropertyForRent p
WHERE s.staffNo = p.staffNo
GROUP BY s.branchNo, s.staffNo;
10
Example 6.3 - Grouped
and Joined Views
11
SQL - DROP VIEW
12
SQL - DROP VIEW
13
View Resolution
14
View Resolution
20
View Updatability
21
View Updatability
23
View Updatability
26
WITH CHECK
OPTION
• Rows exist in a view because they satisfy
WHERE condition of defining query.
• If a row changes and no longer satisfies
condition, it disappears from the view.
• New rows appear within view when
insert/update on view cause them to satisfy
WHERE condition.
• Rows that enter or leave a view are
called
migrating rows.
• WITH CHECK OPTION prohibits a row
migrating out of the view. 27
WITH CHECK
OPTION
• LOCAL/CASCADED apply to view hierarchies.
• With LOCAL, any row insert/update on view
and any view directly or indirectly defined on
this view must not cause row to disappear
from view unless row also disappears from
derived view/table.
• With CASCADED (default), any row insert/
update on this view and on any view directly
or indirectly defined on this view must not
cause row to disappear from the view.
28
Example 6.6 - WITH CHECK
OPTION
CREATE VIEW Manager3Staff
AS SELECT *
FROM Staff
WHERE branchNo = ‘B003’
WITH CHECK OPTION;
• Cannot update branch number of row
B003 to B002 as this would cause row
to migrate from view.
• Also cannot insert a row into view with
a branch number that does not equal
B003.
29
Example 6.6 - WITH
CHECK OPTION
• If Manager3Staff is defined not on Staff
directly but on another view of Staff:
CREATE VIEW LowSalary
AS SELECT * FROM Staff WHERE salary > 9000;
CREATE VIEW HighSalary
AS SELECT * FROM
LowSalary WHERE salary
> 10000
WITH LOCAL CHECK OPTION;
CREATE VIEW
Manager3Staff
AS SELECT * FROM
HighSalary WHERE 30
branchNo = ‘B003’;
Example 6.6 - WITH CHECK
OPTION
UPDATE Manager3Staff
SET salary = 9500
WHERE staffNo = ‘SG37’;
• Update would fail: although update
would cause row to disappear from
HighSalary, row would not disappear
from LowSalary.
• However, if update tried to set salary to
8000, update would succeed as row
would no longer be part of LowSalary.
31
Example 6.6 - WITH
CHECK OPTION
32
Advantages of Views
• Data independence
• Currency
• Improved security
• Reduced complexity
• Convenience
• Customization
• Data integrity
33
Disadvantages of
Views
• Update restriction
• Structure restriction
• Performance
34
View Materialization
36
View Materialization
• If insert row into PropertyForRent with rent �400
then view would be unchanged.
• If insert row for property PG24 at branch B003
with staffNo = SG19 and rent = 550, then row
would appear in materialized view.
• If insert row for property PG54 at branch B003
with staffNo = SG37 and rent = 450, then no new
row would need to be added to materialized
view.
• If delete property PG24, row should be deleted
from materialized view.
• If delete property PG54, then row for PG37
should not be deleted (because of existing
property PG21).
37
Transactions
• SQL defines transaction model based on
COMMIT and ROLLBACK.
• Transaction is logical unit of work with one
or more SQL statements guaranteed to be
atomic with respect to recovery.
• An SQL transaction automatically begins
with a transaction-initiating SQL statement
(e.g., SELECT, INSERT).
• Changes made by transaction are
visible to not
transactionsother
until transaction completes.
concurrently executing
38
Transactions
41
Immediate and Deferred Integrity
Constraints
SET CONSTRAINTS
{ALL | constraintName [, . . . ]}
{DEFERRED ¦ IMMEDIATE}
42
Access Control - Authorization
Identifiers and Ownership
• Authorization identifier is normal SQL
identifier used to establish identity of a
user. Usually has an associated password.
• Used to determine which objects user may
reference and what operations may be
performed on those objects.
• Each object created in SQL has an owner,
as defined in AUTHORIZATION clause of
schema to which object belongs.
• Owner is only person who may know about
it.
43
Privileges
• Can restrict
INSERT/UPDATE/REFERENCES to
named columns.
• Owner of table must grant other users
the necessary privileges using GRANT
statement.
• To create view, user must have SELECT
privilege on all tables that make up view
and REFERENCES privilege on the
named columns.
45
GRANT
48
Example 6.9 - GRANT Specific
Privileges to PUBLIC
GRANT SELECT
ON Branch
TO PUBLIC;
49
REVOKE
50
REVOKE
52
Example 6.10/11 - REVOKE
Specific Privileges