create table account(id int primary key,name varchar(10),balance decimal(18,2));insert into account values(1,'刘备',1000);insert into account values(2,'张飞',1);select * from account;
标准表的创建和插入
drop table if exists account;create table account(id int primary key auto_increment,username varchar(20),balance double);insert into account(username,balance) values('张无忌',1000),('赵敏',1000);
事务的使用步骤
--查看事务是否关闭show variables like 'autocommit';--查看正在执行的事物select * from information_schema.innodb_trx \G;select * from information_schema.innodb_locks \G;# 查看锁等待超时时间show variables like '%innodb_lock_wait%';# 死锁保护,避免死锁。如果超过一定时间自动结束报错show variables like '%innodb_deadlock%'; -- 如果是全程就不必加'',因为有variables指明# 死锁关闭set global innodb_deadlock_detect = off;# 设置时长set '%innodb_lock_wait%' = 600;
innodb_deadlock_detect死锁是全局变量
-- 回滚rollback;
-- 开启事务set autocommit = 0;或者begin;# 可以用name,也可以用1或者'1',-- 编写一组事务语句update account set balance = balance-500 where username='张无忌';update account set balance = balance+500 where username='赵敏';-- 结束事务commit;
事务没有结束之前张无忌和赵敏都是1000
事务结束之后张无忌是500,赵敏是1500
