MySQL advanced features ---- Transaction processing _ MySQL
Source: Internet
Author: User
To use MySQL for transaction processing, you must first create a table that uses the transaction table type (such as BDBBerkeleyDB or InnoDB. CREATETABLEaccount (account_idBIGINTUNSIGNEDNOTNULLPRIMARYKEYAUTO_INCREMENT, balanceDOUBLE) TYPEInnoDB; to use transaction processing on the transaction table, you must first disable the transaction processing from using MySQL, first, create a table that uses the transaction table type (such as BDB = Berkeley DB or InnoDB.
Create table account (
Account_id bigint unsigned not null primary key AUTO_INCREMENT,
Balance DOUBLE
) TYPE = InnoDB;
To use transaction processing in a transaction table, you must first disable automatic commit:
Set autocommit = 0;
Start transaction processing with the BEGIN command:
BEGIN;
Currently, the mysql client is located in the context of server-related things. Any changes made to the transaction table will not become permanent changes before committing.
Updated account set balance = 50.25 WHERE account_id = 1;
Update account set balance = 100.25 WHERE account_id = 2;
After all the changes are made, use the COMMIT command to complete the transaction processing:
COMMIT;
Of course, the true advantage of transaction processing is that it is reflected when an error occurs when the second statement is executed. if the entire transaction is terminated before submission, rollback can be performed:
ROLLBACK;
The following is another example of using MYSQL to perform mathematical operations:
SELECT @ first: = balance FROM account WHERE account_id = 1;
SELECT @ second: = balance FROM account WHERE account_id = 2;
UPDATE account SET balance = @ first-25.00 WHERE account_id = 1;
UPDATE account SET balance = @ second + 25.00 WHERE account_id = 2;
In addition to the COMMIT command, the following command automatically ends the current transaction:
ALTER TABLE
BEGIN
CREATE INDEX
DROP DATABASE
DROP TABLE
LOCK TABLES
RENAME TABLE
TRUNCATE
UNLOCK TABLES
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service