Sei sulla pagina 1di 8

Views Views

RECALLS: o Contents of a view are defined as a query on one or


View more base relations.
Dynamic result of one or more relational
operations operating on the base relations to o Any operations on view are automatically translated
produce another relation. into operations on relations from which it is derived.

o Virtual relation that does not actually exist in the CREATE VIEW view_name [ (column_name [,...]) ]
database but is produced upon request, at time of AS subselect
request.
..

Advanced SQL - 1 Advanced SQL - 2

Example 1 - Create Horizontal View Example 2 - Create Vertical View


CREATE VIEW manager3_staff CREATE VIEW staff3
AS SELECT * AS SELECT sno, fname, lname, address,
FROM staff tel_no, position, sex
WHERE bno = 'B3';
FROM staff
WHERE bno = 'B3';
Or:
CREATE VIEW staff3
AS SELECT sno, fname, lname,address,
tel_no, position, sex
FROM manager3_staff;

Advanced SQL - 3 Advanced SQL - 4

CS3462 Introduction to Database Systems


Helena Wong, 2001
Example 2 - Create Vertical View SQL - DROP VIEW
DROP VIEW view_name
[RESTRICT | CASCADE]

o Causes definition of view to be deleted from the


database.

o For example: DROP VIEW manager3_staff;

o With CASCADE, all related dependent objects are


deleted; i.e. any views defined on view being dropped.

o With RESTRICT (default), if any other objects depend


for their existence on continued existence of view
being dropped, command is rejected.
Advanced SQL - 5 Advanced SQL - 6

Restrictions on Views Restrictions on Views

o SQL-92 imposes several restrictions on creation and o For example, following query would fail:
use of views. SELECT COUNT(cnt)
FROM staff_prop_cnt;
(a) If column in view is based on an aggregate function:
o Similarly, following query would also fail:
– Column may appear only in SELECT and ORDER
BY clauses of queries that access view. SELECT *
FROM staff_prop_cnt
– Column may not be used in WHERE nor be an WHERE cnt > 2;
argument to an aggregate function in any query
based on view.

Advanced SQL - 7 Advanced SQL - 8

CS3462 Introduction to Database Systems


Helena Wong, 2001
Restrictions on Views View Updatability

(b) Grouped view may never be joined with a base o Consider view Staff_Prop_Cnt:
table or a view.
CREATE VIEW staff_prop_cnt (branch_no,
o For example, Staff_Prop_Cnt view is a grouped staff_no, cnt)
view, so any attempt to join this view with another AS SELECT s.bno, s.sno, COUNT(*)
table or view fails.
FROM staff s, property_for_rent p
WHERE s.sno = p.sno
GROUP BY s.bno, s.sno;

Advanced SQL - 9 Advanced SQL - 10

View Updatability View Updatability


o SQL-92 specifies the views that must be updatable
o If change definition of view and replace count with in system that conforms to standard.
actual property numbers:
o Definition given is that a view is updatable iff:
CREATE VIEW staff_prop_list (branch_no,
staff_no, property_no) – DISTINCT is not specified.
AS SELECT s.bno, s.sno, p.pno
– Every element in SELECT list of defining query is
FROM staff s, property_for_rent p
a column name and no column appears more
WHERE s.sno = p.sno; than once.

– FROM clause specifies only one table, excluding


any views based on a join, union, intersection or
difference.

Advanced SQL - 11 Advanced SQL - 12

CS3462 Introduction to Database Systems


Helena Wong, 2001
View Updatability Integrity Enhancement Feature (IEF)
We consider 5 types of integrity constraints:
– WHERE clause does not include any nested
SELECTs that reference the table in FROM o Required data
clause. o Domain constraints

– There is no GROUP BY or HAVING clause in the o Entity integrity


defining query. o Referential integrity

o Also, every row added through view must not o Enterprise constraints
violate integrity constraints of base table.

o For view to be updatable, DBMS must be able to


trace any row or column back to its row or column
in the source table.

Advanced SQL - 13 Advanced SQL - 14

Integrity Enhancement Feature (IEF) IEF - Entity Integrity


1. Required Data
3. Entity Integrity
position VARCHAR(10) NOT NULL
o Primary key of a table must contain a unique, non-
2. Domain Constraints
null value for each row.
(a) CHECK
sex CHAR NOT NULL o PRIMARY KEY clause can be specified only once
CHECK (sex IN ('M', 'F')) per table. Can still ensure uniqueness for alternate
keys using UNIQUE.
b) Using DOMAIN
CREATE DOMAIN sex_type AS CHAR 4. Referential Integrity
CHECK (VALUE IN ('M', 'F'));
o If foreign key contains a value, that value must refer
sex SEX_TYPE NOT NULL to existing row in parent table.

