MySQL 事务介绍及操做演示

1、数据库事务的概念mysql

事务是一种机制、一个操做序列,包含了一组数据库操做命令,而且把全部的命令做为一个总体一块儿向系统提交或撤销操做请求,即这一组数据库命令要么都执行,要么不都执行。linux

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

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

2、事务的ACID特色
事务具备四个属性:ACID
原子性(Atomicity)
一致性(Consistency)
隔高性(Isolation)
持久性(Durability)
一、原子性
事务是一个完整的操做,事务的各元素是不可分的(原子的),事务的全部元素必须做为一个总体提交或回滚。若是事务中的任何元素失败,则整个事务将失败。并发

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

三、隔离性
对数据进行修改的全部并发事务是彼此隔离的,这代表事务必须是独立的,它不该该以任何方式依赖于或影响其余事务。修改数据的事务能够在另外一个使用相同数据的事务开始以前访问这些数据,或者在另外一个使用相同数据的事务结束以后访问这些数据。this

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

3、事务的操做orm

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

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

一、事物处理命令控制

事物处理命令控制事物:

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)

二、使用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语句就会提交一次

相关文章
相关标签/搜索