Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
-
Upload
mark-atkinson -
Category
Documents
-
view
220 -
download
2
Transcript of Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
Lu Wei 1
Outline
• Introduction• Basic SQL• Setting Up and Using PostgreSQL• Advanced SQL• Embeded SQL
Lu Wei 2
Advanced SQL
• Index• View• Transaction• Integrity Constraints• Access control
Lu Wei 3
Advanced SQL
• Index• View• Transaction• Integrity Constraints• Access control
Lu Wei 4
Index
• An index is a structure that provides accelerated access to the rows of a table based on the values of one or more columns.
• We will discuss the index in detail in chapter11.
Lu Wei 5
Index
• Creating an Index (CREATE INDEX)
• Examples
CREATE [UNIQUE] INDEX IndexNameON TableName(columnName[ASC|DESC][,…])
CREATE UNIQUE INDEX StaffNoIndON Staff(staffNo);
CREATE INDEX RentIndON PropertyForRent(city,rent);
Lu Wei 6
Index
• Removeing an Index (DROP INDEX)
• Examples
DROP INDEX IndexName
DROP INDEX RentInd;
Lu Wei 7
Advanced SQL
• Index• View• Transaction• Integrity Constraints• Access control
Lu Wei 8
View
• The dynamic result of one or more relational operations on the base relations to produce another relation.
• A view is a virtual relation that does not necessarily exist in the in the database but can be produced upon request by a particular user, at the time of request.
• The DBMS stores the definition of the view in the database
Lu Wei 9
View
Lu Wei 10
View
• Creating a View (CREATE VIEW)
– If WITH CHECK OPTION is specified, SQL ensures that if a row fails to satisfy the WHERE clause of the defining query of a view, it is not added to the underlying base table of the view.
CREATE VIEW ViewName[(newColumnName[,…])]AS subselect [WITH [CASCADED|LOCAL] CHECK OPTION];
Lu Wei 11
View
• Examples – Horizontal views
CREATE VIEW IS_Student AS SELECT * FROM Student WHERE sDept='IS';
CREATE VIEW Manager3Staff AS SELECT * FROM Staff WHERE branchNo = ‘B003’;
Lu Wei 12
View
– Vertical views
CREATE VIEW Staff3 AS SELECT staffNo,fName,lName,position,sex
FROM Staff WHERE branchNo = ‘B003’;
CREATE VIEW Staff3 AS SELECT staffNo,fName,lName,position,sex
FROM Manager3Staff;
Lu Wei 13
View
– Grouped and joined views
Create a view of students who is in department ‘IS’ and elect the course ‘001’
CREATE VIEW IS_S1(sNo, sName, score) AS SELECT Student.sNo, sName, score FROM Student, SC WHERE sDept='IS' AND Student.sNo=SC.sNo AND SC.cNo='1';
Lu Wei 14
View
Create a view of staff who manage properties for rent, which includes the branch number they work at, their staff number, and the 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;
Lu Wei 15
View
• Removing a View (DROP VIEW)
– If CASCAE is specified, DROP VIEW deletes all related dependent objects, in other words, all objects that reference the view.
DROP VIEW ViewName [RESTRICT|CASCADE];
DROP VIEW Manager3Staff CASCADE;
Lu Wei 16
View
• View Resolution
SELECT *FROM empvu80;
iSQL*Plus
Oracle Server
USER_VIEWS EMPVU80
SELECT employee_id, last_name, salaryFROM employeesWHERE department_id=80;
EMPLOYEES
Lu Wei 17
View
SELECT staffNo,cnt FROM StaffPropCnt WHERE branchNo = ‘B003’ORDER BY staffNo;
SELECT s.staffNo AS staffNo,COUNT(*) AS cnt
FROM Staff s,PropertyForRent p WHERE s.staffNo = p.staffNo AND branchNo = ‘B003’GROUP BY s.branchNo,s.staffNoORDER BY staffNo;
Refer to page129
Lu Wei 18
View
• Restriction on Views– If a column in the view is based on an aggregate funct
ion, then the column may appear only in SELECT and ORDER BY clauses of queries that access the view. In particular, such a column may not be used in a WHERE clause and may not be an argument to an aggregate function in any query based on the view.
– A grouped view may never be joined with a base table or a view.
Lu Wei 19
View
SELECT COUNT(cnt) FROM StaffPropCnt;
SELECT * FROM StaffPropCnt
WHERE cnt > 2;
Lu Wei 20
View
• View Updatability– All updates to a base table are immediately reflected i
n all views that encompass that base table. – Similarly,we may except that if a view is updated then
the base table(s) will reflect that change. – Consider that if any view is updatable
INSERT INTO StaffPropCnt
VALUES(‘B003’,’SG5’,2);
Lu Wei 21
View
CREATE VIEW StaffPropList(branchNo,staffNo,propertyNo)
AS SELECT s.branchNo,s.staffNo,p.propertyNo
FROM Staff s,PropertyForRent p
WHERE s.staffNo = p.staffNo;
INSERT INTO StaffPropList
VALUES(‘B003’,’SG5’,’PG19’);
Lu Wei 22
View
For a view to be updatable, the DBMS must be able to trace any row or column back to its row or column in the source table.
Refere to page131 for upatable view definition given in the ISO standard.
Lu Wei 23
View
UPDATE IS_StudentSET sName='刘辰 'WHERE sNo=‘05002';UPDATE Student SET sName='刘辰 ' WHERE sNo=‘05002' AND sDept='IS';
INSERTINTO IS_StudentVALUES(‘05029’, ‘赵新’ , 20);
INSERT INTO Student(Sno,Sname,Sage,Sdept)VALUES(‘05029', '赵新 ', 20, 'IS');
Lu Wei 24
View
• WITH CHECK OPTION– The rows that enter or leave a view are called migrati
ng rows.– The WITH CHECK OPTION clause of the CREATE VI
EW statement prohibits a row migrating out of the view.
Lu Wei 25
View
CREATE VIEW Manager3Staff AS SELECT * FROM Staff WHERE branchNo = ‘B003’ WITH CHECK OPTION;
UPDATE Manager3StaffSET branchNo = ‘B005’WHERE staffNo = ‘SG37’;
INSERT INTO Manager3StaffVALUES(‘SL15’,’Mary’,’Black’,’Assistant’, ’F’,’1985-06-21’,8000,’B002’);
Lu Wei 26
View
CREATE VIEW LowSalaryAS SELECT * FROM Staff WHERE salary > 9000;
CREATE VIEW HighSalaryAS SELECT * FROM LowSalary WHERE salary > 10000WITH LOCAL CHECK OPTION;
Lu Wei 27
View
UPDATE Manager3StaffSET salary = 9500(8000?)WHERE staffNo = ‘SG37’;
CREATE VIEW Manager3StaffAS SELECT * FROM HighSalary WHERE branchNo = ‘B003’;
Lu Wei 28
View
• Advantages of using view– Data independence– Currently– Improved security– Reduced complexity– Convenience– Customization– Data integrity
Lu Wei 29
View
• Disadvantages of using view– Update restriction– Structure restriction– Performance
•
Lu Wei 30
Advanced SQL
• Index• View• Transaction• Integrity Constraints• Access control
Lu Wei 31
Transaction
• A transaction is a sequence of database statements that needs to execute atomically .
• A database transaction consists of one of the following:– DML statements which constitute one consistent change to the data.
– One DDL statement– One DCL statement
Lu Wei 32
Transaction
• Beginning and end of transaction– Implicitly declare– Explicitly declare (begin transaction, commit / rollbac
k)– Programmatic SQL aborts.
Lu Wei 33
Transaction
• Beginning and end of transaction in PostgreSQL through interactive terminal
SELECT * FROM librarian; INSERT INTO librarian VALUES('Mary');
BEGIN TRANSACTION (implicit) SELECT * FROM librarian; commit (implicit) BEGIN TRANSACTION (implicit) INSERT INTO librarian VALUES('Mary'); COMMIT(implicit)
Lu Wei 34
Transaction
BEGIN TRANSACTION; (explicit) SELECT * FROM librarian; INSERT INTO librarian VALUES('Mary'); COMMIT; (explicit)
Lu Wei 35
Transaction
• Configure certain aspects of the transaction
SET TRANSACTION[READ ONLY|READ WRITE]|[ISOLATION LEVEL READ UNCOMMITTED| READ COMMITTED| REPEATABLE READ| SERIALIZABLE]
Lu Wei 36
Advanced SQL
• Index• View• Transaction• Integrity Constraints• Access control
Lu Wei 37
Integrity Constraints
• Immediate and Deferred Integrity Constraints– In some situations, we do not want integrity constraint
s to be checked immediately, that is after every SQL statement has been executed, but instead at transaction commit
SET CONSTRAINTS{ALL|constraintName[,…]}{[NOT]DEFERRABLE}[INITIALLY IMMEDIATE|DEFEREED]
Lu Wei 38
Integrity Constraints
ALTER TABLEADD CONSTRAINT constraintNameFOREIGN KEY(columnName) REFERENCES tableName.columnNameDEFERRABLE INITIALLY DEFERRED/IMMEDIATE
Lu Wei 39
Advanced SQL
• Index• View• Transaction• Integrity Constraints• Access control
Lu Wei 40
Access control
• DBMS should provide a mechanism to ensure that only authorized users can access the database.
• SQL provide the GRANT and REVOKE statement to allow security to be set up.
Lu Wei 41
Username and passwordPrivileges
Access control
Databaseadministrator
Lu Wei 42
Access control
• Database security– System security– Data security
• System privileges: Gaining access to the database
• Object privileges: Manipulating the content of the database objects
Lu Wei 43
Access control
• System privileges– There are many system privileges available according
to different DBMSs.
• The database administrator has high-level system privileges for tasks such as:– Creating new users– Removing users– Removing tables
Lu Wei 44
Access control
• An application developer, for example, may have the following system privileges:– CREATE SESSION– CREATE TABLE– CREATE VIEW– CREATE PROCEDURE
Lu Wei 45
Access control
• Grant and revoke system privileges
GRANT {system_privilege|role}
[, {system_privilege|role} ]…
TO {user|role||PUBLIC}
[, {user|role||PUBLIC}][WITH ADMIN OPTION];
REVOKE {system_privilege|role}
[, {system_privilege|role} ]…
FROM {user|role||PUBLIC}
[, {user|role||PUBLIC}]...;
Lu Wei 46
Access control
• Examples
GRANT create session, create table, create sequence, create viewTO scott;Grant succeeded.Grant succeeded.
GRANT create session, create table, create sequence, create viewTO scott;Grant succeeded.Grant succeeded.
REVOKE create session, create table, create sequence, create viewFROM scott;Revoke succeeded.Revoke succeeded.
REVOKE create session, create table, create sequence, create viewFROM scott;Revoke succeeded.Revoke succeeded.
Lu Wei 47
Access control
• Object previliges– Object privileges vary from object to object.– An owner has all the privileges on the object.
Lu Wei 48
Access control
Object Privilege Table View Sequence Procedure
ALTER
DELETE
EXECUTE
INDEX
INSERT
REFERENCES
SELECT
UPDATE
Lu Wei 49
Access control
• Grant and revoke object privilegesGRANT {object_privilege[(column_list)]
[, object_privilege[(column_list)] …
|ALL [PRIVILEGES]}
ON [schema.]object
TO {user|role|PUBLIC}[,{user|role|PUBLIC}][WITH GRANT OPTION];
REVOKE {object_privilege
[, object_privilege ]…|ALL [PRIVILEGES]}
ON [schema.] object
FROM {user|role||PUBLIC}
[, {user|role||PUBLIC}]...
[CASCADE CONSTRAINTS];
Lu Wei 50
Access control
• Examples GRANT selectON employeesTO sue, rich;Grant succeeded.Grant succeeded.
GRANT selectON employeesTO sue, rich;Grant succeeded.Grant succeeded.
GRANT update (department_name, location_id)ON departmentsTO scott, manager;Grant succeeded.Grant succeeded.
GRANT update (department_name, location_id)ON departmentsTO scott, manager;Grant succeeded.Grant succeeded.
REVOKE select, insertON departmentsFROM scott;Revoke succeeded.Revoke succeeded.
REVOKE select, insertON departmentsFROM scott;Revoke succeeded.Revoke succeeded.
Lu Wei 51
Access control
• GRANT ALL PRIVILEGES
GRANT ALL PRIVILIGES
ON Staff
TO Manager
WITH GRANT OPTION;
GRANT ALL PRIVILIGES
ON Student, Course
TO U1;
Lu Wei 52
Access control
• GRANT specific privileges
GRANT SELECT, UPDATE (salary)
ON Staff
TO Personnel, Director;
Lu Wei 53
Access control
• GRANT specific privileges to PUBLIC
GRANT SELECT
ON Branch
TO PUBLIC;
Lu Wei 54
Access control
• REVOKE specific privileges from PUBLIC
REVOKE SELECT
ON Branch
FROM PUBLIC;
Lu Wei 55
Access control
• GRANT specific privileges from named user
REVOKE ALL PRIVILEGES
ON Staff
FROM Director;
Lu Wei 56
Access control
• About WITH ADMIN OPTION in system privilege DBA
GRANT
REVOKE
Jeff Emi
Jeff EmiDBA
Lu Wei 57
Access control
• About WITH GRANT OPTION in object privilege
GRANT
REVOKE
Bob Jeff Emi
EmiJeffBob
Lu Wei 58
Summary
• In this part you should have learned:– How to create and drop view and index– The concept of transaction– How to define integrity constraint– How to grant and revoke privileges