Ошибка внешнего ключа MYSQL (150) Не удается создать таблицу

Я только начал изучать MYSQL в колледже, и у меня есть важное задание для моего класса. Мне нужно создать небольшую базу данных, и я не могу добавить таблицу с внешними ключами из-за ошибки (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));

Кажется, я не могу создать таблицу Member, выдает эту ошибку, ОШИБКА 1005 (HY000): не удается создать таблицу «iicp.member» (errno: 150). Заранее спасибо за помощь, мне действительно нужно решить это


person KidDamien Kevon Daniel    schedule 11.02.2013    source источник
comment
Используете ли вы InnoDB или MyISAM для типа двигателя? Я думаю, что только InnoDB поддерживает внешние ключи. Если вы не являетесь владельцем самого сервера, возможно, вам придется запросить эту информацию у администратора баз данных.   -  person Lukos    schedule 11.02.2013
comment
Вы уверены, что хотите тип данных MemberID varchar?   -  person Bhavik Shah    schedule 11.02.2013


Ответы (4)


вот рабочий запрос

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

person echo_Me    schedule 11.02.2013
comment
Вы переименовали ссылочный столбец в последнем определении таблицы - person rkosegi; 11.02.2013
comment
хорошо, что это сработало для вас! , обязательно примите ответ, чтобы помочь другим как полезный ответ. - person echo_Me; 11.02.2013

Ваш SQL правильный. У меня это сработало с изменением следующего изменения:

OrgTelNo.varchar (30) to OrgTelNo varchar (30)
person Ravi Maggon    schedule 11.02.2013
comment
Спасибо, не знал, что период приведет к тому, что таблица не будет введена, но разве такая ошибка не повлияет только на ввод таблицы «Организация»? - person KidDamien Kevon Daniel; 11.02.2013

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

...

Вы указали столбец с именем InterestgrpName в таблице InterestGroup, но фактическое имя столбца IntrestgrpName

Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),

You have type error here -----------------------------------^
person rkosegi    schedule 11.02.2013

OrgTelNo.varchar (30),

Вместо этого вы должны написать

OrgTelNo varchar (30),

А также это орфографическая ошибка при создании последнего члена таблицы.

FOREIGN KEY ( IntrestgrpName ) REFERENCES InterestGroup ( IntrestgrpName)

Вместо этого попробуйте это.

person kewal    schedule 11.02.2013