autocommitA transaction is a set of SQL statements that execute as a unit. Either all the statements execute successfully, or none of them have any effect. This is achieved by using the commit and rollback capabilities of MySQL. If all of the statements in the transaction succeed, changes to the record are committed to the database. If an error occurs during the transaction, the changes are rolled back and canceled.
Transactional processing provides stronger guarantees about the outcome
of database operations, but requires more overhead in CPU cycles, memory, and disk space.
InnoDB is a transaction-safe storage engine.
By default, MySQL runs in autocommit mode, which means that changes
made by individual statements are committed to the database immediately to make them permanent. By default,
each statement is its own transaction. To perform transactions explicitly, disable autocommit mode
and then tell MySQL when to commit or roll back changes.
InnoDB storage engine;autocommit disabled;autocommit enabled;mysql>create table bad(i int) engine = ferrari;
Did you get an error message?
mysql>show warnings;
sql_mode value.mysql>SELECT @@session.sql_mode;
mysql>SET SESSION sql_mode='NO_ENGINE_SUBSTITUTION';
autocommit variableautocommit is enabled in MySQL. Find its current value.
mysql>SELECT @@autocommit;
autocommit.mysql>SET autocommit=1;
mysql>CREATE TABLE t1 (name CHAR(20),
->UNIQUE (name)) ENGINE = InnoDB;
START TRANSACTION statement to suspend autocommit mode.mysql> START TRANSACTION;
mysql> INSERT INTO t1 SET name = 'William';
mysql> INSERT INTO t1 SET name = 'Wallace';
mysql> COMMIT;
mysql> SELECT * FROM t1;
+---------+
| name |
+---------+
| Wallace |
| William |
+---------+
ROLLBACK.mysql> START TRANSACTION;
mysql> INSERT INTO t1 SET name = 'Gromit';
mysql> INSERT INTO t1 SET name = 'Wallace';
Do you get an error regarding a duplicate entry?
mysql> ROLLBACK;
mysql> SELECT * FROM t1;
+---------+
| name |
+---------+
| Wallace |
| William |
+---------+
mysql>CREATE TABLE t2 (name CHAR(20),
->UNIQUE (name)) ENGINE = InnoDB;
autocommit mode.mysql> SET autocommit = 0;
mysql> INSERT INTO t2 SET name = 'William';
mysql> INSERT INTO t2 SET name = 'Wallace';
mysql> COMMIT;
mysql> SELECT * FROM t2;
+---------+
| name |
+---------+
| Wallace |
| William |
+---------+
ROLLBACK.mysql> START TRANSACTION;
mysql> INSERT INTO t2 SET name = 'Gromit';
mysql> INSERT INTO t2 SET name = 'Wallace';
Do you get an error regarding a duplicate entry?
mysql> ROLLBACK;
mysql> SELECT * FROM t2;
+---------+
| name |
+---------+
| Wallace |
| William |
+---------+
autocommit variableautocommit.mysql>SET autocommit=1;
mysql>CREATE TABLE t3 (name CHAR(20),
->UNIQUE (name)) ENGINE = InnoDB;
mysql> INSERT INTO t3 SET name = 'William';
mysql> INSERT INTO t3 SET name = 'Wallace';
mysql> SELECT * FROM t3;
+---------+
| name |
+---------+
| Wallace |
| William |
+---------+
mysql> INSERT INTO t3 SET name = 'Gromit';
mysql> INSERT INTO t3 SET name = 'Wallace';
Do you get an error regarding a duplicate entry?
mysql> ROLLBACK;
mysql> SELECT * FROM t3;
What does the table contain?
QUIT to exit out of the server
mysql>QUIT
Congratulations! You have successfully used a MySQL transaction-safe storage engine.