简体   繁体   English

ER图中的基数

[英]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. 请帮助我。

Here is my EERD : 这是我的EERD: 在此处输入图片说明

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: 至少有两种可能的解决方案:

  1. Add a UNIQUE constraint on user_order.shipping_address_id user_order.shipping_address_id上添加UNIQUE约束
  2. Or: invert the relationship (this my preferred option as it eliminates unneeded surrogate keys): 或者:反转关系(这是我的首选,因为它消除了不需要的代理键):
    1. Remove the user_order.shipping_address_id column. 删除user_order.shipping_address_id列。
    2. Change shipping_address.id to shipping_address.order_id , so that it's a foreign-key of user_order.id 更改shipping_address.idshipping_address.order_id ,所以它是一个外键user_order.id
    3. Make 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: 其他一些技巧:

  • Consider using int rather than bigint for your identities - I doubt you'll have over 2 billion rows in each table. 考虑使用int而不是bigint来标识-我怀疑每个表中是否有超过20亿行。
  • Don't blindly use 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。
  • DO NOT STORE FULL CREDIT CARD NUMBERS IN YOUR DATABASE! 不要在您的数据库中存储完整的信用卡号! (as seen in your 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.

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