Skip to content

TRANSACTION

START TRANSACTION;
INSERT INTO Students (Name, Class, Mark) VALUES ('John Doe', '10th', 85);
INSERT INTO Students (Name, Class, Mark) VALUES ('Jane Smith', '10th', 90);
COMMIT;
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 0 row affected (0.01 sec)

The example demonstrates a transaction in MySQL. The START TRANSACTION command is used to initiate a new transaction. Two INSERT statements follow, each adding a new row to the Students table. The COMMIT command concludes the transaction, applying all changes made since START TRANSACTION. If any command within the transaction encounters an error, all changes made during the transaction would be discarded in order to maintain the database’s consistency.