2014-02-22 79 views
4

我试着用下面的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约束是表访问的外键约束。我放弃了访问表并尝试重新创建它。

有没有办法可以放弃这个外键约束,即使它关联的表不存在?

+0

你可以只是给你的新密钥一个不同的名字? – Nanne

+0

是的,我可以。但我真的很想用旧的名字。使用旧名称很重要。 – toanong

+0

什么'select * from information_schema。table_constraints其中constraint_name ='FK_visit_site''返回' –

回答

1

你的外键已经存在,所以要么放弃外键或重命名你的第二个键。

ALTER TABLE `site` DROP FOREIGN KEY `FK_visit_site`; 

或重命名为其他新的。

CREATE TABLE `visit` (
`visit_id` int(11) NOT NULL PRIMARY KEY, 
`site_id` int(11) NOT NULL, 
CONSTRAINT `FK_visit_site` FOREIGN KEY (`site_id`) REFERENCES `site` (`site_id`), 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

新增PRIMARY KEYvisit_id线。

注:

确保site_idsite表在访问表的site_id完全相同的数据类型。

`site_id` int(11) DEFAULT NULL --//in the `site` table 

The two keys you're coupling must have the exact same datatype (INT NOT NULL), even signedness

+0

在我的情况下,显式约束名称很重要,因为我可能需要稍后以编程方式将其删除。 – toanong

+0

刚刚尝试过。同样的错误。 – toanong

+0

尝试编辑后的版本。下面是现场表: CREATE TABLE'site'( 'site_id' INT(11)NOT NULL, 'location_id' INT(11)DEFAULT NULL, 'site_name' VARCHAR(50)DEFAULT NULL, PRIMARY KEY(' CONSTRAINT'FK_site_location' FOREIGN KEY('location_id')REFERENCES'location'('location_id') )ENGINE = InnoDB DEFAULT CHARSET = utf8; – toanong

0

据我所知,你会得到当你试图添加一个约束与已经用于其他地方的名称此错误。意思是,在你的情况下,FK FK_visit_site已经被使用过。

如果您尝试创建的表包含外键约束,并且您为该约束提供了自己的名称,请记住它在数据库中必须是唯一的。

您可以运行下面的查询,找出相同

SELECT 
    constraint_name, 
    table_name 
FROM 
    information_schema.table_constraints 
WHERE 
    constraint_type = 'FOREIGN KEY' 
    AND table_schema = DATABASE() 
ORDER BY 
    constraint_name; 

从这里后两者 http://www.thenoyes.com/littlenoise/?p=81

尝试使用不同的名称为您的FK喜欢

CREATE TABLE `visit` (
    `visit_id` int(11) NOT NULL, 
    `site_id` int(11) DEFAULT NULL, 
    PRIMARY KEY (`visit_id`), 
    CONSTRAINT `FK_visit_site_New` FOREIGN KEY (`site_id`) 
    REFERENCES `site` (`site_id`), 
) 
+1

约束名称不在您提到的查询结果中。我将不得不保留旧名称。我担心的是,如果这个名字被用在某个地方,它在哪里?为什么我找不到它? – toanong

+0

我在上面看到你的评论,你放弃了“添加约束”。你还在使用同一个会话吗?尝试运行我在不同会话中提到的查询,看看你是否发现它。 – Rahul

+1

刚在新会话中尝试了查询。仍然没有看到它。外键已添加到访问表中。现在访问表并不存在。外键如何仍然存在? – toanong