MYSQL--事务处理_MySQL
bitsCN.com
MYSQL--事务处理
事务处理在各种管理系统中都有着广泛的应用,比如人员管理系统,很多同步数据库操作大都需要用到事务处理。比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成一个事务! www.bitsCN.com
删除的SQL语句
delete from userinfo where ~~~
delete from mail where ~~
delete from article where~~
~~ www.bitsCN.com
如果没有事务处理,在你删除的过程中,假设出错了,只执行了第一句,那么其后果是难以想象的!
但用事务处理。如果删除出错,你只要rollback就可以取消删除操作(其实是只要你没有commit你就没有确实的执行该删除操作)
一般来说,在商务级的应用中,都必须考虑事务处理的!
查看inodb信息
shell> /usr/local/mysql -u root -p
mysql> show variables like "have_%"
系统会提示:
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| have_bdb | YES |
| have_crypt | YES |
| have_innodb | YES |
| have_isam | YES |
| have_raid | YES |
| have_symlink | YES |
| have_openssl | NO |
| have_query_cache | YES |
+------------------+-------+
8 rows in set (0.05 sec)
如果是这样的,那么我们就可以创建一张支持事务处理的表来试试了。
MYSQL的事务处理功能!
作者:Feifengxlq Email:feifengxlq@sohu.com
一直以来我都以为MYSQL不支持事务处理,所以在处理多个数据表的数据时,一直都很麻烦(我是不得不将其写入文本文件,在系统重新加载得时候才写入数据库以防出错)~今天发现MYSQL数据库从4.1就开始支持事务功能,据说5.0将引入存储过程^_^
先简单介绍一下事务吧!事务是DBMS得执行单位。它由有限得数据库操作序列组成得。但不是任意得数据库操作序列都能成为事务。一般来说,事务是必须满足4个条件(ACID)
原子性(Autmic):事务在执行性,要做到“要么不做,要么全做!”,就是说不允许事务部分得执行。即使因为故障而使事务不能完成,在rollback时也要消除对数据库得影响!
一致性(Consistency):事务得操作应该使使数据库从一个一致状态转变倒另一个一致得状态!就拿网上购物来说吧,你只有即让商品出库,又让商品进入顾客得购物篮才能构成事务!
隔离性(Isolation):如果多个事务并发执行,应象各个事务独立执行一样!
持久性(Durability):一个成功执行得事务对数据库得作用是持久得,即使数据库应故障出错,也应该能够恢复!
MYSQL的事务处理主要有两种方法。
1、用begin,rollback,commit来实现
begin 开始一个事务
rollback 事务回滚
commit 事务确认
2、直接用set来改变mysql的自动提交模式
MYSQL默认是自动提交的,也就是你提交一个QUERY,它就
直接执行!我们可以通过
set autocommit=0 禁止自动提交
set autocommit=1 开启自动提交
来实现事务的处理。
但注意当你用 set autocommit=0 的时候,你以后所有的SQL都将
做为事务处理,直到你用commit确认或rollback结束,注意当你结
束这个事务的同时也开启了个新的事务!按第一种方法只将当前的作为一个事务!
个人推荐使用第一种方法!
MYSQL中只有INNODB和BDB类型的数据表才能支持事务处理!
其他的类型是不支持的!(切记!)
下次有空说下MYSQL的数据表的锁定和解锁!
MYSQL5.0 WINXP下测试通过~ ^_^
mysql> use test;
Database changed
mysql> CREATE TABLE `dbtest`(
-> id int(4)
-> ) TYPE=INNODB;
Query OK, 0 rows affected, 1 warning (0.05 sec)
mysql> select * from dbtest
-> ;
Empty set (0.01 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into dbtest value(5);
Query OK, 1 row affected (0.00 sec)
mysql> insert into dbtest value(6);
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from dbtest;
+------+
| id |
+------+
| 5 |
| 6 |
+------+
2 rows in set (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into dbtest values(7);
Query OK, 1 row affected (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from dbtest;
+------+
| id |
+------+
| 5 |
| 6 |
+------+
2 rows in set (0.00 sec)
mysql>
*************************************************
[PHP]
function Tran( $sql ) {
$judge = 1;
mysql_query('begin');
foreach ($sql as $v) {
if ( !mysql_query($v) ) {
$judge = 0;
}
}
if ($judge == 0) {
mysql_query('rollback');
return false;
}
elseif ($judge == 1) {
mysql_query('commit');
return true;
}
}
[/PHP]
************************************************
$handler=mysql_connect("localhost","root","");
mysql_select_db("task");
mysql_query("SET AUTOCOMMIT=0");//设置为不自动提交,因为MYSQL默认立即执行
mysql_query("BEGIN");//开始事务定义
if(!mysql_query("insert into trans (id) values('2')"))
{
mysql_query("ROOLBACK");//判断当执行失败时回滚
}
if(!mysql_query("insert into trans (id) values('4')"))
{
mysql_query("ROOLBACK");//判断执行失败回滚
}
mysql_query("COMMIT");//执行事务
mysql_close($handler);
?>
bitsCN.com
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



How to write a simple online lending management system through PHP requires specific code examples. Introduction: With the advent of the digital age, library management methods have also undergone tremendous changes. Traditional manual recording systems are gradually being replaced by online borrowing management systems. Online borrowing management systems greatly improve efficiency by automating the process of borrowing and returning books. This article will introduce how to use PHP to write a simple online lending management system and provide specific code examples. 1. System requirements analysis before starting to write the online borrowing management system

With technological advancement and social development, smart property management systems have become an indispensable part of modern urban development. In this process, the smart property management system based on Go language has attracted much attention due to its advantages such as efficiency, reliability, and speed. This article will introduce the practice of our team’s smart property management system using Go language. 1. Requirements analysis Our team mainly develops this property management system for a real estate company. Its main task is to connect property management companies and residents to facilitate the management of property management companies, and also to allow residents to

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

How to use PHP to perform database operations in a Linux environment. In modern web applications, the database is an essential component. PHP is a popular server-side scripting language that can interact with various databases. This article will introduce how to use PHP scripts for database operations in a Linux environment and provide some specific code examples. Step 1: Install the Necessary Software and Dependencies Before starting, we need to ensure that PHP and related dependencies are installed in the Linux environment. usually

Pagoda Panel is a powerful panel software that can help us quickly deploy, manage and monitor servers, especially small businesses or individual users who often need to build websites, database management and server maintenance. Among these tasks, MySQL database management is an important job in many cases. So how to use the Pagoda panel for MySQL management? Next, we will introduce it step by step. Step 1: Install Pagoda Panel. Before starting to use Pagoda Panel for MySQL management, you first need to install Pagoda Panel.

How to use MongoDB to develop a simple website backend management system. With the development of the Internet, the use and management of websites have become more and more important. In order to facilitate website administrators to manage website content in the background, it is essential to develop a simple and efficient website background management system. This article will introduce how to use MongoDB to develop a simple website backend management system, and demonstrate it through specific code examples. Preparation First, we need to ensure that the MongoDB database has been installed and configured. specific

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my

How to write a simple student dormitory management system using C++? The student dormitory management system is a software system that can conveniently manage student dormitory information. Using C++ to write a simple student dormitory management system can not only exercise your programming skills, but also improve your understanding and grasp of the student dormitory management process. This article will introduce how to use C++ to write a simple student dormitory management system. First, we need to define the basic information of the student dormitory, including student name, student number, gender, contact information, etc. We can use a structure
