0

我刚开始在大学学习 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): Can't create table 'iicp.member' (errno: 150) 在此先感谢您的帮助,我真的需要解决这个

4

4 回答 4

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));

在这里演示 SQLFIDDLE

于 2013-02-11T14:14:59.440 回答
1

你的 SQL 是正确的。通过更改以下更改对我有用:

OrgTelNo.varchar (30) to OrgTelNo varchar (30)
于 2013-02-11T14:06:19.243 回答
0

OrgTelNo.varchar (30),

而不是这个,你应该写

OrgTelNo varchar (30),

在创建最后一个表成员时也是拼写错误。

FOREIGN KEY ( IntrestgrpName ) REFERENCES InterestGroup ( IntrestgrpName)

试试这个。

于 2013-02-11T14:16:50.187 回答
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 -----------------------------------^
于 2013-02-11T14:10:48.510 回答