WHERE TUOI = 25;
SQL> COMMIT;
Therefore, the two rows from the table will be deleted and the SELECT statement will produce the following results.
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
The ROLLBACK command is the Transaction control command used to return the Transaction to a state before changes have not yet been saved to the Database. The ROLLBACK command can only be used to undo transactions before confirming it with the last Commit or Rollback command.
The basic syntax of ROLLBACK command is as follows:
ROLLBACK;
For example : Suppose the NHANVIEN table has the following records:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
Now use the ROLLBACK command with the delete command tuoi = 25 , not yet committed as follows:
SQL> DELETE FROM NHANVIEN
WHERE TUOI = 25;
SQL> ROLLBACK;
In the results obtained, this DELETE operation does not affect the table because of ROLLBACK changes in the database, the SELECT statement will produce the result:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
SAVEPOINT is a point in a Transaction that you can back Transaction back to a certain point without having to reverse the Transaction to the first state before making that change.
The basic syntax of the SAVEPOINT command is as follows:
SAVEPOINT TEN_SAVEPOINT;
This command only creates SAVEPOINT in Transaction transactions. ROLLBACK then needs to be used to undo a SAVEPOINT as follows:
ROLLBACK TO TEN_SAVEPOINT;
For example : You want to delete three different records from the NHANVIEN table and want to create SAVEPOINT before each deletion to be able to ROLLBACK back to SAVEPOINT at any time to return the appropriate data for the initial state.
Suppose the NHANVIEN table has the following records:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
Here is a sequence of statements:
SQL> SAVEPOINT SP1;
Savepoint created.
SQL> DELETE FROM NHANVIEN WHERE ID=1;
1 row deleted.
SQL> SAVEPOINT SP2;
Savepoint created.
SQL> DELETE FROM NHANVIEN WHERE ID=2;
1 row deleted.
SQL> SAVEPOINT SP3;
Savepoint created.
SQL> DELETE FROM NHANVIEN WHERE ID=3;
1 row deleted.
Above, there are 3 data delete operations that take place. Suppose you change your mind and decide ROLLBACK to SAVEPOINT that you identified as SP2. Because SP2 was created after the first delete operation, the last two delete operations are restored.
SQL> ROLLBACK TO SP2;
Rollback complete.
So only the first delete operation takes place after you ROLLBACK to SP2.
The SAVEPOINT RELEASE command is used to remove a SAVEPOINT that you have created. When SAVEPOINT is deleted, you cannot use the ROLLBACK command to undo those SAVEPOINT transactions.
The syntax of SAVEPOINT RELEASE is as follows:
RELEASE SAVEPOINT TEN_SAVEPOINT;
The SET TRANSACTION can be used to initialize a Database Transaction. This command is used to specify properties for that Transaction. For example, you can specify a Transaction to be read only (read only) or read or write (read write).
The basic syntax of the SET TRANSACTION command in SQL is as follows:
SET TRANSACTION [ READ WRITE | READ ONLY ];
In the next section, we will learn about the representative operators - WILDCARD in SQL , remember to watch.
Previous article: HAVING clause in SQL
Next lesson: Representative operator - WILDCARD in SQL