一、数据库事务的概念

事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么不都执行。

事务是一个不可分割的工作逻辑单元,在数据库系统上执行并发操作时,事务是最小的控制单元。

事务适用于多用户同时操作的数据库系统的场景,如银行、保险公司及证券交易系统等等。通过事务的完整性以保证数据的一致性。

二、事务的ACID特点
事务具有四个属性:ACID
原子性(Atomicity)
一致性(Consistency)
隔高性(Isolation)
持久性(Durability)
1、原子性
事务是一个完整的操作,事务的各元素是不可分的(原子的),事务的所有元素必须作为一个整体提交或回滚。如果事务中的任何元素失败,则整个事务将失败。

2、一致性
当事务完成时,数据必须处于一致状态:在事务开始之前,数据库汇总存储的数据处于一致状态;在正在进行的事务中,数据可能处于不一致的状态;当事务完成时,数据必须再次回到已知的一致状态。

3、隔离性
对数据进行修改的所有并发事务是彼此隔离的,这表明事务必须是独立的,它不应该以任何方式依赖于或影响其他事务。修改数据的事务可以在另一个使用相同数据的事务开始之前访问这些数据,或者在另一个使用相同数据的事务结束之后访问这些数据。

4、持久性
事务的持久性指不管系统是否发生了故障,事务处理的结果都是永久的。一旦事务被提交,事务的效果会被永久地保留在数据库中。

三、事务的操作

默认情况下MySQL的事物是自动提交的,当SQL语句 提交时事物便自动提交。

手动对事物进行控制的方法:

1、事物处理命令控制

事物处理命令控制事物:

begin:开始一个事物

commit:提交一个事物

rollback:回滚一个事物(撤销)

事物的操作必须基于Innodb存储引擎

操作:

[root@localhost ~]# mysql -uroot -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 37
Server version: 5.5.41-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| client |
| crushlinux |
| lty |
| mysql |
| performance_schema |
| test |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> use auth
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
MariaDB [auth]> show tables;
+----------------+
| Tables_in_auth |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)

MariaDB [auth]> drop table user;
Query OK, 0 rows affected (0.01 sec)

MariaDB [auth]> create table users(user_name char(18) not null,user_passwd char(50) default'',primary key (user_name));
Query OK, 0 rows affected (0.02 sec)

MariaDB [auth]> alter table auth.users engine=innodb;

Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

begin实例:

MariaDB [auth]> begin ;                          //事物开始

Query OK, 0 rows affected (0.00 sec)

MariaDB [auth]> insert into users values('lisi',password('123'));
Query OK, 1 row affected (0.01 sec)

MariaDB [auth]> insert into users values('wangwu',password('321'));
Query OK, 1 row affected (0. 00 sec)

MariaDB [auth]> commit;                           //事物提交
Query OK, 0 rows affected (0.00 sec)

MariaDB [auth]> select * from users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| wangwu | *7297C3E22DEB91303FC493303A8158AD4231F486 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

里面存储的数据是永久的事物

rollback实例:

MariaDB [auth]> begin;
Query OK, 0 rows affected (0.00 sec)

MariaDB [auth]> update users set user_passwd=password('') where user_name='lisi';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [auth]> select * from users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | |
| wangwu | *7297C3E22DEB91303FC493303A8158AD4231F486 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

MariaDB [auth]> rollback;                            //事物回滚(撤销)从begin开始的所有的命令都将被撤销
Query OK, 0 rows affected (0.01 sec)

MariaDB [auth]> select * from users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| wangwu | *7297C3E22DEB91303FC493303A8158AD4231F486 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

2、使用set设置事务处理方式

set autocommit=0:禁止自动提交

set autocommit=1:开启自动提交

MariaDB [auth]> set autocommit=0;                               //禁止自动提交
Query OK, 0 rows affected (0.00 sec)

MariaDB [auth]> insert into users values('lty',password('123'));
Query OK, 1 row affected (0.00 sec)

MariaDB [auth]> insert into users values('jhc',password('456'));
Query OK, 1 row affected (0.00 sec)

MariaDB [auth]> commit;
Query OK, 0 rows affected (0.01 sec)

MariaDB [auth]> set autocommit=1;                             //开启自动提交
Query OK, 0 rows affected (0.00 sec)

不能进行回滚,一条SQL语句就会提交一次