DELIMITER $$
use test$$
drop procedure if exists test.t1$$
use `test` $$
create procedure test.t1()
begin
drop table if exists t1;
create table test.t1 (c1 int)
ENGINE = InnoDB;
-- set autocommit=0; 这里设置为手动提交,或者下面开启事务,在这种情况下 rollback都有效。
START TRANSACTION;
insert into test.t1 select 1;
select * from test.t1;
rollback;
select 2,c1 from test.t1;
insert into test.t1 select 2;
commit;
select 3,c1 from test.t1;
insert into test.t1 select 3;
select 4,c1 from test.t1;
end
-- 测试
call test.t1();
本文通过创建存储过程演示了MySQL中如何使用事务来管理数据操作。首先创建了一个名为t1的表,并通过START TRANSACTION开始事务,接着插入一条记录并进行回滚操作,展示了数据未被实际保存的状态;再次插入数据后提交事务,验证了数据持久化的过程。
608

被折叠的 条评论
为什么被折叠?



