college of data control language - wordpress.com · 2017. 12. 2. · lecture11: transaction control...
TRANSCRIPT
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
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;
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
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
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
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
Data ControlLanguage
7
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
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;
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;
11
References• “Database Systems: A Practical Approach to Design,
Implementation and Management.” Thomas Connolly,Carolyn Begg. 5th Edition, Addison-Wesley, 2009.
Lecture11
12