简体   繁体   English

mysql-此错误是什么意思?

[英]mysql - what does this error mean?

I do not require this error to be solved. 我不需要解决此错误。 I just need to understand what it means so I can approach it myself. 我只需要了解它的含义,就可以自己解决这个问题。

Cannot add or update a child row: a foreign key constraint fails
  (`db`.`transaction`, CONSTRAINT `transaction_ibfk_2`
    FOREIGN KEY (`product_id`) REFERENCES `product` (`id`))

INSERT INTO `transaction` ( `client_id`, `cost`, `website_id`, `product_id`,
    `template_number`, `value`, `order_id` )
    VALUES ( '17', '', '2480', '', '', '12', '1');

What is a foreign key? 什么是外键? How is it set? 如何设置?

What does CONSTRAINT transaction_ibfk_2 mean? CONSTRAINT transaction_ibfk_2是什么意思?

Does this mean I need to have a table called transaction_ibfk_2 ? 这是否意味着我需要一个名为transaction_ibfk_2的表?

Thanks. 谢谢。

You are inserting an empty string as productid (Fourth item in the list) 您正在插入一个空字符串作为productid (列表中的第四项)

INSERT INTO transaction  
(client_id, cost, website_id, product_id, template_number, value, order_id) 
VALUES ('17', '', '2480', '', '', '12', '1')

There is a referential integrity constraint set up to ensure that you must only insert productid s matching one in the referenced product table. 设置了参照完整性约束,以确保您只能在参照product表中插入与之匹配的productid

This means you are trying to add a value into the foreign key column which is not available in the referenced column or trying to add blank in foreign key column. 这意味着您试图在外键列中添加一个在引用列中不可用的值,或者试图在外键列中添加空白。 ie You are trying to add product_id as blank which is not allowed. 即您正在尝试将product_id添加为空白,这是不允许的。 All values in foreign key column must be valid values present in the main referenced column id. 外键列中的所有值必须是主引用列ID中存在的有效值。

There's not necessarily a relationship between constraint names and tables although most people name them appropriately to make their lives easier. 约束名称和表之间不一定存在关系,尽管大多数人都对它们进行了适当命名,以使生活更轻松。

All this means is that you have a transaction_ibfk_2 constraint. 这一切意味着您有一个transaction_ibfk_2约束。 The actual problem is in the rest of the message: 实际问题出在消息的其余部分:

FOREIGN KEY (product_id) REFERENCES product  (id)

You need to insert a row into your product table first. 您需要先在product表中插入一行。 The id that you insert there should be the product_id you're trying to insert into transaction (which is '' for some reason - I'm pretty certain this should be a real value (or possibly NULL)). 您在其中插入的id应该是您要插入transactionproduct_id (出于某种原因,它为'' -我很确定这应该是真实值(或可能为NULL))。

You can create foreign keys with a clause of the create table or alter table DDL statements. 您可以使用create table的子句或alter table DDL语句创建外键。

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

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