I'd like to know if I need to create a primary key for my item table?
You always1 need a key2, whether the table is involved in a relationship3 or not. The only question is what kind of key?
Here are your options in this case:
- Make
{item_id}
alone a key. This makes the relationship "non-identifying" anditem
a "strong" entity...- Which produces a slimmer key (compared to the second option), therefore any child tables that may reference it are slimmer.
- Any ON UPDATE CASCADE actions are cut-off at the level of the
item
and not propagated to children. - May play better with ORMs.
- Make a composite4 key on
{branch_id, item_no}
. This makes the relationship "identifying" anditem
a "weak" entity...- Which makes
item
itself slimmer (one less index). - Which may be very useful for clustering.
- May help you avoid a JOIN in some cases (if there are child tables,
branch_id
is propagated to them). - May be necessary for correctly modelling "diamond-shaped" dependencies.
- Which makes
So pick your poison ;)
Of course, branch_id
is a foreign key (but not key) in both cases.
And orthogonal to all that, if item_name
has to be unique per-branch (as opposed to per whole table), you need a composite key on {branch_id, item_name}
as well.
1 From the logical perspective, you always need a key, otherwise your table would be a multiset, therefore not a relation (which is a set), therefore your database would no longer be "relational". From the physical perspective, there may be some special cases for breaking this rule, but they are rare.
2 Whether its primary or not is immaterial from the logical standpoint, although it may be important if the DBMS ascribes a special meaning to it, such is the case with InnoDB which uses primary key as clustering key.
3 Please make a distinction between "relation" and "relationship".
4 Aka. "compound".