0% found this document useful (0 votes)
19 views13 pages

1b. Basic and Advanced SQL (DDL)

The document discusses integrity constraints in SQL including required data, domain constraints, entity integrity, referential integrity, and general constraints. It also covers data definition statements like CREATE SCHEMA, CREATE DOMAIN, CREATE TABLE, CREATE VIEW, and DROP statements. Key SQL statements for defining database objects and enforcing integrity are explained.

Uploaded by

The Real Stuff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views13 pages

1b. Basic and Advanced SQL (DDL)

The document discusses integrity constraints in SQL including required data, domain constraints, entity integrity, referential integrity, and general constraints. It also covers data definition statements like CREATE SCHEMA, CREATE DOMAIN, CREATE TABLE, CREATE VIEW, and DROP statements. Key SQL statements for defining database objects and enforcing integrity are explained.

Uploaded by

The Real Stuff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Advanced Database Mgt.

Systems

ISO SQL Data Types

Advanced SQL – Data Definition

Pearson Education © 2014 Pearson Education © 2014 2

Integrity Enhancement Feature Integrity Enhancement Feature


Consider five types of integrity constraints: Required Data
required data position VARCHAR(10) NOT NULL
domain constraints
entity integrity Domain Constraints
referential integrity (a) CHECK
general constraints. sex CHAR NOT NULL
CHECK (sex IN (‘M’, ‘F’))

Pearson Education © 2014 3 Pearson Education © 2014 4

Integrity Enhancement Feature Integrity Enhancement Feature


(b) CREATE DOMAIN
searchCondition can involve a table lookup:
CREATE DOMAIN DomainName [AS] dataType
[DEFAULT defaultOption] CREATE DOMAIN BranchNo AS CHAR(4)
[CHECK (searchCondition)] CHECK (VALUE IN (SELECT branchNo
For example: FROM Branch));

CREATE DOMAIN SexType AS CHAR Domains can be removed using DROP


CHECK (VALUE IN (‘M’, ‘F’)); DOMAIN:
sex SexType NOT NULL
DROP DOMAIN DomainName
[RESTRICT | CASCADE]

Pearson Education © 2014 5 Pearson Education © 2014 6

Basic & Advanced SQL - DDL (Obuhuma J) 1


Advanced Database Mgt. Systems

IEF - Entity Integrity IEF - Referential Integrity


Primary key of a table must contain a unique, FK is column or set of columns that links each
non-null value for each row. row in child table containing foreign FK to row
ISO standard supports FOREIGN KEY clause in of parent table containing matching PK.
CREATE and ALTER TABLE statements: Referential integrity means that, if FK contains
PRIMARY KEY(staffNo) a value, that value must refer to existing row
PRIMARY KEY(clientNo, propertyNo) in parent table.
ISO standard supports definition of FKs with
Can only have one PRIMARY KEY clause per
FOREIGN KEY clause in CREATE and ALTER
table. Can still ensure uniqueness for
TABLE:
alternate keys using UNIQUE:
FOREIGN KEY(branchNo) REFERENCES Branch
UNIQUE(telNo)
Pearson Education © 2014 7 Pearson Education © 2014 8

IEF - Referential Integrity IEF - Referential Integrity


Any INSERT/UPDATE attempting to create FK CASCADE: Delete row from parent and delete
value in child table without matching CK value matching rows in child, and so on in cascading
in parent is rejected. manner.
Action taken attempting to update/delete a SET NULL: Delete row from parent and set FK
CK value in parent table with matching rows column(s) in child to NULL. Only valid if FK
in child is dependent on referential action columns are NOT NULL.
specified using ON UPDATE and ON DELETE SET DEFAULT: Delete row from parent and set
subclauses: each component of FK in child to specified
default. Only valid if DEFAULT specified for FK
CASCADE - SET NULL columns.
SET DEFAULT - NO ACTION
NO ACTION: Reject delete from parent. Default.
Pearson Education © 2014 9 Pearson Education © 2014 10

IEF - Referential Integrity IEF - General Constraints


FOREIGN KEY (staffNo) REFERENCES Staff Could use CHECK/UNIQUE in CREATE and
ON DELETE SET NULL ALTER TABLE.
FOREIGN KEY (ownerNo) REFERENCES Owner Similar to the CHECK clause, also have:
ON UPDATE CASCADE CREATE ASSERTION AssertionName
CHECK (searchCondition)

Pearson Education © 2014 11 Pearson Education © 2014 12

Basic & Advanced SQL - DDL (Obuhuma J) 2


