2013-02-11 133 views
0

我刚开始在大学学习MYSQL,我有一个重要的任务是为我的课程做。我必须创建一个小型数据库,因为errno(150) 我似乎无法添加带有外键的表。MYSQL外键Errno(150)无法创建表

create table Country 
(CountryName varchar (50) not null, 
Primary Key (CountryName)); 

create table InterestGroup 
(IntrestgrpName varchar (30) not null, 
Primary Key (IntrestgrpName)); 

create table Organisation 
(OrgName varchar (50) not null, 
OrgAddress varchar (30), 
OrgTelNo.varchar (30), 
Primary Key (OrgName)); 

create table Qualification 
(QualName varchar (50) not null, 
Primary Key (QualName)); 

create table Member 
(MemberID varchar (15) not null, 
MemberName varchar (30), 
MemberAdd varchar (50) not null, 
CountryName varchar (50) not null, 
IntrestgrpName varchar (30) not null, 
QualName varchar (50) not null, 
OrgName varchar (50) not null, 
Primary Key (MemberID), 
Foreign Key (CountryName) References Country (CountryName), 
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName), 
Foreign Key (QualName) References Qualification (Qualname), 
Foreign Key (OrgName) References Organisation (OrgName)); 

我不能似乎得到将要创建的成员表,它给这个错误, ERROR 1005(HY000):无法创建表 'iicp.member'(错误:150) 在此先感谢对于帮助,我真的需要解决这个问题

+0

您是否使用InnoDB或MyISAM作为引擎类型?我认为只有InnoDB支持外键。如果您不拥有服务器本身,则可能需要向DBA询问这些信息。 – Lukos 2013-02-11 14:03:25

+0

你确定你想要MemberID varchar数据类型吗? – 2013-02-11 14:07:48

回答

2

这里工作查询

create table Country 
(CountryName varchar (50) not null, 
Primary Key (CountryName)); 

create table InterestGroup 
(IntrestgrpName varchar (30) not null, 
Primary Key (IntrestgrpName)); 

create table Organisation 
(OrgName varchar (50) not null, 
OrgAddress varchar (30), 
OrgTelNo varchar (30), 
Primary Key (OrgName)); 

create table Qualification 
(QualName varchar (50) not null, 
Primary Key (QualName)); 

create table Member 
(MemberID varchar (15) not null , 
MemberName varchar (30), 
MemberAdd varchar (50) not null, 
CountryName varchar (50) not null, 
IntrestgrpName varchar (30) not null, 
QualName varchar (50) not null, 
OrgName varchar (50) not null, 
Primary Key (MemberID), 
Foreign Key (CountryName) References Country (CountryName), 
Foreign Key (IntrestgrpName) References InterestGroup (IntrestgrpName), 
Foreign Key (QualName) References Qualification (Qualname), 
Foreign Key (OrgName) References Organisation (OrgName)); 

DEMO HERE SQLFIDDLE

+0

您在最后一个表格定义中将引用列重命名为 – rkosegi 2013-02-11 14:16:16

+0

这已起作用!非常感谢您的帮助! – 2013-02-11 15:49:48

+0

不错,它为你工作! ,确保接受答案,帮助他人作为有用的答案。 – 2013-02-11 16:54:46

1

你的SQL是正确的。 它的工作对我的变化如下变化:

OrgTelNo.varchar (30) to OrgTelNo varchar (30) 
+0

你有一双好眼睛,老兄! – 2013-02-11 14:11:19

+0

谢谢,不知道这段时间会导致表不能进入,但是这样的错误不会影响组织表的输入吗? – 2013-02-11 15:53:08

0
SHOW ENGINE INNODB STATUS; 


... 

------------------------ 
LATEST FOREIGN KEY ERROR 
------------------------ 
130211 15:09:26 Error in foreign key constraint of table test/member: 
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName), 
Foreign Key (QualName) References Qualification (Qualname), 
Foreign Key (OrgName) References Organisation (OrgName)): 
Cannot resolve column name close to: 
), 
Foreign Key (QualName) References Qualification (Qualname), 
Foreign Key (OrgName) References Organisation (OrgName)) 

... 

你提到的表InterestGroup但列的实际名称命名InterestgrpName列为IntrestgrpName

Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName), 

You have type error here -----------------------------------^ 
0

OrgTelNo.varchar (30),

取而代之的是,你应该写

OrgTelNo varchar (30),

而且也正是在创作最后表成员的法术错误。

FOREIGN KEY (IntrestgrpName) REFERENCES InterestGroup (IntrestgrpName) 

改为试试。

相关问题