当前位置:Gxlcms > mysql > Mysql中使用事宜

Mysql中使用事宜

时间:2021-07-01 10:21:17 帮助过:45人阅读

Mysql中使用事务 1.创建表 create table account( id int primary key auto_increment, name varchar(20), money double ); insert into account values(null,'aaa',1000); insert into account values(null,'bbb',1000); insert into account values(null,'c

Mysql中使用事务
1.创建表
create table account(
id int primary key auto_increment,
name varchar(20),
money double
);

insert into account values(null,'aaa',1000);
insert into account values(null,'bbb',1000);
insert into account values(null,'ccc',1000);

2、MySQL中事务默认自动提交的,每当执行一条SQL,就会提交一个事务 (一条SQL 就是一个事务)
Oracle 中事务默认 不自动提交,需要在执行SQL 语句后 通过commint 手动提交事务

3、mysql管理事务
方式一 :同时事务管理SQL 语句
start transaction 开启事务
rollback 回滚事务 (将数据恢复到事务开始时状态)
commit 提交事务 (对事务中进行操作,进行确认操作,事务在提交后,数据就不可恢复)

方式二:数据库中存在一个自动提交变量 ,通过 show variables like '%commit%'; ---- autocommint 值是 on,说明开启自动提交
关闭自动提交 set autocommit = off / set autocommit = 0
如果设置autocommit 为 off,意味着以后每条SQL 都会处于一个事务中,相当于每条SQL执行前 都执行 start transaction

人气教程排行