Advanced Database Mgt. Systems

IEF - General Constraints Data Definition


CREATE ASSERTION StaffNotHandlingTooMuch SQL DDL allows database objects such as
CHECK (NOT EXISTS (SELECT staffNo schemas, domains, tables, views, and indexes
FROM PropertyForRent to be created and destroyed.
GROUP BY staffNo Main SQL DDL statements are:
HAVING COUNT(*) > 100)) CREATE SCHEMA DROP SCHEMA
CREATE/ALTER DOMAIN DROP DOMAIN
CREATE/ALTER TABLE DROP TABLE
CREATE VIEW DROP VIEW

Many DBMSs also provide:


CREATE INDEX DROP INDEX

Pearson Education © 2014 13 Pearson Education © 2014 14

Data Definition CREATE SCHEMA


Relations and other database objects exist in CREATE SCHEMA [Name |
an environment. AUTHORIZATION CreatorId ]
Each environment contains one or more DROP SCHEMA Name [RESTRICT | CASCADE ]
catalogs, and each catalog consists of set of • With RESTRICT (default), schema must be
schemas. empty or operation fails.
Schema is named collection of related • With CASCADE, operation cascades to drop
database objects. all objects associated with schema in order
Objects in a schema can be tables, views, defined above. If any of these operations
domains, assertions, collations, translations, fail, DROP SCHEMA fails.
and character sets. All have same owner.

Pearson Education © 2014 15 Pearson Education © 2014 16

CREATE TABLE CREATE TABLE


CREATE TABLE TableName Creates a table with one or more columns of
{(colName dataType [NOT NULL] [UNIQUE] the specified dataType.
[DEFAULT defaultOption]
With NOT NULL, system rejects any attempt to
[CHECK searchCondition] [,...]}
insert a null in the column.
[PRIMARY KEY (listOfColumns),]
{[UNIQUE (listOfColumns),] […,]}
Can specify a DEFAULT value for the column.
{[FOREIGN KEY (listOfFKColumns) Primary keys should always be specified as
REFERENCES ParentTableName [(listOfCKColumns)], NOT NULL.
[ON UPDATE referentialAction] FOREIGN KEY clause specifies FK along with
[ON DELETE referentialAction ]] [,…]} the referential action.
{[CHECK (searchCondition)] [,…] })

Pearson Education © 2014 17 Pearson Education © 2014 18

Basic & Advanced SQL - DDL (Obuhuma J) 3


Advanced Database Mgt. Systems

Example 7.1 - CREATE TABLE Example 7.1 - CREATE TABLE


CREATE DOMAIN OwnerNumber AS VARCHAR(5) CREATE TABLE PropertyForRent (
CHECK (VALUE IN (SELECT ownerNo FROM PrivateOwner)); propertyNo PNumber NOT NULL, ….
CREATE DOMAIN StaffNumber AS VARCHAR(5) rooms PRooms NOT NULL DEFAULT 4,
CHECK (VALUE IN (SELECT staffNo FROM Staff)); rent PRent NOT NULL, DEFAULT 600,
CREATE DOMAIN PNumber AS VARCHAR(5); ownerNo OwnerNumber NOT NULL,
CREATE DOMAIN PRooms AS SMALLINT; staffNo StaffNumber
CHECK(VALUE BETWEEN 1 AND 15); Constraint StaffNotHandlingTooMuch ….
CREATE DOMAIN PRent AS DECIMAL(6,2) branchNo BranchNumber NOT NULL,
CHECK(VALUE BETWEEN 0 AND 9999.99); PRIMARY KEY (propertyNo),
FOREIGN KEY (staffNo) REFERENCES Staff
ON DELETE SET NULL ON UPDATE CASCADE ….);

Pearson Education © 2014 19 Pearson Education © 2014 20

ALTER TABLE Example 7.2(a) - ALTER TABLE


Add a new column to a table. Change Staff table by removing default of
Drop a column from a table. ‘Assistant’ for position column and setting
Add a new table constraint. default for sex column to female (‘F’).
Drop a table constraint. ALTER TABLE Staff
Set a default for a column. ALTER position DROP DEFAULT;
Drop a default for a column. ALTER TABLE Staff
ALTER sex SET DEFAULT ‘F’;

Pearson Education © 2014 21 Pearson Education © 2014 22

Example 7.2(b) - ALTER TABLE DROP TABLE


