[英]Circular reference in MySQL database
I am working on a database where I have a customer table, a vendor table and an address table. 我正在使用一个数据库,其中有一个客户表,一个供应商表和一个地址表。 I thought I'd put a customer id and vendor id in each row of the address table, but allow them to be null.
我以为我会将客户ID和供应商ID放在地址表的每一行中,但允许它们为空。
A vendor will likely have one address at a time, but just in case, I'll create it to allow multiple addresses. 一个供应商可能一次只能有一个地址,但以防万一,我将创建一个允许多个地址的地址。
A customer will often have 2 addresses, but might have many. 客户通常有2个地址,但可能有很多。
The customer table will have the fields "default_shipping_id" and "default_billing_id" 客户表将具有字段“ default_shipping_id”和“ default_billing_id”
I can do this without the FOREIGN KEY constraint, but if I try to create these tables with the constraints, I get errors because of the circular reference. 我可以在没有FOREIGN KEY约束的情况下执行此操作,但是如果尝试使用约束创建这些表,则会由于循环引用而出错。 Should I make the constraint one-way or can/should I add the constraints after the tables have all been created?
创建表之后,应该单向创建约束还是可以/应该添加约束?
Try to be neat and flexible with designing databases. 尝试在设计数据库时保持整洁和灵活。 5 tables for your solutions -
5张表格供您选择-
1) `address` table
`id`
`address`
2) `customers` table
`id`
`name`
`email`
3) `customer_address_mapping` table
`id`
`customer_id` (FK to `customers` table)
`address_id` (FK to `address` table)
`is_billing` (Boolean field)
`is_shipping` (Boolean field)
4) `vendors` table
`id`
`name`
`email`
5) `vendor_address_mapping` table
`id`
`vendor_id` (FK to `vendors` table)
`address_id` (FK to `address` table)
Explanation: 说明:
address
table will be a dump
of all the addresses
of all customers
and vendors
. address
表将是所有customers
和vendors
的所有addresses
的dump
。 Both mapping tables will provide you flexibility to add as many addresses you want. 两个映射表都可以让您灵活地添加所需的地址。
is_billing
and is_shipping
boolean fields will let you know the type of address of the customer. is_billing
和is_shipping
布尔字段将让您知道客户的地址类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.