错误信息:Cannot drop index 'energy_type_id': needed in a foreign key constraint
创建device表的信息
CREATE TABLE `device` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `device_type_id` int(16) NOT NULL DEFAULT '2', `energy_type_id` int(11) NOT NULL DEFAULT '1' , PRIMARY KEY (`id`), KEY `energy_type_id` (`energy_type_id`) USING BTREE, CONSTRAINT `device_ibfk_1` FOREIGN KEY (`energy_type_id`) REFERENCES `energy_type` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8 COMMENT='设备';
问题分析:使用coinstraint来将对字段进行限制,将索引device_ibfk_1与外键energy_type_id与外表energy_type表的id限制关联(constraint也有索引的意思,on面跟语句)
1.在navicat中直接删除外键,索引字段,会报错,
2.直接使用alter table device drop column energy_type_id
解决方案:1.先解除限制alter table device drop foreign key device_ibfk_1
2.删除字段alter table device drop column energy_type_id