赞
踩
今天小编在用mysql数据库去删除一个表的字段时遇到了Cannot drop index 'PK_school_province': needed in a foreign key constraint问题,如下将表的创建贴出来:
CREATE TABLE `school` (
`school_id` int(11) NOT NULL AUTO_INCREMENT,
`school_name` varchar(255) NOT NULL,
`school_pro_id` int(11) NOT NULL,
PRIMARY KEY (`school_id`),
KEY `PK_school_province` (`school_pro_id`),
CONSTRAINT `PK_school_province` FOREIGN KEY (`school_pro_id`) REFERENCES `province` (`province_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2735 DEFAULT CHARSET=utf8
大家都知道KEY与index的意思相近,都是创建索引,现在使用coinstraint来将对字段进行限制,将索引PK_school_province 与外键school_pro_id与外表province表的province_id限制关联(constraint也有索引的意思,on面跟语句),那么现在我想直接删除school_pro_id这个字段,这时,直接使用"alter table school drop column school_pro_id"会出现Cannot drop index 'PK_school_province': needed in a foreign key constraint;大意是不能删除被外键限制的索引PK_school_province。那么这个时候我们需要将外键限制的索引给删除掉,使用”alter table school drop foreign key PK_school_province“就能将PK_school_province删除掉,那么这个时候再使用“alter table school drop column school_pro_id”就可以将school表中的school_pro_id删除掉。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。