A transaction is a logical unit of work used to separate database activities. A transaction has a starting point and an ending point;
When one of the following events occurs, the transaction starts:
- Connect to the database and execute the DML statement on the first day;
- After the current transaction ends, another DML statement is entered;
When one of the following events occurs, the transaction ends:
- Execute the COMMIT or ROLLBACK statement;
- Execute a DLL statement, such as the create table statement. In this case, the COMMIT statement is automatically executed;
- Execute a DCL statement, such as a GRANT statement. In this case, the COMMIT statement is automatically executed;
- Disconnect from the database. When you exit SQL * Plus, the EXIT command is usually entered, and the COMMIT statement is automatically executed. If SQL * Plus is accidentally terminated (for example, the computer running SQL * Plus crashes), the ROLLBACK statement is automatically executed;
- If a DML statement is executed, the statement fails. In this case, ROLLBACK is executed for the invalid DML statement;
Note: after the transaction is completed, if you do not explicitly COMMIT or roll back the transaction, it is considered a bad programming habit. Therefore, ensure that the COMMIT or ROLLBACK statement is executed after each transaction.