【案例】:测试提交事务和回滚事务
drop table if exists dept_bak ;
create table dept_bak(
DEPTNO int(2) not null ,
DNAME varchar(14) ,
LOC varchar(13) ,
primary key (DEPTNO)
);
select * from dept_bak ; // 记为表1
start transaction ;
insert into dept_bak (deptno , dname , loc) values (10 , 'SALES' , 'SHANDONG');
insert into dept_bak (deptno , dname , loc) values (20 , 'SALES' , 'SHANDONG');
insert into dept_bak (deptno , dname , loc) values (30 , 'SALES' , 'SHANDONG');
insert into dept_bak (deptno , dname , loc) values (40 , 'SALES' , 'SHANDONG');
select * from dept_bak ; // 记为表2 , 查看与表1的区别
rollback ;
select * from dept_bak ; // 记为表3 , 查看与表2的区别
insert into dept_bak (deptno , dname , loc) values (10 , 'SALES' , 'SHANDONG');
insert into dept_bak (deptno , dname , loc) values (20 , 'SALES' , 'SHANDONG');
insert into dept_bak (deptno , dname , loc) values (30 , 'SALES' , 'SHANDONG');
insert into dept_bak (deptno , dname , loc) values (40 , 'SALES' , 'SHANDONG');
select * from dept_bak ;
commit;
rollback ;
select * from dept_bak ; // 记为表4 , 查看与表2,3的区别
【结论】:rollback可以回滚到上一次的commit处
无论是提交事务还是回滚事务都会结束事务