繁体   English   中英

尝试使用 FOREIGN KEY 创建新表时出现 mysql 错误 1822

[英]mysql error 1822 when trying to create new table with a FOREIGN KEY

尝试以下代码:

create table donation(
    donation_number int not null primary key  ,
    product_id int not null
);

create table stock ( 
    product_id int not null primary key, 
    available_qty int not null, 
    FOREIGN KEY (product_id ) REFERENCES donation(product_id)
);

回报

错误代码:1822。添加外键约束失败。 引用的表“捐赠”中缺少约束“stock_ibfk_1”的索引。

为什么? 我怎么解决这个问题?

要创建外键关系,您要在其上创建关系的父表列必须是唯一的或主要的,并且它们也必须具有相同的数据类型和大小

捐赠表中的 product_id 不是唯一的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM