我想将供应商列中的 id 设置为订单列中的 sellers_id 外键。
但是我收到黄色字段中显示的错误。 外键设置为 null,但即使我禁用该选项,它仍然会给出错误。
希望有人能帮忙
这是我的表定义:
===Database inventory_test
== Table structure for table mutaties
|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|product_id|int(11)|No|
|aantal|int(11)|No|
|date|timestamp|No|CURRENT_TIMESTAMP
|omschrijving|varchar(255)|No|
== Table structure for table orders
|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|ordernummer|varchar(255)|No|
|product_id|int(11)|No|
|aantal|int(11)|No|
|omschrijving|varchar(255)|No|
|supplier_id|int(11)|Yes|NULL
|geleverd|tinyint(1)|No|0
|date|timestamp|No|CURRENT_TIMESTAMP
== Table structure for table products
|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|merk|varchar(190)|No|
|model|varchar(190)|No|
|threshold|int(11)|No|
== Table structure for table suppliers
|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|naam|int(11)|No|
最佳答案
问题在于订单列在 suppliers_id 列中包含值为 0 的数据。但是供应商表的 id 列中不存在 0 值,因此当您尝试创建外键时会出现错误。
您需要将订单表中的suppliers_id列设置为空,然后添加外键。要将供应商 ID 设置为 null,请运行以下查询:update Orders set seller_id=null
。另请参阅 MySQL Foreign Key 上的本教程
关于mysql - 无法设置关系 phpmyadmin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57088345/