Foreign Key (Entity Relationship Diagram)
A foreign key (abbreviated as FK) is an attribute or a set of attributes within an entity that references the primary key of another entity.
A foreign key can be a single attribute or a set of multiple attributes. The types of the corresponding attributes of a primary key must be the same as the types of foreign key attributes.
Foreign Key Symbol
The attributes that are part of a foreign key are marked with one of these symbols:
- hash (#)
- abbreviation (FK)
- bent arrow (↪)
The attributes of the foreign key are rendered with italic.
Foreign Key Example
An apartment is a part of a building. We can model this using two entities: Apartment and Building, the Apartment's foreign key points to the primary key of the Building entity. In this case, the foreign key consists of multiple attributes because the Building entity has a multi-attribute primary key (BuildingNumber, StreetName, and StreetNumber).
How to Set Foreign Key for ERD Entity
There are various options how to set foreign key to an ERD entity:
- Right-click on the ERD entity attribute and choose Foreign Key from the context menu
- Select an ERD entity attribute and click on the Foreign Key button in the ribbon / Field tab / Modifiers group.
- Edit the ERD entity attribute text in the diagram editor and type # before the attribute name
- Open the Properties dialog for the ERD entity (choose Properties from the context menu after right-clicking the ERD entity), switch to the Attributes tab, and check the FK check box for the attribute (or attributes) that should be the foreign key for the entity.
Automatic Setting of Foreign Key
The foreign key is automatically set if you create a relationship from a primary key of an entity to another entity. If the types of connected attributes are inconsistent, Software Ideas Modeler will suggest a fix.
Multi-Attribute Foreign Key
If you want to specify which primary key's and foreign key's attributes are linked you can define them using the Properties dialog. Select the relationship between the entities, open the Properties dialog, switch to the Relationship tab, and edit the corresponding primary and foreign attributes in the Attributes grid.