MySQL5添加复合外键失败

发布于 2024-11-19 13:38:17 字数 1566 浏览 2 评论 0原文

我试图强制用户地址信息中的州/省和国家/地区名称来自一组表格,其中列出了国家/地区和州/省。为了做到这一点,我尝试运行像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2024-11-26 13:38:17

引用的表中没有索引,其中引用的列显示为第一列。

您需要 StateProvince.(name,countryName) 上的索引,但您在 StateProvince.(countryName,name) 上有索引。尝试颠倒索引或 FK 引用的顺序。

There is no index in the referenced table where the referenced columns appear as the first columns.

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.

甜妞爱困 2024-11-26 13:38:17

由于您没有显示创建语句,我不确定,但我相信这与没有主键的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文