Easy Tutorial
❮ Mysql Delete Query Mysql Php Syntax ❯

MySQL Transactions

MySQL transactions are primarily used for handling large and complex data operations. For example, in a personnel management system, when you delete a person, you need to delete their basic information as well as related information such as mailboxes and articles. These database operation statements together form a transaction!

Generally, a transaction must meet four conditions (ACID): Atomicity (Atomicity, or indivisibility), Consistency (Consistency), Isolation (Isolation, also known as independence), and Durability (Durability).

In the default settings of MySQL command line, transactions are automatically committed, meaning that after an SQL statement is executed, a COMMIT operation is immediately performed. Therefore, to explicitly start a transaction, you must use the commands BEGIN or START TRANSACTION, or execute the command SET AUTOCOMMIT=0 to disable automatic commit for the current session.

Transaction Control Statements:

MySQL Transaction Processing Methods:

  1. Using BEGIN, ROLLBACK, COMMIT:

    • BEGIN starts a transaction
    • ROLLBACK rolls back the transaction
    • COMMIT confirms the transaction
  2. Directly changing MySQL's auto-commit mode with SET:

    • SET AUTOCOMMIT=0 disables auto-commit
    • SET AUTOCOMMIT=1 enables auto-commit

Transaction Testing

mysql> use tutorialpro;
Database changed
mysql> CREATE TABLE tutorialpro_transaction_test( id int(5)) engine=innodb;  # Create a data table
Query OK, 0 rows affected (0.04 sec)

mysql> select * from tutorialpro_transaction_test;
Empty set (0.01 sec)

mysql> begin;  # Start transaction
Query OK, 0 rows affected (0.00 sec)

mysql> insert into tutorialpro_transaction_test value(5);
Query OK, 1 rows affected (0.01 sec)

mysql> insert into tutorialpro_transaction_test value(6);
Query OK, 1 rows affected (0.00 sec)

mysql> commit; # Commit transaction
Query OK, 0 rows affected (0.01 sec)

mysql>  select * from tutorialpro_transaction_test;
+------+
| id   |
+------+
| 5    |
| 6    |
+------+
2 rows in set (0.01 sec)

mysql> begin;    # Start transaction
Query OK, 0 rows affected (0.00 sec)

mysql>  insert into tutorialpro_transaction_test values(7);
Query OK, 1 rows affected (0.00 sec)

mysql> rollback;   # Rollback
Query OK, 0 rows affected (0.00 sec)

mysql>   select * from tutorialpro_transaction_test;   # Data not inserted due to rollback
+------+
| id   |
+------+
| 5    |
| 6    |
+------+
2 rows in set (0.01 sec)

mysql>

PHP Example Using Transactions

MySQL ORDER BY Testing:

<?php
$dbhost = 'localhost';  // MySQL server host address
$dbuser = 'root';       // MySQL username
$dbpass = '123456';     // MySQL user password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Connection failed: ' . mysqli_error($conn));
}
// Set encoding to prevent Chinese乱码
mysqli_query($conn, "set names utf8");
mysqli_select_db( $conn, 'tutorialpro' );
mysqli_query($conn, "SET AUTOCOMMIT=0"); // Set to not auto-commit, as MySQL defaults to immediate execution
mysqli_begin_transaction($conn);            // Start transaction definition

if(!mysqli_query($conn, "insert into tutorialpro_transaction_test (id) values(8)"))
{
    mysqli_query($conn, "ROLLBACK");     // Rollback if execution fails
}

if(!mysqli_query($conn, "insert into tutorialpro_transaction_test (id) values(9)"))
{
    mysqli_query($conn, "ROLLBACK");      // Rollback if execution fails
}
mysqli_commit($conn);            // Execute transaction
mysqli_close($conn);
?>
❮ Mysql Delete Query Mysql Php Syntax ❯