Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline Introduction Basic SQL Setting Up and Using PostgreSQL

Similar presentations


Presentation on theme: "Outline Introduction Basic SQL Setting Up and Using PostgreSQL"— Presentation transcript:

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 IndexName ON TableName(columnName[ASC|DESC][,…]) CREATE UNIQUE INDEX StaffNoInd ON Staff(staffNo); CREATE INDEX RentInd ON 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 Oracle Server iSQL*Plus SELECT * FROM empvu80;
USER_VIEWS EMPVU80 SELECT employee_id, last_name, salary FROM employees WHERE department_id=80; iSQL*Plus SELECT * FROM empvu80; EMPLOYEES Lu Wei

17 View Refer to page129 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.staffNo ORDER BY staffNo; Lu Wei

18 View Restriction on Views
If a column in the view is based on an aggregate function, 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 in 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_Student SET sName='刘辰' WHERE sNo=‘05002';
UPDATE Student SET sName='刘辰' WHERE sNo=‘05002' AND sDept='IS'; INSERT INTO IS_Student VALUES(‘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 migrating rows. The WITH CHECK OPTION clause of the CREATE VIEW 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 Manager3Staff SET branchNo = ‘B005’ WHERE staffNo = ‘SG37’; INSERT INTO Manager3Staff VALUES(‘SL15’,’Mary’,’Black’,’Assistant’, ’F’,’ ’,8000,’B002’); Lu Wei

26 View CREATE VIEW LowSalary AS SELECT *    FROM Staff    WHERE salary > 9000; CREATE VIEW HighSalary AS SELECT *    FROM LowSalary    WHERE salary > 10000 WITH LOCAL CHECK OPTION; Lu Wei

27 View CREATE VIEW Manager3Staff AS SELECT *    FROM HighSalary    WHERE branchNo = ‘B003’; UPDATE Manager3Staff SET salary = 9500(8000?) WHERE staffNo = ‘SG37’; 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 / rollback) 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) 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 constraints 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 TABLE ADD CONSTRAINT constraintName FOREIGN KEY(columnName) REFERENCES tableName.columnName DEFERRABLE 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 Access control Username and password Privileges Database administrator
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 view TO scott; Grant succeeded. REVOKE create session, create table, create sequence, create view FROM scott; 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 privileges
GRANT {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 select ON employees TO sue, rich;
Grant succeeded. GRANT update (department_name, location_id) ON departments TO scott, manager; Grant succeeded. REVOKE select, insert ON departments FROM scott; 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 Jeff
Emi GRANT There are no cascading effects when a system privilege is revoked, regardless of whether it was given the ADMIN OPTION. Read through the following steps that illustrate this. Scenario 1. The DBA grants the CREATE TABLE system privilege to Jeff with the ADMIN OPTION. 2. Jeff creates a table. 3. Jeff grants the CREATE TABLE system privilege to Emi. 4. Emi creates a table. 5. The DBA revokes the CREATE TABLE system privilege from Jeff. The result Jeff’s table still exists, but no new tables can be created. Emi’s table still exists and she still has the CREATE TABLE system privilege. DBA Jeff Emi REVOKE Lu Wei

57 Access control About WITH GRANT OPTION in object privilege Bob Jeff
Emi GRANT REVOKE Bob Jeff Emi Revoking Object Privileges (continued) Cascading effects can be observed when revoking a system privilege that is related to a DML operation. For example, if the SELECT ANY TABLE privilege is granted to a user, and that user has created procedures that use the table, all procedures that are contained in the user’s schema must be recompiled before they can be used again. Revoking object privileges will also cascade when given WITH GRANT OPTION. Read through the following steps that illustrate this. Scenario Jeff is granted the SELECT object privilege on EMPLOYEES with the GRANT OPTION. Jeff grants the SELECT privilege on EMPLOYEES to Emi. Later, the SELECT privilege is revoked from Jeff. This revoke is cascaded to Emi as well. 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 Lu Wei


Download ppt "Outline Introduction Basic SQL Setting Up and Using PostgreSQL"

Similar presentations


Ads by Google