Reading the v.8.4 current documentation about "commit", we can read
By default, MySQL runs with autocommit mode enabled. This means that, when not otherwise inside a transaction, each statement is atomic, as if it were surrounded by START TRANSACTION and COMMIT. You cannot use ROLLBACK to undo the effect; however, if an error occurs during statement execution, the statement is rolled back.
and
With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK.
This example demonstrates that START TRANSACTION
makes it contents a single atomic operation with no effects when an error is thrown:
CREATE TABLE testing (
id int NOT NULL PRIMARY KEY
) ENGINE=InnoDB;
INSERT INTO testing (id) VALUES (1);
START TRANSACTION;
INSERT INTO testing(id) VALUES (9);
INSERT INTO testing(id) VALUES (1); -- It fails because primary key
COMMIT;
The first INSERT
was not saved:
SELECT * from testing;
+----+
| id |
+----+
| 1 |
+----+
You can see more explanations and examples in the section "autocommit, commit and rollback".
Many SOF answers redirect to the same PROCEDURE
solution, and I don't understand why. Might it be a matter of MySQL versions?