执行语句:alter table test.sc drop primary key;
报错信息为:ERROR 1025 (HY000): Error on rename of '.\test\#sql-12fc_2b' to '.\test\sc' (errno: 150 - Foreign key constraint is incorrectly formed);
试了半天无果,然后我使用:show create table test.sc;
语句查看建表语句,结构如下:
CREATE TABLE `sc` (
`Sno` varchar(255) NOT NULL,
`Cno` varchar(255) NOT NULL,
`Score` float NOT NULL,
PRIMARY KEY (`Sno`) USING BTREE,
KEY `Cno` (`Cno`),
CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`Sno`) REFERENCES `student` (`Sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
原因找到,我想删除主键Sno,但是主键同时又是外键参照了student表的Sno列,所以我先将外键删除,再将主键删除。成功。
alter table test.sc drop foreign key sc_ibfk_1;
alter table test.sc drop primary key;
注意删除外键的时候用的是一个系统生成的别名。