1. create table account(
  2. id int primary key,
  3. name varchar(10),
  4. balance decimal(18,2)
  5. );
  6. insert into account values(1,'刘备',1000);
  7. insert into account values(2,'张飞',1);
  8. select * from account;

标准表的创建和插入

  1. drop table if exists account;
  2. create table account(
  3. id int primary key auto_increment,
  4. username varchar(20),
  5. balance double
  6. );
  7. insert into account(username,balance) values('张无忌',1000),('赵敏',1000);

事务的使用步骤

  1. --查看事务是否关闭
  2. show variables like 'autocommit';
  3. --查看正在执行的事物
  4. select * from information_schema.innodb_trx \G;
  5. select * from information_schema.innodb_locks \G;
  6. # 查看锁等待超时时间
  7. show variables like '%innodb_lock_wait%';
  8. # 死锁保护,避免死锁。如果超过一定时间自动结束报错
  9. show variables like '%innodb_deadlock%'; -- 如果是全程就不必加'',因为有variables指明
  10. # 死锁关闭
  11. set global innodb_deadlock_detect = off;
  12. # 设置时长
  13. set '%innodb_lock_wait%' = 600;

innodb_deadlock_detect死锁是全局变量

  1. -- 回滚
  2. rollback;
  1. -- 开启事务
  2. set autocommit = 0;或者begin;
  3. # 可以用name,也可以用1或者'1',
  4. -- 编写一组事务语句
  5. update account set balance = balance-500 where username='张无忌';
  6. update account set balance = balance+500 where username='赵敏';
  7. -- 结束事务
  8. commit;

事务没有结束之前张无忌和赵敏都是1000
事务结束之后张无忌是500,赵敏是1500