事务具有四个特征ACID
原子性(Atomicity),事务是最小单元,不可再分;
一致性(Consistency),事务要求所有的DML语句操作的时候,必须保证同时成功或同时失败;
隔离性(Isolation),一个事务不会影响其他事务的执行;
持久性(Durability),在事务完成之后,该事务对数据库所作的更改将持久地保存在数据库中,并不会被回滚;
MySQL事务的提交和回滚
查看事务是否自动提交
show variables like '%commit%';
查看结果
+--------------------------------+-------+
| Variable_name | Value |
+--------------------------------+-------+
| autocommit | ON | #自动提交
| binlog_order_commits | ON |
| innodb_api_bk_commit_interval | 5 |
| innodb_commit_concurrency | 0 |
| innodb_flush_log_at_trx_commit | 1 |
+--------------------------------+-------+
开启事务,可关闭自动提交
start transaction;
例:
Step1:初始化数据
create table t_bank(