A database transaction concept
Database Transaction is actually performing a series of operations on the database, while the transaction is either fully performing all operations or doing nothing at all, which guarantees the security and stability of the data.
Two- transaction features
Transactions have so-called acid properties, with the following characteristics:
1. atomicity (atomicity): atoms mean the smallest particles, or things that cannot be divided. The principle of non-re-division of database transactions is atomicity. All queries that make up a transaction must either be executed or all canceled.
2. Consistency (consistency): refers to the data rules, should be consistent before/after the transaction.
3. Isolation (Isolation): Simply put, the operation of a transaction is not visible to other transactions.
4. persistence (Durability): When a transaction is complete, its impact should be preserved and cannot be undone.
Three- transaction usage steps
1. Open transaction: Start transaction;
2. Perform SQL operations;
3. Commit or Rollback (commit/rollback)
Four operational examples
1. Create the following table and insert two data
Mysql> CREATE Table A (
ID int,
money int);
Mysql> INSERT into a values (1,5000), (2,5000);
2. The Black window is assumed to be the server side, the Blue window is assumed to be the client, querying the data separately, as follows
3. The server opened the transaction, while performing SQL operations, 1th to 2nd to transfer 500 yuan, while the server in the absence of a commit operation, the client will still see the original data.
4. The server commits the confirmation (commit) operation, the client sees that the data has changed
5. If the server executes the same transaction again, but finally cancels (rollback) The transaction, the data that the client sees does not change
MySQL Learning notes-Transactions