Based on the SQLD exam syllabusReport an error
COMMIT makes changes permanent, and ROLLBACK cancels all changes since the last COMMIT. ROLLBACK TO savepoint cancels only what came after that savepoint.ROLLBACK TO a savepoint, any savepoints created after it disappear. A savepoint recreated with the same name overwrites the earlier one.A transfer that takes 100,000 won out of account A and puts 100,000 won into account B is two UPDATE statements. If only the first statement takes effect and the second fails, the money vanishes. The two statements must both take effect or both be cancelled.
| Property | Name | Meaning | Transfer example |
|---|---|---|---|
| Atomicity | Atomicity | Everything takes effect, or nothing does | It never happens that the withdrawal succeeds but the deposit does not |
| Consistency | Consistency | Data obeys the rules (constraints) before and after execution | The sum of the two account balances stays the same |
| Isolation | Isolation | Other transactions cannot see the intermediate results of a running transaction | The balance at the moment only the withdrawal has happened is invisible to others |
| Durability | Durability | Committed results survive failures | After COMMIT, the transfer result remains even if the server shuts down |
COMMIT; -- make all changes so far permanent
ROLLBACK; -- cancel all changes since the last COMMIT
SAVEPOINT name; -- create an intermediate savepoint
ROLLBACK TO [SAVEPOINT] name; -- cancel only the changes after the savepointCREATE TABLE T_DEPT AS SELECT * FROM DEPT;
INSERT INTO T_DEPT VALUES (50, 'EDUCATION', 'SEOUL');
COMMIT;
DELETE FROM T_DEPT WHERE DEPTNO = 10;
ROLLBACK;
SELECT DEPTNO, DNAME FROM T_DEPT ORDER BY DEPTNO;
DROP TABLE T_DEPT PURGE;Table created.
1 row created.
Commit complete.
1 row deleted.
Rollback complete.
DEPTNO DNAME
---------- --------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
50 EDUCATION
5 rows selected.
Table dropped.Commit complete. and Rollback complete.| Point in time | Session that made the change | Other sessions | Locks |
|---|---|---|---|
| Right after DML (before COMMIT) | Sees the changed values | Sees the values from before the change | Held on the changed rows |
| After COMMIT | Changed values | Changed values | Released |
| After ROLLBACK | Original values | Original values | Released |
ROLLBACK cancels everything since the last COMMIT. To cancel only the latter part of a long transaction, set savepoints along the way.
CREATE TABLE EMP_T AS SELECT EMPNO, ENAME, SAL FROM EMP WHERE DEPTNO = 10;
UPDATE EMP_T SET SAL = 1000 WHERE ENAME = 'CLARK';
SAVEPOINT SP1;
UPDATE EMP_T SET SAL = 2000 WHERE ENAME = 'KING';
SAVEPOINT SP2;
DELETE FROM EMP_T WHERE ENAME = 'MILLER';
ROLLBACK TO SP2;
SELECT ENAME, SAL FROM EMP_T ORDER BY ENAME;
ROLLBACK TO SAVEPOINT SP1;
SELECT ENAME, SAL FROM EMP_T ORDER BY ENAME;
ROLLBACK TO SP2;
ROLLBACK;
SELECT ENAME, SAL FROM EMP_T ORDER BY ENAME;
DROP TABLE EMP_T PURGE;Table created.
1 row updated.
Savepoint created.
1 row updated.
Savepoint created.
1 row deleted.
Rollback complete.
ENAME SAL
---------- ----------
CLARK 1000
KING 2000
MILLER 1300
3 rows selected.
Rollback complete.
ENAME SAL
---------- ----------
CLARK 1000
KING 5000
MILLER 1300
3 rows selected.
ROLLBACK TO SP2
*
ERROR at line 1:
ORA-01086: savepoint 'SP2' never established in this session or is invalid
Rollback complete.
ENAME SAL
---------- ----------
CLARK 2450
KING 5000
MILLER 1300
3 rows selected.
Table dropped.Here it is in chronological order.
CTAS ─ CLARK=1000 ─ [SP1] ─ KING=2000 ─ [SP2] ─ delete MILLER
◀── ROLLBACK TO SP2 : MILLER restored
◀────────────────────── ROLLBACK TO SP1 : KING restored, SP2 gone
◀──────────────────────────────────────────── ROLLBACK : CLARK restoredROLLBACK TO SP2 cancelled only the DELETE that came after SP2. The changes to CLARK and KING remain.ROLLBACK TO SP1, trying to go back to SP2 raised ORA-01086, because SP2, created after SP1, had already disappeared.ROLLBACK TO does not end the transaction. The final ROLLBACK cancelled everything, including CLARK's change.CREATE TABLE T_DEPT AS SELECT * FROM DEPT;
SAVEPOINT A;
DELETE FROM T_DEPT WHERE DEPTNO = 10;
SAVEPOINT A;
DELETE FROM T_DEPT WHERE DEPTNO = 20;
ROLLBACK TO A;
SELECT DEPTNO FROM T_DEPT ORDER BY DEPTNO;
ROLLBACK;
SELECT COUNT(*) FROM T_DEPT;
ROLLBACK TO A;
DROP TABLE T_DEPT PURGE;Table created.
Savepoint created.
1 row deleted.
Savepoint created.
1 row deleted.
Rollback complete.
DEPTNO
----------
20
30
40
3 rows selected.
Rollback complete.
COUNT(*)
----------
4
1 row selected.
ROLLBACK TO A
*
ERROR at line 1:
ORA-01086: savepoint 'A' never established in this session or is invalid
Table dropped.SAVEPOINT A replaces the first A. So ROLLBACK TO A restored only department 20.ROLLBACK TO A gives ORA-01086.⚠️ Trap: With several savepoints,
ROLLBACK TOgoes back to the savepoint whose name you wrote, not to the most recent savepoint. Savepoints and changes after it are all gone, and changes before it remain.
Oracle COMMITs any open transaction before executing DDL, and COMMITs again when the DDL finishes.
CREATE TABLE T_DEPT AS SELECT * FROM DEPT;
INSERT INTO T_DEPT VALUES (50, 'EDUCATION', 'SEOUL');
CREATE TABLE T_LOG (MSG VARCHAR2(20));
ROLLBACK;
SELECT COUNT(*) FROM T_DEPT;
DROP TABLE T_LOG PURGE;
DROP TABLE T_DEPT PURGE;Table created.
1 row created.
Table created.
Rollback complete.
COUNT(*)
----------
5
1 row selected.
Table dropped.
Table dropped.CREATE TABLE T_LOG committed it along with itself. Even after ROLLBACK, there are 5 rows.| When it is automatically COMMITted | When it is automatically ROLLed BACK |
|---|---|
| Running DDL (CREATE, ALTER, DROP, TRUNCATE, RENAME) | When the session ends abnormally |
| Running DCL (GRANT, REVOKE) | When the DB goes down due to a failure (uncommitted changes) |
| Exiting SQL*Plus normally with EXIT |
CREATE TABLE T_DEPT AS SELECT * FROM DEPT;
INSERT INTO T_DEPT VALUES (50, 'EDUCATION', 'SEOUL');
INSERT INTO T_DEPT VALUES (60, 'TOO LONG DEPARTMENT NAME', 'SEOUL');
COMMIT;
SELECT COUNT(*) FROM T_DEPT;
DROP TABLE T_DEPT PURGE;Table created.
1 row created.
INSERT INTO T_DEPT VALUES (60, 'TOO LONG DEPARTMENT NAME', 'SEOUL')
*
ERROR at line 1:
ORA-12899: value too large for column "SCOTT"."T_DEPT"."DNAME" (actual: 24, maximum: 14)
Commit complete.
COUNT(*)
----------
5
1 row selected.
Table dropped.| Aspect | Oracle | SQL Server |
|---|---|---|
| Default mode | Requires an explicit COMMIT | AUTO COMMIT. Each statement is committed immediately |
| Starting a transaction | Starts automatically when the first DML runs | Stated explicitly with BEGIN TRAN |
| Savepoints | SAVEPOINT name / ROLLBACK TO name | SAVE TRAN name / ROLLBACK TRAN name |
| DDL and transactions | Implicit COMMIT. Cannot be undone | DDL inside a transaction can also be rolled back |
To group several statements into one in SQL Server, write it like this.
BEGIN TRAN;
UPDATE ACCOUNT SET BAL = BAL - 100000 WHERE ID = 'A';
UPDATE ACCOUNT SET BAL = BAL + 100000 WHERE ID = 'B';
COMMIT TRAN; -- if there is a problem, ROLLBACK TRAN;SET IMPLICIT_TRANSACTIONS ON puts you in implicit-transaction mode, where you must COMMIT yourself, as in Oracle.SHOW AUTOCOMMIT showed autocommit OFF.Tomorrow we cover DDL (CREATE, ALTER, DROP), which creates and modifies tables, along with constraints, and DCL (GRANT, REVOKE), which grants and revokes privileges.
Click a choice to reveal the answer and explanation.
Question 1
On EMP_T (CLARK 2450, KING 5000, MILLER 1300), the following were run in order: UPDATE EMP_T SET SAL = 1000 WHERE ENAME = 'CLARK'; → SAVEPOINT SP1; → UPDATE EMP_T SET SAL = 2000 WHERE ENAME = 'KING'; → SAVEPOINT SP2; → DELETE FROM EMP_T WHERE ENAME = 'MILLER'; → ROLLBACK TO SP1; → COMMIT;. What is KING's committed SAL?
Question 2
On T_DEPT (4 rows), a copy of DEPT, the following were run in order: INSERT INTO T_DEPT VALUES (50, 'EDUCATION', 'SEOUL'); → CREATE TABLE T_LOG (MSG VARCHAR2(20)); → ROLLBACK;. What is the result of SELECT COUNT(*) FROM T_DEPT;?
Question 3
Which property says "all operations in a transaction must take effect, or none of them may"?
Question 4
Which statement about transaction handling in Oracle and SQL Server is correct?
Question 5
On T_DEPT (10, 20, 30, 40), a copy of DEPT, the following were run in order: SAVEPOINT A; → DELETE FROM T_DEPT WHERE DEPTNO = 10; → SAVEPOINT A; → DELETE FROM T_DEPT WHERE DEPTNO = 20; → ROLLBACK TO A;. How many rows does T_DEPT have now?