深入理解MySQL中的事务机制_MySQL
使用数据库事务可以确保除事务性单元内的所有操作都成功完成。MySQL中的InnoDB引擎的表才支持transaction。在一个事务里,如果出现一个数据库操作失败了,事务内的所有操作将被回滚,数据库将会回到事务前的初始状态。有一些不能被回滚的语句:将在本文的最后讨论。
在一个web应用中,会很经常遇到需要使用事务的地方,要么希望若干语句都执行成功,要么都不执行,如果出现有些执行成功,而其他的失败将会导致数据损坏。
在这篇文章的例子中,我们使用下面的两张表"employee"和"telephone",下面是SQL语句(作为参考):
创建 employee表:
CREATE TABLE `employee` ( `id` int NOT NULL AUTO_INCREMENT, `first_name` varchar(100) NOT NULL, `last_name` varchar(100) NOT NULL, `job_title` varchar(100) DEFAULT NULL, `salary` double DEFAULT NULL, `notes` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
向employee中插入数据
INSERT INTO `employee` (`first_name`, `last_name`, `job_title`, `salary`) VALUES ('Robin', 'Jackman', 'Software Engineer', 5500), ('Taylor', 'Edward', 'Software Architect', 7200), ('Vivian', 'Dickens', 'Database Administrator', 6000), ('Harry', 'Clifford', 'Database Administrator', 6800), ('Eliza', 'Clifford', 'Software Engineer', 4750), ('Nancy', 'Newman', 'Software Engineer', 5100), ('Melinda', 'Clifford', 'Project Manager', 8500), ('Harley', 'Gilbert', 'Software Architect', 8000);
创建telephone表
CREATE TABLE `telephone` ( `id` int NOT NULL AUTO_INCREMENT, `employee_id` int DEFAULT NULL, `type` varchar(20) NOT NULL, `no` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
向telephone表插入数据
INSERT INTO `telephone` (`employee_id`, `type`, `no`) VALUES (1, 'mobile', '245-249697'), (2, 'mobile', '270-235969'), (2, 'land', '325-888885'), (3, 'mobile', '270-684972'), (4, 'mobile', '245-782365'), (4, 'land', '325-888886'), (5, 'mobile', '245-537891'), (6, 'mobile', '270-359457'), (7, 'mobile', '245-436589'), (7, 'land', '325-888887'), (8, 'mobile', '245-279164'), (8, 'land', '325-888888');
设想你需要一个新的叫做Grace Williams雇员,并带有他的电话号码信息。你可能会执行下面两句sql:
INSERT INTO `employee` (`id`, `first_name`, `last_name`, `job_title`, `salary`) VALUES (9, 'Grace', 'Williams', 'Softwaree Engineer', 5000); INSERT INTO `telephone` (`id`, `employee_id`, `type`, `no`) VALUES (13, 9, 'mobile', '270-598712');
让我们看看第二个语句,在第一个语句中,employee_id是在第一条语句中指定的,设想一下,当第一条语句失败,而第二条语句成功的状况。在这种状况下,telephone表中就会有一条employee_id为9的记录,而employee表中并没有id为9的记录,而如果将这两个语句放在MySQL事务中,如果第一条语句失败,那么第二条语句也将回滚,从而不会造成这种问题。
在PHP(PHP参考文档)中我们可以使用如下的方式启用事务:
<?php //$salary = 5000; $salary = '$5000'; /* Change database details according to your database */ $dbConnection = mysqli_connect('localhost', 'robin', 'robin123', 'company_db'); mysqli_autocommit($dbConnection, false); $flag = true; $query1 = "INSERT INTO `employee` (`id`, `first_name`, `last_name`, `job_title`, `salary`) VALUES (9, 'Grace', 'Williams', 'Softwaree Engineer', $salary)"; $query2 = "INSERT INTO `telephone` (`id`, `employee_id`, `type`, `no`) VALUES (13, 9, 'mobile', '270-598712')"; $result = mysqli_query($dbConnection, $query1); if (!$result) { $flag = false; echo "Error details: " . mysqli_error($dbConnection) . ". "; } $result = mysqli_query($dbConnection, $query2); if (!$result) { $flag = false; echo "Error details: " . mysqli_error($dbConnection) . ". "; } if ($flag) { mysqli_commit($dbConnection); echo "All queries were executed successfully"; } else { mysqli_rollback($dbConnection); echo "All queries were rolled back"; } mysqli_close($dbConnection); ?>
当你执行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

AI Hentai Generator
Generate AI Hentai for free.

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



Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Setting up a MySQL connection pool using PHP can improve performance and scalability. The steps include: 1. Install the MySQLi extension; 2. Create a connection pool class; 3. Set the connection pool configuration; 4. Create a connection pool instance; 5. Obtain and release connections. With connection pooling, applications can avoid creating a new database connection for each request, thereby improving performance.

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATETABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical case: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses the DELETE statement to delete the record matching the ID from the users table.

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...
