IS220 : Database Fundamentals Lecture11: Transaction Control Language Data Control Language College Compu a Informati Science Informati Syste De Prepared by L. Nouf Almujally & Aisha AlArfaj 1
Transaction Control A Transaction Control Language (TCL) is used to control transactional processing in a database The following commands used to control transactions: COMMIT: to save the changes permanently in the database Syntax COMMIT; ROLLBACK: to undo all changes of a transaction. Syntax ROLLBACK; Lecture1 SAVEPOINT: creates points within groups of transactions in which to ROLLBACK Syntax SAVEPOINT SAVEPOINT_NAME; 2
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 you COMMIT the work. If you change your mind or need to correct a mistake, you can use the ROLLBACK statement to roll back (undo) the changes. The ROLLBACK statement is the inverse of COMMIT statement. It undoes some or all database changes made during the current transaction. Lecture1 4
custno custname custst custcity age 1 C1 Olaya St Jeddah 20 Example 2 C2 Mains St Riyadh 30 3 C3 Mains Rd Riyadh 25 SQL> DELETE FROM CUSTOMER WHERE AGE = 25; 1 rows deleted SQL> COMMIT; 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> DELETE FROM CUSTOMER WHERE AGE = 30; 1 rows deleted custno custname custst custcity age 1 C1 Olaya St Jeddah 20 4 C4 Mains Rd Dammam SQL> ROLLBACK; SQL> SELECT * FROM CUSTOMER; 5 C5 Mains Rd Riyadh custno custname custst custcity age 1 C1 Olaya St Jeddah 20 2 C2 Mains St Riyadh 30 5 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. Savepoint a will be deleted SQL> commit; Commit complete. The COMMIT statement erases any SAVEPOINTS you marked since the last commit. Lecture1 SQL> rollback a; rollback a * ERROR at line 1: ORA-02181: invalid option to ROLLBACK WORK 6
Data Control Language 7
The Data Control Language (DCL) The Data Control Language (DCL) This component of the SQL language is used to create privileges to allow users access to, and manipulation of, the database. Ø There are two main commands: 1. GRANT : to grant a privilege to a user 2. REVOKE : to remove a privilege from a user Lecture1 8
GRANT command In order to log onto the database, create tables, and manipulate data (select, insert, update and delete) in tables created by other users you must be given the appropriate privileges. The SQL command to grant a privilege on a table is: GRANT SELECT, INSERT, UPDATE, DELETE ON tablename TO username; 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 TO JOHN; 9
REVOKE command To remove a privilege from a user use REVOKE The SQL command to revoke a privilege on a table is: REVOKE SELECT, INSERT, UPDATE, DELETE ON tablename FROM username; For example: REVOKE SELECT ON employee FROM NOURA; REVOKE SELECT, UPDATE, DELETE ON employee FROM JOHN; Lecture1 10
11
References Database Systems: A Practical Approach to Design, Implementation and Management. Thomas Connolly, Carolyn Begg. 5th Edition, Addison-Wesley, 2009. Lecture1 12