Remove constraint from PropertyForRent that DROP TABLE TableName [RESTRICT | CASCADE]
staff are not allowed to handle more than 100
e.g. DROP TABLE PropertyForRent;
properties at a time. Add new column to
Client table. Removes named table and all rows within it.
With RESTRICT, if any other objects depend for
ALTER TABLE PropertyForRent their existence on continued existence of this
DROP CONSTRAINT StaffNotHandlingTooMuch; table, SQL does not allow request.
ALTER TABLE Client With CASCADE, SQL drops all dependent
ADD prefNoRooms PRooms; objects (and objects dependent on these
objects).

Pearson Education © 2014 23 Pearson Education © 2014 24

Basic & Advanced SQL - DDL (Obuhuma J) 4


Advanced Database Mgt. 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.

Pearson Education © 2014 25 Pearson Education © 2014 26

SQL - CREATE VIEW SQL - CREATE VIEW


CREATE VIEW ViewName [ (newColumnName [,...]) ] List must be specified if there is any ambiguity
AS subselect in a column name.
[WITH [CASCADED | LOCAL] CHECK OPTION] The subselect is known as the defining query.
• Can assign a name to each column in view. WITH CHECK OPTION ensures that if a row
fails to satisfy WHERE clause of defining
• If list of column names is specified, it must query, it is not added to underlying base
have same number of items as number of table.
columns produced by subselect.
Need SELECT privilege on all tables referenced
• If omitted, each column takes name of in subselect and USAGE privilege on any
corresponding column in subselect. domains used in referenced columns.

Pearson Education © 2014 27 Pearson Education © 2014 28

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’;

Pearson Education © 2014 29 30


Pearson Education © 2014

Basic & Advanced SQL - DDL (Obuhuma J) 5


Advanced Database Mgt. Systems

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;

Pearson Education © 2014 31 Pearson Education © 2014 32

SQL - DROP VIEW SQL - DROP VIEW


DROP VIEW ViewName [RESTRICT | CASCADE] With CASCADE, all related dependent objects
are deleted; i.e. any views defined on view
Causes definition of view to be deleted from being dropped.
database.
With RESTRICT (default), if any other objects
For example: depend for their existence on continued
DROP VIEW Manager3Staff; existence of view being dropped, command is
rejected.

Pearson Education © 2014 33 Pearson Education © 2014 34

View Resolution View Resolution


Count number of properties managed by each (a) View column names in SELECT list are
member at branch B003. translated into their corresponding column
names in the defining query:
SELECT staffNo, cnt SELECT s.staffNo As staffNo, COUNT(*) As cnt
FROM StaffPropCnt
(b) View names in FROM are replaced with
WHERE branchNo = ‘B003’ corresponding FROM lists of defining query:
ORDER BY staffNo;
FROM Staff s, PropertyForRent p

Pearson Education © 2014 35 Pearson Education © 2014 36

Basic & Advanced SQL - DDL (Obuhuma J) 6


Advanced Database Mgt. Systems

View Resolution View Resolution


(c) WHERE from user query is combined with
(f) Final merged query is now executed to
WHERE of defining query using AND:
produce the result:
WHERE s.staffNo = p.staffNo AND branchNo = ‘B003’
SELECT s.staffNo AS staffNo, COUNT(*) AS cnt
(d) GROUP BY and HAVING clauses copied from FROM Staff s, PropertyForRent p
defining query: WHERE s.staffNo = p.staffNo AND
GROUP BY s.branchNo, s.staffNo branchNo = ‘B003’
(e) ORDER BY copied from query with view column GROUP BY s.branchNo, s.staffNo
name translated into defining query column ORDER BY s.staffNo;
name
ORDER BY s.staffNo
Pearson Education © 2014 37 Pearson Education © 2014 38

Restrictions on Views Restrictions on Views


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

Pearson Education © 2014 39 Pearson Education © 2014 40

Restrictions on Views View Updatability


(b) Grouped view may never be joined with a All updates to base table reflected in all views
base table or a view. that encompass base table.
Similarly, may expect that if view is updated
• For example, StaffPropCnt view is a grouped then base table(s) will reflect change.
view, so any attempt to join this view with
another table or view fails.

Pearson Education © 2014 41 Pearson Education © 2014 42

Basic & Advanced SQL - DDL (Obuhuma J) 7


Advanced Database Mgt. Systems

View Updatability View Updatability


