About the Transaction feature in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces the transaction features in MySQL, which is the basic knowledge in MySQL beginners. if you need it, you can refer to that the next transaction is a continuous set of database operations, it is like a single unit of work. In other words, it will never be a complete transaction, unless each individual operation in the group is successful. If any operation in the transaction fails, the entire transaction will fail.

In fact, many SQL statements in the club are queried to a group, and all the execution personnel are considered as part of the transaction.
Features of transactions:

Transactions are abbreviated as ACID, which is commonly referred to:

  • Atomicity: make sure all operations in the work unit are successfully completed. Otherwise, the transaction will be aborted at the fault point, and the previous operations will be rolled back to the previous state.
  • Consistency: transactions that are successfully committed after the database correctly changes the status.
  • Isolation: independent and transparent transaction operations.
  • Durability: ensure that the results or effects of the committed transactions remain in the case of system faults.
  • In MySQL, transactions begin to work and end with COMMIT or ROLLBACK statements. A large number of transactions are formed between the SQL commands in the beginning and end sentences.
  • COMMIT & ROLLBACK:
  • The two keywords commit and rollback are mainly used for MySQL transactions.
  • After a successful transaction is completed, the COMMIT command should be issued so that all changes that participate in the table will take effect.
  • In the event of a fault, a ROLLBACK command should be issued to return each table referenced in the transaction to the previous state.

The controllable transaction behavior is called AUTOCOMMIT setting session variables. If AUTOCOMMIT is set to 1 (default value), then each SQL statement (in transaction or not) is considered a complete transaction and promises to be completed by default. When AUTOCOMMIT is SET to 0, the set autocommit = 0 command is issued. the subsequent statements act like a transaction until a clear COMMIT statement is submitted without activity.

You can use the mysql_query () function to execute these SQL commands in PHP.
General transaction example

This series of events are independent of the programming language used, and can be created in any language used to create the logical path of the application.
You can use the mysql_query () function to execute these SQL commands in PHP.

  • Begin work start the transaction to issue the SQL command
  • Issue one or more SQL commands, such as SELECT, INSERT, UPDATE, or DELETE
  • Check whether there are any errors. everything depends on your needs.
  • If there are any errors, the ROLLBACK command is incorrect; otherwise, the COMMIT command is issued.

Transaction Security table type in MySQL:

If you plan to use MySQL transaction programming, you need to create a table in a special way. There are many transaction support, but the most popular is the InnoDB table type.

When compiling MySQL from source code, InnoDB tables support specific compilation parameters. If the MySQL version does not support InnoDB, ask the internet service provider to create a version of MySQL that supports the InnoDB table type, you can also download and install the MySQL-Max binary distribution and table types for Windows or Linux/UNIX in the development environment.
If MySQL installation supports InnoDB tables, you only need to add a TYPE = InnoDB definition table creation statement. For example, the following code creates the InnoDB table tcount_tbl:

root@host# mysql -u root -p password;Enter password:*******mysql> use TUTORIALS;Database changedmysql> create table tcount_tbl  -> (  -> tutorial_author varchar(40) NOT NULL,  -> tutorial_count INT  -> ) TYPE=InnoDB;Query OK, 0 rows affected (0.05 sec)

Please refer to the following link for more information:

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.