Java Reference
In-Depth Information
If it is not appropriate for the tables to share primary keys, then a foreign key relationship
between the two tables can be maintained, with a “unique” constraint applied to the foreign
key column. For example, reusing the User table from Table 5-6, the Email table can be suit-
ably populated, as shown in Table 5-8.
Table 5-8. An Email Table with a Foreign Key to the User Table
ID
Email
UserID (Unique)
34
dcminter@example.com
1
35
jlinwood@example.com
2
36
tjkitchen@example.com
3
This has the advantage that the association can easily be changed from one-to-one to
many-to-one by removing the unique constraint. Figure 5-6 shows this type of relationship.
Figure 5-6. Entities related by a foreign key relationship
The One-to-Many and Many-to-One Association
A one-to-many association (or from the perspective of the other class, a many-to-one associa-
tion) can most simply be represented by the use of a foreign key, with no additional
constraints.
The relationship can also be maintained by the use of a link table. This will maintain a for-
eign key into each of the associated tables, which will itself form the primary key of the link
table. An example of this is shown in Tables 5-9, 5-10, and 5-11.
Table 5-9. A Simple User Table
ID
Username
1
dcminter
2
jlinwood
Search WWH ::




Custom Search