深入理解MySQL中的事务机制_MySQL
使用数据库事务可以确保除事务性单元内的所有操作都成功完成。MySQL中的InnoDB引擎的表才支持transaction。在一个事务里,如果出现一个数据库操作失败了,事务内的所有操作将被回滚,数据库将会回到事务前的初始状态。有一些不能被回滚的语句:将在本文的最后讨论。
在一个web应用中,会很经常遇到需要使用事务的地方,要么希望若干语句都执行成功,要么都不执行,如果出现有些执行成功,而其他的失败将会导致数据损坏。
在这篇文章的例子中,我们使用下面的两张表"employee"和"telephone",下面是SQL语句(作为参考):
创建 employee表:
1 2 3 4 5 6 7 8 9 |
|
向employee中插入数据
1 2 3 4 5 6 7 8 9 |
|
创建telephone表
1 2 3 4 5 6 7 |
|
向telephone表插入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
设想你需要一个新的叫做Grace Williams雇员,并带有他的电话号码信息。你可能会执行下面两句sql:
1 2 3 4 5 6 |
|
让我们看看第二个语句,在第一个语句中,employee_id是在第一条语句中指定的,设想一下,当第一条语句失败,而第二条语句成功的状况。在这种状况下,telephone表中就会有一条employee_id为9的记录,而employee表中并没有id为9的记录,而如果将这两个语句放在MySQL事务中,如果第一条语句失败,那么第二条语句也将回滚,从而不会造成这种问题。
在PHP(PHP参考文档)中我们可以使用如下的方式启用事务:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
当你执行mysqli_query函数的时候,结果被立即提交到了数据库。使用mysqli_autocommit函数,可以关闭自动提交,执行结果只有当你想提交的时候才提交。
如果任何语句执行失败我们都可以设置$flag变量为false。如果有很多语句要执行,可以考虑将他们放在for循环中。
最后,如果flag是true(也就是没有错误发生),我们使用mysqli_commit提交事务。否则我们使用mysqli_rollback回滚事务。
所以,事务可以在某种程度上帮助我们维护数据的完整和正确,另外,为了保证数据无误,我们还推荐使用外键。
并不是所有的语句都是支持事务的,例如,如果使用CREATE TABLE或者ALTER TABLE语句,需要了解更多可以参考MySQL手册查看哪些语句不能回滚。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