However, consider again view StaffPropCnt. If change definition of view and replace count
If we tried to insert record showing that at with actual property numbers:
branch B003, SG5 manages 2 properties: CREATE VIEW StaffPropList (branchNo,
INSERT INTO StaffPropCnt staffNo, propertyNo)
VALUES (‘B003’, ‘SG5’, 2); AS SELECT s.branchNo, s.staffNo, p.propertyNo
FROM Staff s, PropertyForRent p
• Have to insert 2 records into PropertyForRent WHERE s.staffNo = p.staffNo;
showing which properties SG5 manages.
However, do not know which properties they
are; i.e. do not know primary keys!

Pearson Education © 2014 43 Pearson Education © 2014 44

View Updatability View Updatability


Now try to insert the record: ISO specifies that a view is updatable if and
only if:
INSERT INTO StaffPropList
- DISTINCT is not specified.
VALUES (‘B003’, ‘SG5’, ‘PG19’); - Every element in SELECT list of defining query is a column
name and no column appears more than once.
• Still problem, because in PropertyForRent all - FROM clause specifies only one table, excluding any views
columns except postcode/staffNo are not based on a join, union, intersection or difference.
allowed nulls. - No nested SELECT referencing outer table.
• However, have no way of giving remaining - No GROUP BY or HAVING clause.
- Also, every row added through view must not violate
non-null columns values. integrity constraints of base table.

Pearson Education © 2014 45 Pearson Education © 2014 46

Updatable View WITH CHECK OPTION


For view to be updatable, DBMS must be Rows exist in a view because they satisfy
able to trace any row or column back to its WHERE condition of defining query.
row or column in the source table. If a row changes and no longer satisfies
condition, it disappears from the view.
New rows appear within view when
insert/update on view cause them to satisfy
WHERE condition.
Rows that enter or leave a view are called
migrating rows.
WITH CHECK OPTION prohibits a row
migrating out of the view.
Pearson Education © 2014 47 Pearson Education © 2014 48

Basic & Advanced SQL - DDL (Obuhuma J) 8


Advanced Database Mgt. Systems

WITH CHECK OPTION Example 7.6 - WITH CHECK OPTION


LOCAL/CASCADED apply to view hierarchies. CREATE VIEW Manager3Staff
With LOCAL, any row insert/update on view AS SELECT *
and any view directly or indirectly defined on FROM Staff
this view must not cause row to disappear WHERE branchNo = ‘B003’
from view unless row also disappears from WITH CHECK OPTION;
derived view/table. Cannot update branch number of row B003 to
With CASCADED (default), any row insert/ B002 as this would cause row to migrate from
update on this view and on any view directly view.
or indirectly defined on this view must not Also cannot insert a row into view with a
cause row to disappear from the view. branch number that does not equal B003.

Pearson Education © 2014 49 Pearson Education © 2014 50

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

Example 7.6 - WITH CHECK OPTION Advantages of Views


If HighSalary had specified WITH CASCADED Data independence
CHECK OPTION, setting salary to 9500 or 8000 Currency
would be rejected because row would Improved security
disappear from HighSalary.
Reduced complexity
To prevent anomalies like this, each view
should be created using WITH CASCADED Convenience
CHECK OPTION. Customization
Data integrity

Pearson Education © 2014 53 Pearson Education © 2014 54

Basic & Advanced SQL - DDL (Obuhuma J) 9


Advanced Database Mgt. Systems

Disadvantages of Views View Materialization


Update restriction View resolution mechanism may be slow,
Structure restriction particularly if view is accessed frequently.
Performance View materialization stores view as
temporary table when view is first queried.
Thereafter, queries based on materialized
view can be faster than recomputing view
each time.
Difficulty is maintaining the currency of view
while base tables(s) are being updated.

Pearson Education © 2014 55 Pearson Education © 2014 56

View Maintenance View Materialization


View maintenance aims to apply only those If insert row into PropertyForRent with rent 400 then
changes necessary to keep view current. view would be unchanged.
Consider following view: If insert row for property PG24 at branch B003 with
staffNo = SG19 and rent = 550, then row would appear
CREATE VIEW StaffPropRent(staffNo) in materialized view.
ASSELECT DISTINCT staffNo If insert row for property PG54 at branch B003 with
FROM PropertyForRent staffNo = SG37 and rent = 450, then no new row would
WHERE branchNo = ‘B003’ AND need to be added to materialized view.
rent > 400; If delete property PG24, row should be deleted from
materialized view.
If delete property PG54, then row for PG37 should not
be deleted (because of existing property PG21).

Pearson Education © 2014 57 Pearson Education © 2014 58

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

Basic & Advanced SQL - DDL (Obuhuma J) 10


Advanced Database Mgt. Systems

