今天遇到一個要刪除外鍵列的問題,我發現:
show create table userfans;
| userfans | CREATE TABLE `userfans` ( `id` int(11) NOT NULL AUTO_INCREMENT, `status` int(11) DEFAULT NULL, `update_time` datetime DEFAULT NULL, `user` int(11) DEFAULT NULL, `fllower` int(11) DEFAULT NULL, `follower` int(11) DEFAULT NULL, PRIMARY KEY (`id`),KEY 'FKF025E60B81E8743E` (`fllower`), KEY `FKF025E60BAFF2360E` (`user`), KEY `FKF025E60BC1B86A61` (`follower`),CONSTRAINT `FKF025E60B81E8743E` FOREIGN KEY(`fllower`) REFERENCES `user` (`id`), CONSTRAINT `FKF025E60BAFF2360E` FOREIGN KEY (`user`) REFERENCES `user` (`id`), CONSTRAINT `FKF025E60BC1B86A61` FOREIGN KEY (`follower`) REFERENCES `user` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
剛開始我想:是不是先刪除KEY,再刪除constraint,最後刪除列?因為MySQL的外鍵列和Oracle不一樣,MySQL的都是有KEY的;結果試了半天一直失敗,最後發現,刪除外鍵列竟然出奇的簡單:
alter table userfans drop foreign key FKF025E60B81E8743E;
顯示:
Query OK, 2 rows affected (0.32 sec)Records: 2 Duplicates: 0 Warnings: 0
真的是踏破鐵鞋,白費功夫!