MySQL5添加复合外键失败
我试图强制用户地址信息中的州/省和国家/地区名称来自一组表格,其中列出了国家/地区和州/省。为了做到这一点,我尝试运行像这样的 alter table 命令...
ALTER TABLE User
ADD FOREIGN KEY (stateProvince,country)
REFERENCES `StateProvince`(`name`,`countryName`);
然后我收到此消息...
使用外键创建表“realtorprint_dev/#sql-d5c_3d” 约束失败。引用的表中没有索引 引用的列显示为第一列。
有人知道如何处理此错误消息吗?
这是州和国家/地区表的创建...
CREATE TABLE Country (
name varchar(40) NOT NULL,
abbreviation varchar(4) NOT NULL,
PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE StateProvince (
countryName varchar(40) NOT NULL,
name varchar(100) NOT NULL,
abbreviation varchar(3) NOT NULL,
PRIMARY KEY (countryName,name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table StateProvince
add constraint FK_StateProvince_Country
foreign key (countryName)
references Country (name);
现在是用户表...
create table realtorprint_dev.user (
id bigint not null,
created datetime,
email varchar(255) not null,
fax varchar(255),
mobile varchar(255),
name varchar(255),
password varchar(255),
phone varchar(255),
title varchar(255),
tollFree varchar(255),
updated datetime,
web varchar(255),
brokerage_id bigint,
address varchar(255),
city varchar(255),
country varchar(255),
stateProvince varchar(255),
type varchar(255),
zipPostal varchar(255),
activated bit not null,
locked bit not null,
primary key (id),
foreign key FK285FEB6722226 (brokerage_id) references brokerage(id)
);
I am trying to enforce that the state/province and country names in the address information for users comes from a set of tables where I list countries and state/provinces. In order to do this I tried running an alter table command like this...
ALTER TABLE User
ADD FOREIGN KEY (stateProvince,country)
REFERENCES `StateProvince`(`name`,`countryName`);
Then I get this message...
Create table 'realtorprint_dev/#sql-d5c_3d' with foreign key
constraint failed. There is no index in the referenced table where the
referenced columns appear as the first columns.
Does anybody have an idea how to handle this error message?
Here is the create for the state and country tables...
CREATE TABLE Country (
name varchar(40) NOT NULL,
abbreviation varchar(4) NOT NULL,
PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE StateProvince (
countryName varchar(40) NOT NULL,
name varchar(100) NOT NULL,
abbreviation varchar(3) NOT NULL,
PRIMARY KEY (countryName,name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table StateProvince
add constraint FK_StateProvince_Country
foreign key (countryName)
references Country (name);
And now for the user table...
create table realtorprint_dev.user (
id bigint not null,
created datetime,
email varchar(255) not null,
fax varchar(255),
mobile varchar(255),
name varchar(255),
password varchar(255),
phone varchar(255),
title varchar(255),
tollFree varchar(255),
updated datetime,
web varchar(255),
brokerage_id bigint,
address varchar(255),
city varchar(255),
country varchar(255),
stateProvince varchar(255),
type varchar(255),
zipPostal varchar(255),
activated bit not null,
locked bit not null,
primary key (id),
foreign key FK285FEB6722226 (brokerage_id) references brokerage(id)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要 StateProvince.(name,countryName) 上的索引,但您在 StateProvince.(countryName,name) 上有索引。尝试颠倒索引或 FK 引用的顺序。
You need an index on StateProvince.(name,countryName), but you have an index on StateProvince.(countryName,name). Try reversing the order of your index, or of your FK reference.
由于您没有显示创建语句,我不确定,但我相信这与没有主键的
realtorprint_dev
表有关。Since you didn't show the create statement i'm not sure, but I beleve this has to do with the
realtorprint_dev
table not having a primairy key.