Java Reference
In-Depth Information
Table 5-10.
A Simple
Email
Table
ID
Email
1
dcminter@example.com
2
dave@example.com
3
jlinwood@example.com
4
jeff@example.com
Table 5-11.
A Link Table Joining
User
and
Email
in a One-to-Many Relationship
UserID
EmailID
1
1
1
2
2
3
2
4
Additional columns can be added to the link table to maintain information on the order-
ing of the entities in the association.
A unique constraint must be applied to the “one” side of the relationship (the
userId
col-
umn of the
UserEmailLink
table in Figure 5-7); otherwise, the link table can represent the set of
all possible relationships between
User
and
Email
entities—a many-to-many set association.
Figure 5-7.
A relationship represented by a link table (duplicates are not permitted because of the
use of a compound primary key)
The Many-to-Many Association
As noted at the end of the previous section, if a unique constraint is not applied to the “one”
end of the relationship when using a link table, it becomes a limited sort of many-to-many
relationship. All of the possible combinations of
User
and
Email
can be represented, but it is
not possible for the same
User
to have the same e-mail address entity associated twice,
because that would require the compound primary key to be duplicated.
If instead of using the foreign keys together as a compound primary key, we give the link
table its own primary key (usually a surrogate key), the association between the two entities
can be transformed into a full many-to-many relationship, as shown in Table 5-12.
Search WWH ::
Custom Search