Data Definition Language
)数据库定义语言 建表, 建库, 修改表结构Data Manipulation Language
)数据操纵语言 增删改查, select, insert, update, deleteData Control Language
)数据库控制语言 修改用户权限, 重置密码Transaction Control Language
)事务控制语言 建立事务, 回滚一组SQL语句组成的执行单元, 这一组SQL语句, 要么所有执行, 要么所有不执行.
复制代码
举例sql
事务:数据库
一个不可分割的总体
回滚
举例2:session
下一步
, 若是点击取消
, 则所有回滚
.表类型
)表类型
):memory
)。事务处理
的数据库(以确保事务处理不成功时数据的回退能力), 那就用innodb
。存储引擎
(也称做表类型
)。create table tb (id int) engine = myisam;
-- 或者
alter table tb engine = memory;
复制代码
show engines;
复制代码
ACID
)原子性 atomicity
并发
一致性 consistency
oop
能量守恒定律
事务
)先后, 郭靖, 黄蓉的银行卡余额之和是固定不变的.隔离性(isolation
)post
持久性(durability
)性能
事务一般包括多条SQL语句(DML), 其实单独的DML语句, 也是一个事务测试
隐式事务(自动提交)ui
insert
,update
,delete
显式事务atom
set autocommit = 0;
show variables like 'autocommit';
-- 或者
select @@autocommit;
复制代码
-- 当前会话有效
set autocommit = 0;
-- 或者
set session autocommit = 0;
-- 或者
set @@autocommit = 0;
-- 或者
set @@session.autocommit = 0;
复制代码
开启事务
set autocommit = 0;
start transaction; // 可选
复制代码
编写事务中的sql语句(select
, insert
, update
, delete
) 不包括DDL(create
, drop
, alter
)
结束事务
commit
rollback
drop table if exists test_tb;
CREATE TABLE `test_tb` (
`id` int(5) unsigned NOT NULL,
`age` tinyint(5) unsigned NOT NULL,
`account` int(11) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=innodb DEFAULT CHARSET=utf8;
INSERT INTO `test_tb`(`id`, `age`, `account`, `name`) VALUES (1, 11, 5000, '张三');
INSERT INTO `test_tb`(`id`, `age`, `account`, `name`) VALUES (2, 12, 5000, '李四');
复制代码
set autocommit = 0;
start TRANSACTION;
update test_tb set account = account - 4000 where name = '张三';
update test_tb set account = account + 4000 where name = '李四';
commit;
复制代码
set autocommit = 0;
start TRANSACTION;
update test_tb set account = account - 4000 where name = '张三';
update test_tb set account = account + 4000 where name = '李四';
rollback;
复制代码
对于同时运行的多个事务, 若是没有采用必要的隔离机制, 就会致使各类并发问题
更新可是尚未被提交
的字段以后插入/删除
了一些新的行以后, 若是T1再次读取同一个表, 就会多出/少了几行.幻觉
...更新
了该字段并提交, T1再次读取同一个字段, 值就不一样了.一次只作一件事, 没有其余事情的干扰, 确定不容易出错, 可是效率也会比较低
read uncommitted
)
脏读
, 不可重复读
和幻读
, 都会出现read commited
)
不可重复读
和幻读
问题仍然可能出现repeatable read
)
脏读
和不可重复读
, 可是幻读
问题让然存在serializable
)
全部并发问题均可以免
MySQL支持以上所有四种事务隔离级别, 默认 repeatable read
(可重复读)
select @@tx_isolation;
-- 或者
show variables like "tx_isolation";
复制代码
set session transaction isolation level repeatable read; // 当前会话
-- 或者
set global transaction isolation level repeatable read; // 全局
复制代码
感兴趣的小伙伴们, 能够测试一下各个隔离级别的不一样 下面是测试须要时, 用到的数据
DROP TABLE IF EXISTS test;
CREATE TABLE test (
id INT auto_increment PRIMARY KEY,
NAME VARCHAR ( 10 ) NOT NULL,
account INT ( 11 ) NOT NULL,
age TINYINT ( 1 ) NOT NULL,
sex CHAR ( 1 ) NOT NULL DEFAULT '男'
);
INSERT INTO test ( NAME, account, age )
VALUES
( '张三', 3000, 18 ),
( '李四', 4000, 28 ),
( '王五', 5000, 38 ),
( '赵六', 6000, 48 ),
( '孙七', 2000, 19 ),
( '周八', 1000, 29 ),
( '吴老九', 9000, 39 ),
( '冯老十', 8000, 49 );
复制代码
脏读、不可重复读、幻读的级别高低是:
脏读 < 不可重复读 < 幻读
。
因此,设置了最高级别的serializable
就不用在设置repeatable read
和read committed
了
savepoint
)就像玩游戏时的
存盘点
, 若是游戏人物死了, 就会在存盘点复活
直接上例子, 以上面的数据为例
set autocommit = 0;
start TRANSACTION;
update test set account = 9999 where id = 1;
SAVEPOINT a;
update test set account = 9999 where id = 2;
rollback to a;
select * from test;
复制代码