Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take an order with multiple order line items as an example. The weak entities would be the individual line items stored in their own table. Their primary key could be the primary key of the order, plus a simple integer number (e.g. 1, 2, 3, which is unique only within the order.) Thus, they don't really have their own primary key as a unique numbered column, their key spans two columns and is only unique that way.</p> <p>The order line items should be deleted if and when the order is deleted - they don't make sense standing on their own. It is this linkage that makes them weak -- one thing being deleted should delete the other.</p> <p>If you give each order line item their own primary key, you'll still need to relate them back to the order item, which means putting in a foreign key for the order item or, having a cross reference table. (You may also need to know the line item number from the order, which would mean adding a simple integer column... and at this point you've added enough to have a key without an auto generated one.) For the design pattern of owned sub items, either of these alternatives is a bit of overkill.</p> <p>Using the complex primary key also enforces the relationship between order and line order items, in that this schema will not allow you cannot have a line item assigned to multiple orders.</p> <p>Another consideration is that you can shard the orders and order line items according to the order item primary key, since both tables have that key. (Sharding is generally easier to do based on the primary key than regular columns.)</p> <hr> <p>Hierarchical containment isn’t always what you want; but, it is such a commonly occurring pattern that it is nice to be clear about it, and composite keys can be used in this case. Here, using order items with line items as sub-items (i.e. contained), we’re saying not just that line items are 1 to many with respect an order, but that line items are owned and don’t exist independently of orders — that line items compose to create a single order object.</p> <p>In keeping with that, we’re explicitly not going to manage a separate key space for (all) line items (together as a group), but instead borrow and extend the key space of an order. Instead of asking the system to maintain a separate key space for line items, and manually (i.e. less formally) maintaining a foreign key relation back to the order, and also maintaining an integer line item rather separately (from the order foreign reference), we can ask the system to ensure uniqueness of the whole composite key, which includes the line item number within the order.</p> <p>Of course, you wouldn’t be able to add a line item that isn’t associated with an order, but additionally, using the composite sub-key, you also won’t be able to add one that overlaps with another (e.g. it won’t let you add two line item #3’s for the same order).</p> <p>This forces producers and consumers of line items to think about them as being contained within and part of orders, and not as independent items, or, put another way, to reference a line item by going thru an order, or, yet in other words, to get a reference to the order “for free” by referencing one of its line items. (And because you also have a reference to the order as part of such a foreign key, you can also use that order portion of the composite foreign key alone to group or join.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload