日期:2014-05-17 浏览次数:20530 次
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`gid` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `gid` (`gid`),
CONSTRAINT `FK_GID` FOREIGN KEY (`gid`) REFERENCES `grade` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for grade
-- ----------------------------
DROP TABLE IF EXISTS `grade`;
CREATE TABLE `grade` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` bigint(10) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`age` varchar(10) DEFAULT NULL,
`cid` bigint(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `FK_CID` (`cid`),
CONSTRAINT `FK_CID` FOREIGN KEY (`cid`) REFERENCES `class` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
mysql> set autocommit=0;
mysql> set autocommit=0;
mysql> delete student from student inner join class on student.cid=class.id where class.gid=2;
insert into class(name,gid) values('calss4',1);
mysql> commit;
mysql> commit;
delete.. from.. inner join...操作时,是不是class 表在锁表?依这种情况来看,是锁表现象。 那么如果真的锁表了,有没有什么方法能够避免这种锁表呢?
select id from class where class.gid=2查出来的数据,放入临时表temp_table中,然后
delete student from student inner join temp_table on student.cid=temp_table.id;这样可以不锁 class 表。有没有更好的方法呢?或者说我这个思路来就就是错的。请大家给我点建议,感激感激!