我試着用下面的CREATE TABLE語句在MySQL中創建一個表:MySQL的:無法創建表
CREATE TABLE `visit` (
`visit_id` int(11) NOT NULL,
`site_id` int(11) DEFAULT NULL,
PRIMARY KEY (`visit_id`),
CONSTRAINT `FK_visit_site` FOREIGN KEY (`site_id`) REFERENCES `site` (`site_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我收到此錯誤:
ERROR 1005 (HY000): Can't create table 'fooschema.visit' (errno: 121)
我用SHOW ENGINE INNODB STATUS命令。這是錯誤消息:
------------------------ LATEST FOREIGN KEY ERROR ------------------------ 140222 7:03:17 Error in foreign key constraint creation for table `fooschema`.`visit`. A foreign key constraint of name `fooschema/FK_visit_site` already exists. (Note that internally InnoDB adds 'databasename/' in front of the user-defined constraint name). Note that InnoDB's FOREIGN KEY system tables store constraint names as case-insensitive, with the MySQL standard latin1_swedish_ci collation. If you create tables or databases whose names differ only in the character case, then collisions in constraint names can occur. Workaround: name your constraints explicitly with unique names.
然後,我用下面的查詢列出所有可用的限制:
select *
from information_schema.table_constraints
where constraint_schema = 'fooschema'
我沒有看到任何約束名爲「FK_visit_site」的結果。
FK_visit_site約束是表訪問的外鍵約束。我放棄了訪問表並嘗試重新創建它。
有沒有辦法可以放棄這個外鍵約束,即使它關聯的表不存在?
你可以只是給你的新密鑰一個不同的名字? – Nanne
是的,我可以。但我真的很想用舊的名字。使用舊名稱很重要。 – toanong
什麼'select * from information_schema。table_constraints其中constraint_name ='FK_visit_site''返回' –