事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这组数据库命令要么都执行,要么都不执行
事务是一个不可分割的工作逻辑单元,在数据库系统上执行并发操作时,事务是最小的控制单元
事务适用于多用户同时操作的数据库系统的场景,如银行、保险公司及证券交易系统等等
事务通过事务的整体性以保证数据的一致性
事务是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位
事务的隔离级别决定了事务之间可见的级别
用以控制事务所做的修改,并将修改通告至其它并发的事务
MySQL默认的事务处理级别是repeatable read,而Oracle和SQL Server是read committed
show global variables like '%isolation%';
SELECT @@global.tx_isolation;
show session variables like '%isolation%';
SELECT @@session.tx_isolation;
SELECT @@tx_isolation;
set global transaction isolation level read committed;
set @@global.tx_isolation='read-committed'; #重启服务后失效
set session transaction isolation level repeatable read;
set @@session.tx_isolation='repeatable-read';
在事务完成以后,该事务对数据库所作的更改便持久的保存在数据库之中,并不会被回滚
不管系统是否发生故障,事务处理的结果都是永久的
一旦事务被提交,事务的效果会被永久地保留在数据库中
在事务管理中,原子性是基础,隔离性是手段,一致性是目的,持久性是结果
名称 | 含义 |
---|---|
BEGIN 或 START TRANSACTION | 显式地开启一个事务 |
COMMIT 或 COMMIT WORK | 提交事务,并使已对数据库进行的所有修改变为永久性的 |
ROLLBACK 或 ROLLBACK WORK | 回滚会结束用户的事务,并撤销正在进行的所有未提交的修改 |
SAVEPOINT S1 | 使用 SAVEPOINT 允许在事务中创建一个回滚点,一个事务中可以有多个 SAVEPOINT;“S1”代表回滚点名称 |
ROLLBACK TO [SAVEPOINT] S1 | 把事务回滚到标记点 |
use tour;
create table account(
id int(10) primary key not null,
name varchar(40),
money double
);
insert into account values(1,'A',1000);
insert into account values(2,'B',1000);
例:mysql> use tour;
Database changed
mysql> create table account(
-> id int(10) primary key not null,
-> name varchar(40),
-> money double
-> );
Query OK, 0 rows affected
mysql> insert into account values(1,'A',1000);
Query OK, 1 row affected
mysql> insert into account values(2,'B',1000);
Query OK, 1 row affected
begin;
update account set money= money - 100 where name='A';
commit;
quit
例:mysql> begin;
Query OK, 0 rows affected
mysql> update account set money=money - 100 where name='A';
Query OK, 1 row affected
Rows matched: 1 Changed: 1 Warnings: 0
mysql> commit;
Query OK, 0 rows affected
mysql> quit
mysql -u root -p
use tour;
select * from account;
例:[root@www ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use tour;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 900 |
| 2 | B | 1000 |
+----+------+-------+
2 rows in set (0.00 sec)
begin;
update account set money= money + 100 where name='A';
rollback;
例:mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update account set money= money + 100 where name='A';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> rollback;
Query OK, 0 rows affected (0.01 sec)
begin;
update account set money= money + 100 where name='A';
SAVEPOINT S1;
update account set money= money + 100 where name='B';
SAVEPOINT S2;
insert into account values(3,'C',1000);
例:mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update account set money= money + 100 where name='A';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SAVEPOINT S1;
Query OK, 0 rows affected (0.00 sec)
mysql> update account set money= money + 100 where name='B';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SAVEPOINT S2;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into account values(3,'C',1000);
Query OK, 1 row affected (0.00 sec)
select * from account;
ROLLBACK TO S1;
select * from account;
例:mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 1000 |
| 2 | B | 1100 |
| 3 | C | 1000 |
+----+------+-------+
3 rows in set (0.00 sec)
mysql> ROLLBACK TO S1;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 1000 |
| 2 | B | 1000 |
+----+------+-------+
2 rows in set (0.00 sec)
SET AUTOCOMMIT=0; #禁止自动提交
SET AUTOCOMMIT=1; #开启自动提交,Mysql默认为1
SHOW VARIABLES LIKE 'AUTOCOMMIT'; #查看Mysql中的AUTOCOMMIT值
use tour;
select * from account;
SET AUTOCOMMIT=0;
update account set money= money + 100 where name='B';
select * from account;
quit
例:mysql> use tour;
Database changed
mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 1000 |
| 2 | B | 1000 |
+----+------+-------+
2 rows in set (0.00 sec)
mysql> SET AUTOCOMMIT=0;
Query OK, 0 rows affected (0.00 sec)
mysql> update account set money= money + 100 where name='B';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 1000 |
| 2 | B | 1100 |
+----+------+-------+
2 rows in set (0.00 sec)
mysql> quit
mysql -u root -p
use tour;
select * from account;
例:[root@www ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use tour;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 900 |
| 2 | B | 1000 |
+----+------+-------+
2 rows in set (0.00 sec)