--主表 CREATE TABLE `wwj`.`t1` ( `deptno` INT NOT NULL, `deptname` VARCHAR(45) NOT NULL, `address` VARCHAR(45) NOT NULL, PRIMARY KEY (`deptno`));
--子表 CREATE TABLE `wwj`.`t2` ( `empno` INT NOT NULL, `empname` VARCHAR(45) NOT NULL, `age` INT NOT NULL, `deptno` INT NOT NULL, PRIMARY KEY (`empno`), INDEX `deptno_idx` (`deptno` ASC), CONSTRAINT `deptno` FOREIGN KEY (`deptno`) REFERENCES `wwj`.`t1` (`deptno`) ON DELETE NO ACTION ON UPDATE NO ACTION);
insert into wwj.t1 values(1,'it','北京'); insert into wwj.t1 values(2,'product','天津'); insert into wwj.t1 values(3,'haha','上海'); mysql innodb lock机制原理
五.一条sql的加锁范围 对于各种情况下加锁的分析 mysql> show full processlist; +----+------+-----------+------+---------+------+----------+-----------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+----------+-----------------------+ | 11 | root | localhost | NULL | Sleep | 99 | | NULL | | 12 | root | localhost | NULL | Sleep | 81 | | NULL | | 13 | root | localhost | NULL | Query | 0 | starting | show full processlist | +----+------+-----------+------+---------+------+----------+-----------------------+ 杀掉线程: KILL [CONNECTION | QUERY] thread_id kill 13 --杀掉线程连接 kill query 13 --杀掉正在执行的语句,保留连接
mysql> show engine innodb status/G; ------------ TRANSACTIONS ------------ Trx id counter 1296 Purge done for trx's n:o < 1294 undo n:o < 0 state: running but idle History list length 2 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 421324408397424, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 1295, ACTIVE 396 sec inserting mysql tables in use 1, locked 1 LOCK WAIT 2 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1 MySQL thread id 12, OS thread handle 139848225883904, query id 61 localhost root executing insert into wwj.t3 select 4,20 ------- TRX HAS BEEN WAITING 10 SEC FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 26 page no 4 n bits 72 index idx-1 of table `wwj`.`t3` trx id 1295 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 4; hex 8000001e; asc ;; 1: len 4; hex 80000003; asc ;;
--------------------- TRANSACTION 1294, ACTIVE 449 sec 4 lock struct(s), heap size 1136, 3 row lock(s) MySQL thread id 11, OS thread handle 139848226150144, query id 50 localhost root Trx read view will not see trx with id >= 1294, sees < 1294