Nothing Special   »   [go: up one dir, main page]

SQL Tutorial - Commit: Insert Into Values

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

 Chapter 6: TCL

SQL Tutorial - Commit


 COMMIT is a part of Transaction Control Language (TCL) that allows us to commit the database
transactions i.e. to save the work permanently in the database.
 Commit plays a vital role when we work on DML operations because we need to explicitly save it.

Commit has two types


1. Explicit Commit – User have to Commit explicitly when ever any DML operations like insert,
update, delete are performed on the database table.

Example:-For example we are inserting record into emp table Shown below
SQL> INSERT INTO emp VALUES (7125,’SCOTT’,'ANALYST', 7902,'22-DEC-1982', 15000, 1000, 20);

Output:

1 ROW created

-- To save the record into the database execute commit.

SQL> Commit;

Output:

Commit complete
For some reason we did not commit the record after DML operation then ‘if system crashes’ or ‘if
session is ended’ – data will be lost from cache. i.e. even we inserted the record we will not be
able to see it in the database table.

2. Implicit Commit – system will automatically execute commit whenever we perform any DDL
Commands like create, alter, drop.
That is the reason if we perform any DDL command we will not be able to revert back the
operation (like using ROLLBACK command).

Example:
SQL>TRUNCATE TABLE emp;

-- Data will be permanently deleted from EMP table. (AUTO COMMIT)


SQL Tutorial - Rollback
 ROLLBACK is just opposite to COMMIT.
 ROLLBACK will undo the DML operation that we perform on database which is not committed yet.

Example: – First we are updating the salary of employee record, ename=’JOHN’


SQL> UPDATE emp SET sal=sal+1000 WHERE ename='JOHN';

-- 1 row updated
After update and inside the same session if we retrieve the record from database using below
query we can see the updated salary i.e ‘7500’

SQL> SELECT *FROM emp WHERE ename='JOHN';

Now for some reason we wanted to revert the DML operation on the table. (Remember we have
not done COMMIT).

- Run ROLLBACK command.

SQL> ROLLBACK;

Output :

ROLLBACK complete.
We need to explicitly ROLLBACK the transaction if we wanted to revert any DML operation that
occurred on the database (Which is not committed yet).

Now let’s retrieve the same record i.e. ename=’JOHN’ to check the salary.

SQL> SELECT *FROM emp WHERE ename='JOHN';

Here we can see that DML operation is Roll backed (Sal from 7500 to 6500).

SQL Tutorial - Savepoint


 Savepoint is a part of Transaction Control Language (TCL).
 We use save points to rollback portions of our current set of transactions.
 These save points are effectively used when we write huge programming code for better control
on the program.

Let’s assume we have empty EMP table and below is the example with savepoint at different
levels

Example:- Inserting records into EMP table

SQL>SAVEPOINT s1;

SQL>INSERT INTO emp VALUES (7744,'SMITH','CLERK', 7902,'10-JUN-1980', 800, NULL, 20);

SQL> SAVEPOINT s2;

SQL> INSERT INTO emp VALUES (7745,'MARK','MGR', NULL,'14-JUN-1970', 2000, NULL, 30);

SQL> SAVEPOINT s3;

SQL> INSERT INTO emp VALUES (7746,'SCOTT','CLERK', 7902,'10-JUN-1980', 900, NULL, 40);

SQL> SAVEPOINT s4;

SQL> INSERT INTO emp VALUES (7747,'LUKE','CONS', 7999,'17-JAN-1983', 1500, NULL, 10);

Before rollback
SQL>SELECT * FROM emp;

ROLLBACK with SAVEPOINT


SQL> ROLLBACK TO s3;
This will rollback last two records. i.e. Will rollback all the operations occurred after savepoint s3.

SQL> SELECT *FROM emp;


– first 2 records are fetched.

Now lets rollback further, i.e. till savepoint s2

SQL> ROLLBACK TO s2;


This will rollback all operations occurred after savepoint s2.

SQL> SELECT *FROM emp;


– only first record is fetched.

You might also like