Mysql transaction and mysql storage engine

Source: Internet
Author: User
Transaction Concept and Storage Engine 1.0 why transaction? Let's take a look at a scenario where bank transfers and remittances: Li Yanhong and Zhou Hongyi fight every day. now let Li Yanhong transfer RMB 1000 to Zhou Hongyi and design the following table account table number (id) user name (user) amount (cash... "> <LINKhref =" http://www.php100.com//static

 

Transaction Concept and storage engine

1.0 why transaction?

Let's look at a scenario where bank transfers and remittances:

Li Yanhong and Zhou Hongyi fought every day and now asked Li Yanhong to transfer 1000 yuan to Zhou Hongyi

Design the following table

Account table

No. (id) user amount (cash)

1 Li Yanhong 3000

2 Zhou Hongyi 2000

Traditional practices:

Mysql> update account set cash = cash-1000 where id = 2;

Update account set cash = cash + 1000 where id = 1;

Defects:

Insecure. For example, the command to reduce Li Yanhong's money was executed, but the command to increase Zhou Hongyi's money was not executed successfully.

At this time, we need to use the mysql transaction mechanism.

Transaction 1.1

  

 

1.2 Use of transactions

Transaction-related statement DTL

  

 

1.3 Four Features of transactions:

1. atomicity is an inseparable logical unit. a group of SQL statements are either executed or not executed.

2. isolation, the execution process in the transaction is invisible.

3. Durability. Once a transaction is committed, it cannot be undone.

4. Consistency. data is consistent before and after a transaction occurs. (Conservation of energy)

1.4 Principles of transactions:

Traditional methods:

  

 

Transaction mechanism:

  

 

The preceding example shows how Li Yanhong transfers money to Zhou Hongyi:

# Enable things

Start transaction;

# Executed code

Update account set cash = cash-1000 where id = 1;

Update account set cash + 1000 where id = 2;

# Submit

Commit

# Statement errors can be rolled back

# Rollback

1.5 when transactions are used:

Generally, transactions are recommended for businesses with higher security requirements.

2.0 mysql storage engine

What is a storage engine?

We say that databases are warehouses that organize, store, and manage data. Then, the database stores data by means of a storage engine.

In mysql, the storage engine is loaded as a plug-in. Mysql has a wide variety of storage engines. for us, we need to be familiar with the two storage engines, MyISAM and inonoDB.

Myisam does not support transactions.

Innodb supports transactions.

Full-text indexing is not supported by innodb in versions earlier than MySQL 5.5, but is supported by versions later than MySQL.

Common storage engines

  

 

The default storage engine of mysql,

Versions earlier than mysql5.5: MyISAM

Versions later than mysql5.5: innodb

If we want to declare the storage engine ourselves:

Create table () engine = myisam default charset utf8

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.