简体   繁体   English

带外键的NULL值

[英]NULL values with foreign key

I have 2 forms in php for adding the supplier and customer. 我在php中有2种表单,用于添加供应商和客户。 On these forms I can add contact of supplier or customer. 在这些表格上,我可以添加供应商或客户的联系方式。 Now, I made foreign key on table contact since 1 supplier or 1 customer can have many contacts. 现在,我在表联系人上设置了外键,因为1个供应商或1个客户可以有多个联系人。 But the problem is that I have 1 contact table, structured like this: 但问题是我有1个联系表,其结构如下:

ID  |  telefon  |  name  |  mail  |  type_fk  | supplier_fk_id  |  customer_id_fk
------------------------------------------------------------------------------
1   |  123      | john   | mail   |     1     |       23        |      NULL
2   |  123234   | julie  | gfdh   |     3     |       NULL      |      45

So basically, I want that to be stored in one table, and right now I'm not sure if that was a good idea. 因此,基本上,我希望将其存储在一个表中,现在我不确定这是否是一个好主意。 Since when I add customer contacts I have NULL value in supplier_fk_id, and same this for the supplier but in customer_fk_id. 自从我添加客户联系人以来,我在Supplier_fk_id中具有NULL值,对于供应商来说也是相同,但在customer_fk_id中。 I have foreign keys on update and on delete - cascade since I want those contacts to be deleted if the customer/supplier is deleted. 我在更新和删除时具有外键-级联,因为如果要删除客户/供应商,我希望删除这些联系人。 If I remove the foreign keys in the database I won't have the database side check which I want. 如果删除数据库中的外键,我将不需要数据库方面的检查。 But I get error like this: 但是我得到这样的错误:

Error   
Error inserting:
Cannot add or update a child row: a foreign key constraint fails 
(`testbase`.`pro_contact`, CONSTRAINT `supplier_fk_id` FOREIGN KEY (`supplier_fk_id`) 
REFERENCES `pro_supplier` (`supplier_id`) ON DELETE CASCADE ON UPDATE CASCADE)

Any advice would be appreciated... 任何意见,将不胜感激...

我认为根据您的决定,对SUPPLIER/CONTACTS使用ON DELETE SET NULL ,对CUSTOMER/CONTACTS ON DELETE CASCADE ,反之亦然

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

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