简体   繁体   English

关系数据库设计:条件

[英]Relational Database Design: Conditionals

I'm designing a relational database that I plan to implement with SQL.我正在设计一个我计划用 SQL 实现的关系数据库。 I have a use case that I'm working on and seem to be having a bit of trouble thinking through the solution.我有一个正在处理的用例,似乎在思考解决方案时遇到了一些麻烦。 The design is for an e-commerce order system.该设计用于电子商务订单系统。

Use Case: The ORDER_DETAILS table contains a deliveryMethod attribute.用例: ORDER_DETAILS表包含一个deliveryMethod属性。 I then have a SHIPPING_DETAILS table that contains address information and a PICKUP_DETAILS table that contains location, date, and time information for an in-person pickup.然后我有一个包含地址信息的SHIPPING_DETAILS表和一个包含地点、日期和时间信息的PICKUP_DETAILS表,以便亲自取货。 When a user places an order, they have the option to have their order shipped to their address or to pick up their order in person.当用户下订单时,他们可以选择将订单运送到他们的地址或亲自取货。 My current thought is to have a shippingId foreign key and pickupId foreign key in the ORDER_DETAILS table.我目前的想法是在ORDER_DETAILS表中有一个shippingId外键和pickupId外键。 Then, basically run a conditional check on the deliveryMethod attribute and retrieve data from the appropriate table depending on the value of that attribute (either "shipping" or "pickup").然后,基本上对deliveryMethod属性运行条件检查,并根据该属性的值(“shipping”或“pickup”)从适当的表中检索数据。 With this thought, however, I would be allowing for null values to be present in the ORDER_DETAILS for either the shippingId or the pickupId attributes.然而,有了这个想法,我将允许在ORDER_DETAILSshippingIdpickupId属性提供空值。 From my understanding, null values are viewed negatively in relational designs.根据我的理解,在关系设计中,空值被认为是负面的。 So I'm looking for some feedback on this design.所以我正在寻找有关此设计的一些反馈。 Is this okay?这个可以吗? Am I overthinking the nulls?我是否过度考虑空值? Is there a more efficient way to design this particular schema?有没有更有效的方法来设计这个特定的模式?

If I understand your problem correctly,如果我正确理解你的问题,

  1. The cardinality of the relationship of ORDER to SHIPPING is 1 ---> (0, 1) ORDER 与 SHIPPING 关系的基数是 1 ---> (0, 1)
  2. The cardinality of the relationship of ORDER to PICKUP is 1 ---> (0, 1) ORDER 与 PICKUP 关系的基数为 1 ---> (0, 1)
  3. An ORDER MUST have either a SHIPPING or a PICKUP, but not both.订单必须有 SHIPPING 或 PICKUP,但不能同时有。

To enforce the constraint (#3) you could define a functional constraint in the database.要强制执行约束 (#3),您可以在数据库中定义一个功能约束。 That gets into interesting stuff.这进入了有趣的事情。

Anyway, like you say, you could make columns in ORDER that are FKs to the SHIPPING or PICKUP tables, but both of those are nullable.无论如何,就像您说的那样,您可以在 ORDER 中创建列,这些列是 SHIPPING 或 PICKUP 表的 FK,但它们都是可为空的。 I don't think null FKs are evil or anything, but they do get messy especially if you had a whole bunch of delivery methods and not just two.我不认为空 FK 是邪恶的或任何东西,但它们确实会变得混乱,特别是如果你有一大堆交付方法而不是两个。

If you don't like the nulls, you could have separate association tables: (1) ORDER_DELIVERY that has just an order_id and an delivery_id, each are FKs to the respective tables, and (2) ORDER_PICKUP, also a two column table.如果你不喜欢空值,你可以有单独的关联表:(1) ORDER_DELIVERY 只有一个 order_id 和一个 delivery_id,每个都是各自表的 FK,以及 (2) ORDER_PICKUP,也是一个两列表。 In each case the primary key would be order_id .在每种情况下,主键都是order_id Now there are no nulls: the orders with delivery are in the ORDER_DELIVERY table and the orders with pickup are in ORDER_PICKUP.现在没有空值:带交货的订单在 ORDER_DELIVERY 表中,带取货的订单在 ORDER_PICKUP 中。

Of course there's a tradeoff, as maintaining the constraint that there be exactly one and only one delivery method is not a consistency check across tables.当然有一个权衡,因为维护只有一种且只有一种交付方法的约束不是跨表的一致性检查。

Another idea is to make the delivery and pickup details be JSON fields.另一个想法是使交货和取货详细信息成为 JSON 字段。 Here you are doing more work on the application side, enforcing constraints programmatically, but you won't have nulls.在这里,您在应用程序方面做了更多工作,以编程方式强制执行约束,但您不会有空值。

I wish I could say that there was a slam-dunk go-to design pattern here, but I don't see one.我希望我能说这里有一个灌篮高手的设计模式,但我没有看到。 Personally with only two types of delivery methods, I would not shy from having nulls (as I'm not a purist).就个人而言,只有两种类型的交付方法,我不会回避空值(因为我不是纯粹主义者)。 But I do love it when the database does the work, so....但是当数据库完成工作时我确实喜欢它,所以......

(Oh, the answer to the question "are you over thinking things?" is no, this thinking is really good!) (哦,“你是不是想多了?”这个问题的答案是否定的,这个想的真好!)

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

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