1b. Basic and Advanced SQL (DDL)
1b. Basic and Advanced SQL (DDL)
Systems
Views Views
View Contents of a view are defined as a query on
Dynamic result of one or more relational one or more base relations.
operations operating on base relations to With view resolution, any operations on view
produce another relation. are automatically translated into operations
on relations from which it is derived.
• Virtual relation that does not necessarily With view materialization, the view is stored
actually exist in the database but is produced as a temporary table, which is maintained as
upon request, at time of request. the underlying base tables are updated.
Example 7.3 - Create Horizontal View Example 7.4 - Create Vertical View
Create view so that manager at branch B003 can Create view of staff details at branch B003
only see details for staff who work in his or her excluding salaries.
office.
CREATE VIEW Staff3
CREATE VIEW Manager3Staff
AS SELECT * AS SELECT staffNo, fName, lName, position, sex
FROM Staff FROM Staff
WHERE branchNo = ‘B003’; WHERE branchNo = ‘B003’;
Example 7.5 - Grouped and Joined Views Example 7.3 - Grouped and Joined Views
Create view of staff who manage properties
for rent, including branch number they work
at, staff number, and number of properties
they manage.
CREATE VIEW StaffPropCnt (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;
Example 7.6 - WITH CHECK OPTION Example 7.6 - WITH CHECK OPTION
Now consider the following: UPDATE Manager3Staff
SET salary = 9500
CREATE VIEW LowSalary WHERE staffNo = ‘SG37’;
ASSELECT * FROM Staff WHERE salary > 9000;
CREATE VIEW HighSalary
• This update would fail: although update
would cause row to disappear from
ASSELECT * FROM LowSalary
HighSalary, row would not disappear from
WHERE salary > 10000
LowSalary.
WITH LOCAL CHECK OPTION;
• However, if update tried to set salary to 8000,
CREATE VIEW Manager3Staff
update would succeed as row would no
ASSELECT * FROM HighSalary
longer be part of LowSalary.
WHERE branchNo = ‘B003’;
Pearson Education © 2014 51 Pearson Education © 2014 52
Transactions Transactions
SQL defines transaction model based on Transaction can complete in one of four ways:
COMMIT and ROLLBACK. - COMMIT ends transaction successfully,
Transaction is logical unit of work with one or making changes permanent.
more SQL statements guaranteed to be atomic - ROLLBACK aborts transaction, backing out
with respect to recovery. any changes made by transaction.
An SQL transaction automatically begins with - For programmatic SQL, successful program
a transaction-initiating SQL statement (e.g., termination ends final transaction
SELECT, INSERT). successfully, even if COMMIT has not been
Changes made by transaction are not visible executed.
to other concurrently executing transactions - For programmatic SQL, abnormal program
until transaction completes. end aborts transaction.
Pearson Education © 2014 59 Pearson Education © 2014 60
Immediate and Deferred Integrity Constraints Access Control - Authorization Identifiers and Ownership
MySql
CREATE USER ‘username@localhost’
IDENTIFIED BY ‘password’;
Pearson Education © 2014 65 Pearson Education © 2014 66
Privileges GRANT
Can restrict INSERT/UPDATE/REFERENCES to GRANT {PrivilegeList | ALL PRIVILEGES}
named columns. ON ObjectName
Owner of table must grant other users the TO {AuthorizationIdList | PUBLIC}
necessary privileges using GRANT statement. [WITH GRANT OPTION]
To create view, user must have SELECT PrivilegeList consists of one or more of above
privilege on all tables that make up view and privileges separated by commas.
REFERENCES privilege on the named columns.
ALL PRIVILEGES grants all privileges to a user.
REVOKE REVOKE
GRANT OPTION FOR allows privileges passed
on via WITH GRANT OPTION of GRANT to be
revoked separately from the privileges
themselves.
REVOKE fails if it results in an abandoned
object, such as a view, unless the CASCADE
keyword has been specified.
Privileges granted to this user by other users
are not affected.