Advanced SQL - 15 Advanced SQL - 16

CS3462 Introduction to Database Systems


Helena Wong, 2001
Example 3 - CREATE TABLE IEF - Referential Integrity
CREATE TABLE property_for_rent ( o Any INSERT/UPDATE that attempts to create FK
value in child table without matching candidate key
pno PROP_NUMBER NOT NULL, value in parent is rejected.
rooms PROPERTY_ROOMS NOT NULL
DEFAULT 4, o Action taken that attempts to update/delete a
rent PROPERTY_RENT NOT NULL, candidate key value in parent table with matching
ono OWNER_NUMBER NOT NULL, rows in child is dependent on referential action
specified using ON UPDATE/ and ON DELETE
bno BRANCH_NUMBER NOT NULL, subclauses:
PRIMARY KEY (pno), - CASCADE - SET NULL,

FOREIGN KEY (ono) REFERENCES owner ON - SET DEFAULT - NO ACTION.


DELETE NO ACTION ON UPDATE CASCADE);
Advanced SQL - 17 Advanced SQL - 18

IEF - Referential Integrity IEF - Referential Integrity

CASCADE: Delete row from parent and delete FOREIGN KEY (sno) REFERENCES staff
matching rows in child, and so on in cascading ON DELETE SET NULL
manner.
FOREIGN KEY (ono) REFERENCES owner
SET NULL: Delete row from parent and set FK ON UPDATE CASCADE
column(s) in child to NULL. Only valid if FK columns
are NOT NULL.

SET DEFAULT: Delete row from parent and set each


component of FK in child to specified default. Only
valid if DEFAULT specified for FK columns

NO ACTION: Reject delete from parent. Default.

Advanced SQL - 19 Advanced SQL - 20

CS3462 Introduction to Database Systems


Helena Wong, 2001
IEF - Enterprise Constraints ALTER TABLE
Enterprise Constraints
Example:
o Add a new column to a table.
CREATE TABLE property_for_rent( o Drop a column from a table.
pno …, o Add a new table constraint.
address …, o Drop a table constraint.
sno VARCHAR(5) o Set a default for a column.
CONSTRAINT staff_not_handling_too_much
o Drop a default for a column.
CHECK (NOT EXISTS (SELECT sno
FROM property_for_rent
GROUP BY sno
HAVING COUNT(*) > 10)),
bno ..);
Advanced SQL - 21 Advanced SQL - 22

Example 4 - ALTER TABLE Access Control - Privileges

ALTER TABLE staff o Usually, every user has an associated password.


ALTER position DROP DEFAULT; o Actions user permitted to carry out on given base
table or view:
ALTER TABLE staff
ALTER sex SET DEFAULT 'F'; SELECT Retrieve data from a table.
INSERT Insert new rows into a table.
ALTER TABLE property_for_rent UPDATE Modify rows of data in a table.
DROP CONSTRAINT staff_not_handling_too_much;
DELETE Delete rows of data from a table.
ALTER TABLE renter REFERENCES Reference columns of named table
ADD pref_area VARCHAR(15); in integrity constraints.
USAGE Use domains, collations, character sets,
and translations.
Advanced SQL - 23 Advanced SQL - 24

CS3462 Introduction to Database Systems


Helena Wong, 2001
Example 5 - GRANT All Privileges Example 6 - GRANT Specific Privileges

Give Manager full privileges to Staff table. Give Admin SELECT and UPDATE on column Salary
of Staff.
GRANT ALL PRIVILEGES
ON staff GRANT SELECT, UPDATE (salary)
TO manager WITH GRANT OPTION; ON staff
TO admin;

Advanced SQL - 25 Advanced SQL - 26

Example 7 - GRANT Specific Privileges to Multiple Users Example 8 - GRANT Specific Privileges to PUBLIC

Give users Personnel and Deputy SELECT on Staff Give all users SELECT on Branch table.
table.
GRANT SELECT
GRANT SELECT ON branch
ON staff TO PUBLIC;
TO personnel, deputy;

Advanced SQL - 27 Advanced SQL - 28

CS3462 Introduction to Database Systems


Helena Wong, 2001
Example 9 - REVOKE Specific Privileges from PUBLIC Example 10 - REVOKE Specific Privileges from Named User

Revoke privilege SELECT on Branch table from all Revoke all privileges given to Deputy on Staff table.
users.
REVOKE ALL PRIVILEGES
REVOKE SELECT ON staff
ON branch FROM deputy;
FROM PUBLIC ;

Advanced SQL - 29 Advanced SQL - 30

CS3462 Introduction to Database Systems


Helena Wong, 2001

Potrebbero piacerti anche