Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This is basically a question about what are weak entities? When should we use them? How should they be modeled?

What is the main difference between normal entities and weak entities? Does weak entities correspond to value objects when doing Domain Driven Design?

To help keep the question on topic here is an example taken from Wikipedia that people can use to answer these question: enter image description here

In this example OrderItem was modeled as a weak entity, but I can't understand why it can't be modeled as a normal entity.
Another question is what if I want to track the order history (i.e. the changes in it status) would that be a normal or weak entity?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Formally a "weak" entity has the following characteristics.

1.  It is existence-dependent on another entity, i.e., 
    it cannot exist without the entity with which it has a relationship.

2.  It inherits at least part of it's primary key from the entity to which 
    it is related. 


    i.e. -> A weak entity's primary key must be a composite key that includes 
       the primary key of the entity on which it is existence-dependent.

I would say that in practice you wouldn't overtly decide to make something a "weak" entity per se; you would instead structure the data to be representative of whatever you are trying to model. If, after you have done this, you look at a particular entity and it has the characteristics of a "weak" entity, you can document or diagram it accordingly if for some reason you feel the need to explicitly call this out or for the sake of formality.

share|improve this answer
    
hmmm so what about my example? here OrderItem is dependent on Order as no orderItems can exist without belonging to an order, but I can't see why I can't use ItemLineNumber to solely identify an item?! Actually I might just make ItemLineNumber an auto generated int to insure uniqueness and use a foreign key orderID to link the two entities together?! –  Songo Dec 7 '12 at 11:54
1  
If you define your OrderItem to have a uniquely identifying sequential id, and the OrderId is not part of the key, then you are treating OrderItems as first order citizens and don't really have a weak entity. You could FK other tables to OrderItems individually if you wanted to; it is unnecessary to already have an OrderId to get at OrderItems. On the other hand if you keyed OrderItem with OrderId and a sequenceId (or similar) relevant to the Order, you would have a weak entity and individual line items would only be referenceable using the OrderId and sequenceId. Model usage as intended. –  Ed Hastings Dec 7 '12 at 20:27
1  
Also, a tangential comment, it can be very tempting to just give every table its own sequential primary key, and keep relationships as simple as possible with single field PK->FK relationships. It's great for simple databases in particular, and it is easy to reason about. However, when modeling more complex and / or sophisticated relationships composite keys become very useful and give you more options to model nuances. –  Ed Hastings Dec 7 '12 at 20:39

An OrderItem can not exist without an order or an product. Hence it's weak since it's dependencies control it.

If you for instance remove the order you have no way of knowing where the item should be shipped. Or if you remove the product you don't know what to ship.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.