college of data control language - wordpress.com · 2017. 12. 2. · lecture11: transaction control...

12
Lecture11: Transaction Control Language Data Control Language 1 College Compu a Informati Science Informati Syste De Prepared by L. Nouf Almujally & Aisha AlArfaj IS220 : Database Fundamentals

Upload: others

Post on 22-Aug-2020

2 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

Lecture11: Transaction Control Language

Data Control Language

1

College ofComputer

andInformation

Sciences -Information

SystemsDept.

Prepared by L. Nouf Almujally & AishaAlArfaj

IS220 : Da t a ba se F unda m e nt a l s

Page 2: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

Transaction ControlA Transaction Control Language (TCL) is used to controltransactional processing in a databaseThe following commands used to control transactions:• COMMIT: to save the changes permanently in the database• Syntax

• ROLLBACK: to undo all changes of a transaction.• Syntax

• SAVEPOINT: creates points within groups of transactions in

which to ROLLBACK• Syntax

Lecture11

2

COMMIT;

ROLLBACK;

SAVEPOINT SAVEPOINT_NAME;

Page 3: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

Savepoint• The SAVEPOINT statement names and marks the current point in

the processing of a transaction• Example:• SQL> INSERT INTO AUTHOR VALUES ('A11l', 'john', 'garmany', '123-345-

4567', '1234 here st', 'denver', 'CO','90204', '9999');1 row created.

• SQL> savepoint in_author;Savepoint created.

• SQL> INSERT INTO BOOK_AUTHOR VALUES ('A111', 'B130', .20); 1 row created.

• SQL> savepoint in_book_author;Savepoint created.

• SQL> INSERT INTO BOOK VALUES ('B130', 'P002', 'easy oracle sql','miscellaneous', 9.95, 1000, 15, 0, '', to_date ('02-20-2005','MM-DD-YYYY'));1 row created.

• SQL> rollback to in_author;Rollback complete. 3

Page 4: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

Commit and Rollback

• If you want to make your update permanent, use COMMIT;• The COMMIT command is the command used to save changes

invoked by a transaction to the database.• The COMMIT statement erases any SAVEPOINTS you

marked since the last commit.• You can see the changes when you query the tables you

modified, but other users cannot see the changes until youCOMMIT the work.

• If you change your mind or need to correct a mistake, you canuse the ROLLBACK statement to roll back (undo) the changes.

• The ROLLBACK statement is the inverseof COMMIT statement. It undoes some or all database changesmade during the current transaction.

Lecture11

4

Page 5: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

Example

SQL> DELETE FROM CUSTOMER WHERE AGE = 25;1 rows deleted SQL> COMMIT; SQL> DELETE FROM CUSTOMER WHERE AGE = 30;1 rows deleted SQL> ROLLBACK;SQL> SELECT * FROM CUSTOMER;

5

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

Page 6: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

SQL> select * from customer; CUSTNO CUSTNAME CUSTST CUSTCITY AGE-------------- -------------- ---------- ---------- ---------- 1 C1 Olaya St Jeddah 20 2 C2 Mains St Riyadh 30 3 C3 Mains Rd Riyadh 25 4 C4 Mains Rd Dammam 5 C5 Mains Rd Riyadh SQL> delete from customer where custno='1';1 row deleted. SQL> savepoint a;Savepoint created. SQL> commit;Commit complete. SQL> rollback a;rollback a *ERROR at line 1:ORA-02181: invalid option to ROLLBACK WORK

Lecture11

6

The COMMIT statement erases any SAVEPOINTSyou marked since the last commit.

Savepoint a will be deleted

Page 7: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

Data ControlLanguage

7

Page 8: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

The Data Control Language (DCL)

The Data Control Language (DCL)This component of the SQL language is used to createprivileges to allow users access to, and manipulation of,the database.

Ø There are two main commands:1. GRANT : to grant a privilege to a user2. REVOKE : to remove a privilege from a user

Lecture11

8

Page 9: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

GRANT command• In order to log onto the database, create tables, and manipulate

data (select, insert, update and delete) in tables created byother users you must be given the appropriate privileges.

• The SQL command to grant a privilege on a table is: • Any combination of the above privileges is allowed.• You can issue this command on any tables that you have

created. • For example:GRANT SELECT ON employee TO NOURA;GRANT SELECT, UPDATE, DELETE ON employee TOJOHN;

9

GRANT SELECT, INSERT, UPDATE, DELETE ON tablename TOusername;

Page 10: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

REVOKE commandTo remove a privilege from a user use REVOKE • The SQL command to revoke a privilege on a table is: • For example:REVOKE SELECT ON employee FROM NOURA;REVOKE SELECT, UPDATE, DELETE ON employee FROMJOHN;

Lecture11

10

REVOKE SELECT, INSERT, UPDATE, DELETE ON tablename FROMusername;

Page 11: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

11

Page 12: College of Data Control Language - WordPress.com · 2017. 12. 2. · Lecture11: Transaction Control Language Data Control Language 1 College of Computer and Information Sciences -

References• “Database Systems: A Practical Approach to Design,

Implementation and Management.” Thomas Connolly,Carolyn Begg. 5th Edition, Addison-Wesley, 2009.

Lecture11

12