[英]Cardinality in ER diagram
I've made a project that's essentially an online bookstore where one can buy books and place the order. 我已经完成了一个项目,该项目实质上是一个在线书店,可以在其中购买书籍并下订单。
My database contains various tables like: 我的数据库包含各种表,例如:
user
user_shipping_address
user_payment_mode
user_order
order_shipping_address
order_billing_address
order_payment_details
I tried to construct the EERD diagram for this but I am confused about one thing: A user_order
can only have one shipping address. 我试图为此构建EERD图,但是我对一件事感到困惑:一个user_order
只能有一个送货地址。 I've created a foreign key order_id
in the order_shipping_address
table that references the primary key order.id
. 我已经在order_shipping_address
表中创建了一个引用主键order.id
的外键order_id
。 I also have a shipping_address_id
foreign key in the table order
that references order_shipping_address.id
. 我在表order
中也有一个shipping_address_id
外键,它引用了order_shipping_address.id
。
When I try to generate the ER diagram, it gives me two different relationships. 当我尝试生成ER图时,它给了我两种不同的关系。 A 1:1 relationship between the order
and the shipping address and a 1:M relationship from the shipping address to the order. order
与收货地址之间为1:1关系,而收货地址与order
之间为1:M关系。 I don't know how to structure the foreign key constraints because I feel the order table should contain the shipping_address_id
and the shipping address should contain the order_id
, right? 我不知道如何构造外键约束,因为我认为订单表应包含shipping_address_id
,送货地址应包含order_id
,对吗? This just made everything more confusing. 这只是让一切变得更加混乱。
Please help me about this. 请帮助我。
This happens because your current design means it's possible for multiple user_order
rows to reference the same single shipping_address
row. 发生这种情况是因为您当前的设计意味着多个user_order
行可以引用同一单个shipping_address
行。
You need to change the design so that it's impossible for multiple user_order
rows to reference the same single shipping_address
row. 您需要更改设计,以使多个user_order
行无法引用相同的单个shipping_address
行。
There are at least two different possible solutions: 至少有两种可能的解决方案:
UNIQUE
constraint on user_order.shipping_address_id
在user_order.shipping_address_id
上添加UNIQUE
约束 user_order.shipping_address_id
column. 删除user_order.shipping_address_id
列。 shipping_address.id
to shipping_address.order_id
, so that it's a foreign-key of user_order.id
更改shipping_address.id
到shipping_address.order_id
,所以它是一个外键user_order.id
shipping_address.order_id
the new primary key of shipping_address
. 让shipping_address.order_id
的新的主键shipping_address
。 Note that both of these options are a denormalization as it prevents the sharing of shipping addresses between different orders (eg if the same customer makes the same repeat order a lot) though this can be intentional - so that if a user's future address changes it won't unintentionally retroactively update old order shipping records. 请注意,这两个选项都是非规范化的,因为它可以防止不同订单之间的送货地址共享(例如,如果同一位客户多次重复同一重复订单),尽管这样做可能是有意的-因此,如果用户的未来地址发生更改,则可能会获胜不会无意地追溯更新旧订单的运输记录。
A few other tips: 其他一些技巧:
int
rather than bigint
for your identities - I doubt you'll have over 2 billion rows in each table. 考虑使用int
而不是bigint
来标识-我怀疑每个表中是否有超过20亿行。 varchar(255)
for all text columns - use it to enforce reasonable constraints on data length, for example state
doesn't need to be longer than 2 characters if you're storing abbreviations, ditto zipcode
which can be varchar(10)
if you're using ZIP+4. 不要盲目地对所有文本列使用varchar(255)
-使用它来对数据长度实施合理的约束,例如,如果您存储缩写,则state
不必超过2个字符,同上的zipcode
可以是varchar(10)
如果您使用的是ZIP + 4。 payment
table) - This is a violation of PCI Rules, a massive liability and probably illegal negligence in your jurisdiction. (如您的payment
表中所示)-这违反了PCI规则,在您的管辖范围内承担巨额责任,并且可能是非法过失。 Your payment processor will provide you with a substitute opaque token value (or something similar) as a means of identifying charge-cards and applying future charges to stored payment details - the most you can reasonably store is the last 4 digits. 您的付款处理器将为您提供一个替代的不透明令牌值(或类似的值),作为识别签帐卡并将未来的费用应用于已存储的付款明细的一种方法-您可以合理地存储的最多是最后4位数字。 Whether or not your encrypt the data is largely irrelevant. 加密数据与否无关紧要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.