今天遇到一個要刪除外鍵列的問題,我發現:
Sql代碼
show create table userfans;
Java代碼
| 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`),
<span style="color: #ff0000;">KEY</span>
`<span style="color: #ff0000;"><strong>FKF025E60B81E8743E</strong>
</span>
` (`fllower`),
KEY `FKF025E60BAFF2360E` (`user`),
KEY `FKF025E60BC1B86A61` (`follower`),
<span style="color: #ff0000;">CONSTRAINT</span>
`<strong><span style="color: #ff0000;">FKF025E60B81E8743E</span>
</strong>
` <span style="color: #ff0000;">FOREIGN</span>
<span style="color: #ff0000;">KEY</span>
(`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的;結果試了半天一直失敗,最後發現,刪除外鍵列竟然出奇的簡單:
Sql代碼
alter table userfans drop foreign key <strong><span style="color: #ff0000;">FKF025E60B81E8743E</span>
</strong>
;
顯示:
Sql代碼
Query OK, 2 rows affected (0.32 sec)
Records: 2 Duplicates: 0 Warnings: 0
真的是踏破鐵鞋,白費功夫!
作者“絕塵”