Transactions Immediate and Deferred Integrity Constraints


New transaction starts with next transaction- Do not always want constraints to be checked
initiating statement. immediately, but instead at transaction
SQL transactions cannot be nested. commit.
SET TRANSACTION configures transaction: Constraint may be defined as INITIALLY
IMMEDIATE or INITIALLY DEFERRED, indicating
SET TRANSACTION mode the constraint assumes at start of each
[READ ONLY | READ WRITE] | transaction.
[ISOLATION LEVEL READ UNCOMMITTED |
In former case, also possible to specify
READ COMMITTED|REPEATABLE READ |SERIALIZABLE ]
whether mode can be changed subsequently
using qualifier [NOT] DEFERRABLE.
Default mode is INITIALLY IMMEDIATE.
Pearson Education © 2014 61 Pearson Education © 2014 62

Immediate and Deferred Integrity Constraints Access Control - Authorization Identifiers and Ownership

SET CONSTRAINTS statement used to set Authorization identifier is normal SQL


mode for specified constraints for current identifier used to establish identity of a user.
transaction: Usually has an associated password.
Used to determine which objects user may
SET CONSTRAINTS reference and what operations may be
{ALL | constraintName [, . . . ]} performed on those objects.
{DEFERRED ¦ IMMEDIATE} Each object created in SQL has an owner, as
defined in AUTHORIZATION clause of schema
to which object belongs.
Owner is only person who may know about it.

Pearson Education © 2014 63 Pearson Education © 2014 64

Creating a User Account Creating a User Account


We first need to have a user account existing. Having created a user account, we need to
The following is the syntax for creating a user map it to an existing database. The following is
account: the syntax for creating a user account:

MS-SQL Server MS-SQL Server


CREATE LOGIN username CREATE USER username
WITH PASSWORD=‘password’; FROM LOGIN username;

MySql
CREATE USER ‘username@localhost’
IDENTIFIED BY ‘password’;
Pearson Education © 2014 65 Pearson Education © 2014 66

Basic & Advanced SQL - DDL (Obuhuma J) 11


Advanced Database Mgt. Systems

Removing a User Account Privileges


To remove an account we use the DROP Actions user permitted to carry out on given
statement. The following is the syntax for base table or view:
droping a user account: SELECT Retrieve data from a table.
INSERT Insert new rows into a table.
MS-SQL Server UPDATE Modify rows of data in a table.
DROP USE username;
DELETE Delete rows of data from a table.
MySql
REFERENCES Reference columns of named
table in integrity constraints.
DROP USE username;
USAGE Use domains, collations, character
sets, and translations.
Pearson Education © 2014 67 Pearson Education © 2014 68

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.

Pearson Education © 2014 69 Pearson Education © 2014 70

GRANT Example 7.7/8 - GRANT


PUBLIC allows access to be granted to all Give Manager full privileges to Staff table.
present and future authorized users. GRANT ALL PRIVILEGES
ObjectName can be a base table, view, ON Staff
domain, character set, collation or TO Manager WITH GRANT OPTION;
translation.
WITH GRANT OPTION allows privileges to be Give users Personnel and Director SELECT and
passed on. UPDATE on column salary of Staff.
GRANT SELECT, UPDATE (salary)
ON Staff
TO Personnel, Director;

Pearson Education © 2014 71 Pearson Education © 2014 72

Basic & Advanced SQL - DDL (Obuhuma J) 12


Advanced Database Mgt. Systems

Example 7.9 - GRANT Specific Privileges to PUBLIC REVOKE


Give all users SELECT on Branch table. REVOKE takes away privileges granted with
GRANT.
GRANT SELECT
REVOKE [GRANT OPTION FOR]
ON Branch
{PrivilegeList | ALL PRIVILEGES}
TO PUBLIC;
ON ObjectName
FROM {AuthorizationIdList | PUBLIC}
[RESTRICT | CASCADE]
• ALL PRIVILEGES refers to all privileges granted
to a user by user revoking privileges.

Pearson Education © 2014 73 Pearson Education © 2014 74

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.

Pearson Education © 2014 75 Pearson Education © 2014 76

Example 7.10/11 - REVOKE Specific Privileges

Revoke privilege SELECT on Branch table from


all users.
REVOKE SELECT
ON Branch
FROM PUBLIC;
Revoke all privileges given to Director on Staff
table.
REVOKE ALL PRIVILEGES
ON Staff
FROM Director;
Pearson Education © 2014 77

Basic & Advanced SQL - DDL (Obuhuma J) 13

You might also like