使用外键错误号:150创建sql表

时间:2013-11-09 09:03:57

标签: mysql sql database foreign-keys

我尝试用2个外键创建一个表,但是当我尝试将其写入mysql时,得到一个错误说“ERROR 1005(HY000):无法创建表(错误号:150)”是否存在有人帮我吗?这是我的表:

  create table store (
  StoreName varchar(255),
  Category varchar(20),
  primary key(StoreName,Category));




 create table shopps (
 StoreName varchar(255),
 Category varchar(20),
 primary key(StoreName,Category),
 foreign key(StoreName) References store);
 foreign key(Category) References store);

1 个答案:

答案 0 :(得分:0)

由于您的store表具有复合主键,因此在shopps表中引用它的外键也必须是复合的:

create table shopps (
    StoreName varchar(255),
    Category varchar(20),
    primary key(StoreName,Category),
    foreign key(StoreName,Category) References store (StoreName,Category)
)

Demo on sqlfiddle.