事务语句
事务开启语句是由begin或start transaction(read write|read only)命令来开始的,或者把自提交特性关掉(执行set autocommit=0命令),
select * from test;
begin;
insert into test (id,name) values(4,’dd’);
commit;
select * from test;
begin;
insert into test(id,name) values(5,’ee’);
rollback;
select * from test;
隐式提交:
DDL语句的操作或再次输入begin和start transaction命令,隐式回滚可以退出会话、连接超时或关机等:
select * from test;
begin
insert into t(id,name) values(5,’ee’);
create table tt(id int);
select * from test; (有数据);
在DDL语句中默认自带一个commit;
隐匿回滚:select * from test;
begin;
insert into test(id,name) values(66,’dd’);
exit
再查询未提交有此记录
truncate和delete的区别:
truncate是DDL语句操作,delete是DML语句操作,truncate在事务中不能被回滚,且会清空表的自增属性,都是清空表的数据。
事务的隔离级:
目前mysql默认隔离级是可重复读(repeatable_read)
show variables like '%tx_isolation%';
show variables like '%iso%';
select * from t;
begin;
insert into t (name,city) values (‘f’,’tj’);
commit;
begin; (会话2)
select* from t;
select * from t for update; for update可以查到最新数据版本号的记录,或者执行commit操作。