Using transactions
Depending on the application you use to connect to MySQL, you may have to execute a COMMIT
statement to save the data. By default, the MySQL client is set to use autocommit
, so you don't have to do this. If you want to have the option to undo the INSERT
statement, then you need to use a transaction. This can be done either with a BEGIN
statement or a START TRANSACTION
statement. Once you have run one or more statements to modify the data, you need to use COMMIT
or ROLLBACK
.
The following code shows how to use a transaction:
BEGIN; -- This indicates the begin of the transaction INSERT INTO mytable VALUES (1, 'foo', 'bar', 'baz'); SELECT * FROM mytable; COMMIT; -- Use ROLLBACK instead of COMMIT if you don't want to save your work
Note
In the preceding statements, the two dashes followed by a space (--
) indicates a comment in MySQL. These comments can also use C style comments; for example, ...