简体   繁体   English

MYSQL外键Errno(150)无法创建表

[英]MYSQL Foreign Key Errno (150) Can't create table

I just started learning MYSQL in college and I have an important assignment to do for my class. 我刚开始在大学里学习MYSQL,并且我有一个重要的任务要完成。 I have to create a small database and I can't seem to add a table with foreign keys because of the errno(150) Here's what I have. 我不得不创建一个小型数据库,由于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));

I cant seem to get the Member table to be created, It gives this error, ERROR 1005 (HY000): Can't create table 'iicp.member' (errno: 150) Thanks in advance for the help, I really need to solve this 我似乎无法获得要创建的Member表,它给出了此错误,错误1005(HY000):无法创建表iicp.member(errno:150)在此先感谢您的帮助,我真的需要解决这个

here the working query 这是工作查询

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 在此处演示SQLFIDDLE

You SQL is correct. 您的SQL是正确的。 It worked for me with change the following change: 它通过以下更改为我工作:

OrgTelNo.varchar (30) to OrgTelNo varchar (30)
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))

...

You referred column named InterestgrpName in table InterestGroup but actual name of column is IntrestgrpName 您在表InterestGroup中引用了名为InterestgrpName的列,但该列的实际名称是IntrestgrpName

Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),

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

OrgTelNo.varchar (30),

Instead of this you should write 代替这个,你应该写

OrgTelNo varchar (30),

And also it is a spell mistake in creation of last table member. 这也是最后一个表成员创建中的一个拼写错误。

FOREIGN KEY ( IntrestgrpName ) REFERENCES InterestGroup ( IntrestgrpName)

Try this out instead. 尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM