Outline Introduction Basic SQL Setting Up and Using PostgreSQL

Slides:



Advertisements
Similar presentations
Views-basics 1. 2 Introduction a view is a perspective of the database different users may need to see the database differently; this is achieved through.
Advertisements

Access Control & Views Reading: C&B, Chap 7. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the principles of object.
14-1 Copyright  Oracle Corporation, All rights reserved. Privileges Database security: – System security – Data security System privileges: Gain.
13 Copyright © Oracle Corporation, All rights reserved. Controlling User Access.
Chapter 23 Database Security and Authorization Copyright © 2004 Pearson Education, Inc.
Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application.
1 Minggu 5, Pertemuan 10 SQL: Views and Access Control Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
System Administration Accounts privileges, users and roles
1 Pertemuan 15 SQL View Matakuliah: >/ > Tahun: > Versi: >
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
By Lecturer / Aisha Dawood 1.  Administering Users  Create and manage database user accounts.  Create and manage roles.  Grant and revoke privileges.
Copyright س Oracle Corporation, All rights reserved. 14 Controlling User Access.
Database Programming Sections 13–Creating, revoking objects privileges.
Chapter 6 SQL Data Definition Language Chapter 7 in Textbook.
10/25/2012ISC239 Isabelle Bichindaritz1 SQL Commands.
Controlling User Access. Objectives After completing this lesson, you should be able to do the following: Create users Create roles to ease setup and.
Chapter 6 Database Administration
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
1 Chapter 6 Database Administration. 2 Introduction Database administration The process of managing a database Database administrator A person or an entire.
SQL : Data Definition Session 9 – 10 Course Name: Database System Year : 2012.
Controlling User Access Fresher Learning Program January, 2012.
Chapter 6 SQL: Data Definition Transparencies. 2 Chapter 6 - Objectives u Data types supported by SQL standard. u Purpose of integrity enhancement feature.
Chapter 6 SQL: Data Definition Transparencies Last Updated: 10 th March 2011 By M. Arief
Chapter Name SQL: Data Definition
Database Security. Multi-user database systems like Oracle include security to control how the database is accessed and used for example security Mechanisms:
Copyright © 2004, Oracle. All rights reserved. CONTROLLING USER ACCESS Oracle Lecture 8.
Chapter 6 SQL: Data Definition Transparencies © Pearson Education Limited 1995, 2005.
CSC271 Database Systems Lecture # 17. Summary: Previous Lecture  View updatability  Advantages and disadvantages of views  View materialization.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Data Definition Language
Chapter 6 SQL: Data Definition Transparencies © Pearson Education Limited 1995, 2005.
Oracle 11g: SQL Chapter 7 User Creation and Management.
13 Copyright © Oracle Corporation, All rights reserved. Controlling User Access.
Database Security. Multi-user database systems like Oracle include security to control how the database is accessed and used for example security Mechanisms:
1 Copyright © 2006, Oracle. All rights reserved. Controlling User Access ( 사용자 접근 제어 )
1 Copyright © 2009, Oracle. All rights reserved. Controlling User Access.
Chapter 7 SQL – Data Definition Pearson Education © 2014.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
1 Copyright © 2005, Oracle. All rights reserved. Oracle Database Administration: Overview.
Copyright  Oracle Corporation, All rights reserved. 14 Controlling User Access.
Controlling User Access
Controlling User Access
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
Managing Privileges.
Controlling User Access
SQL: Data Definition Transparencies
Objectives User access Create users Create roles
TABLES AND INDEXES Ashima Wadhwa.
Controlling User Access
Managing Privileges.
The Basics of Data Manipulation
IS221: Database Management
Database Security.
Chapter 7 SQL – Data Definition Pearson Education © 2014.
Database Security.
The Basics of Data Manipulation
Chapter 2 Views.
Chapter 7 SQL – Data Definition Pearson Education © 2014.
Chapter 2 Views.
Managing Privileges.
IST 318 Database Administration
Presentation transcript:

Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL Lu Wei

Advanced SQL Index View Transaction Integrity Constraints Access control Lu Wei

Advanced SQL Index View Transaction Integrity Constraints Access control Lu Wei

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

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

Index Removeing an Index (DROP INDEX) Examples DROP INDEX IndexName DROP INDEX RentInd; Lu Wei

Advanced SQL Index View Transaction Integrity Constraints Access control Lu Wei

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

View Lu Wei

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

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

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

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

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

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

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

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

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

View SELECT COUNT(cnt) FROM StaffPropCnt; SELECT * FROM StaffPropCnt WHERE cnt > 2; Lu Wei

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

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

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

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

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

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’,’1985-06-21’,8000,’B002’); Lu Wei

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

View CREATE VIEW Manager3Staff AS SELECT *    FROM HighSalary    WHERE branchNo = ‘B003’; UPDATE Manager3Staff SET salary = 9500(8000?) WHERE staffNo = ‘SG37’; Lu Wei

View Advantages of using view Data independence Currently Improved security Reduced complexity Convenience Customization Data integrity Lu Wei

View Disadvantages of using view Update restriction Structure restriction Performance Lu Wei

Advanced SQL Index View Transaction Integrity Constraints Access control Lu Wei

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

Transaction Beginning and end of transaction Implicitly declare Explicitly declare (begin transaction, commit / rollback) Programmatic SQL aborts. Lu Wei

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

Transaction BEGIN TRANSACTION; (explicit) SELECT * FROM librarian; INSERT INTO librarian VALUES('Mary'); COMMIT; (explicit) Lu Wei

Transaction Configure certain aspects of the transaction SET TRANSACTION [READ ONLY|READ WRITE]| [ISOLATION LEVEL READ UNCOMMITTED| READ COMMITTED| REPEATABLE READ| SERIALIZABLE] Lu Wei

Advanced SQL Index View Transaction Integrity Constraints Access control Lu Wei

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

Integrity Constraints ALTER TABLE ADD CONSTRAINT constraintName FOREIGN KEY(columnName) REFERENCES tableName.columnName DEFERRABLE INITIALLY DEFERRED/IMMEDIATE Lu Wei

Advanced SQL Index View Transaction Integrity Constraints Access control Lu Wei

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

Access control Username and password Privileges Database administrator Lu Wei

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

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

Access control An application developer, for example, may have the following system privileges: CREATE SESSION CREATE TABLE CREATE VIEW CREATE PROCEDURE Lu Wei

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

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

Access control Object previliges Object privileges vary from object to object. An owner has all the privileges on the object. Lu Wei

Access control Object Privilege Table View Sequence Procedure ALTER Ö Ö DELETE Ö Ö EXECUTE Ö INDEX Ö INSERT Ö Ö REFERENCES Ö Ö SELECT Ö Ö Ö UPDATE Ö Ö Lu Wei

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

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

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

Access control GRANT specific privileges GRANT SELECT, UPDATE (salary) ON Staff TO Personnel, Director; Lu Wei

Access control GRANT specific privileges to PUBLIC GRANT SELECT ON Branch TO PUBLIC; Lu Wei

Access control REVOKE specific privileges from PUBLIC REVOKE SELECT ON Branch FROM PUBLIC; Lu Wei

Access control GRANT specific privileges from named user REVOKE ALL PRIVILEGES ON Staff FROM Director; Lu Wei

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

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

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