SQL Tutorial - Commit: Insert Into Values
SQL Tutorial - Commit: Insert Into Values
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
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;
-- 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’
Now for some reason we wanted to revert the DML operation on the table. (Remember we have
not done COMMIT).
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.
Here we can see that DML operation is Roll backed (Sal from 7500 to 6500).
Let’s assume we have empty EMP table and below is the example with savepoint at different
levels
SQL>SAVEPOINT s1;
SQL> INSERT INTO emp VALUES (7745,'MARK','MGR', NULL,'14-JUN-1970', 2000, NULL, 30);
SQL> INSERT INTO emp VALUES (7746,'SCOTT','CLERK', 7902,'10-JUN-1980', 900, NULL, 40);
SQL> INSERT INTO emp VALUES (7747,'LUKE','CONS', 7999,'17-JAN-1983', 1500, NULL, 10);
Before rollback
SQL>SELECT * FROM emp;