2013-02-11 134 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) 

改爲試試。

相關問題