Please note : This help page is not for the latest version of Enterprise Architect. The latest help can be found here.

Create a Foreign Key

Foreign keys are modeled in Enterprise Architect as operations with the stereotype FK. The operation's parameters are the columns involved in the key.

Create a Foreign Key

Step

Action

See also

1

Locate the required child (foreign key) Table and parent (primary key) Table in your Data Modeling diagram.

Both Tables must have defined database types. The parent Table must have at least one column with a Primary Key and/or at least one unique constraint.

The parent Table constraint defines the columns that are involved in the Key, and the system automatically assigns columns from the child Table to match those columns. A simple key has one column pair, whilst a composite key has several column pairs.

 

 

Set The Database Type

Primary Key

Unique Constraints

 

2

Select an Association connector in the Data Modeling page of the Diagram Toolbox.

 

 

3

Click on the Table to contain the Foreign Key (the child Table) and draw the connector to the parent Table.

 

 

4

If the Foreign Key Constraint dialog has been set to display automatically when two Tables are joined, it does so. Otherwise, either:

·Double-click on the connector or
·Right-click on the connector and select the Foreign Keys context menu option

 

The Foreign Key Constraint screen displays. Define the key as directed.

 

 

Define the Foreign Key

Field/Option/Button

Action

See also

Join on Constraint

This combo box lists the Primary Key and/or unique constraints defined against the parent Table.  If there is more than one item listed, select which one to use in the Foreign Key.

When you select the Primary Key or a constraint, its columns are automatically listed in the Involved Columns panel, under the Parent: <tablename> column.

 

 

Involved Columns

This panel shows the parent Table and child Table columns that are paired up in the key.

The system expects to link columns with the same name in a Foreign Key, and automatically draws matching columns from the child Table until all parent Table columns in the constraint are paired.

If the child Table does not include a column of the same name as in the constraint, a new column of that name will be added to the child Table, marked with a following asterisk (*). The asterisk is removed when you subsequently redisplay the dialog.

However, if you want to force the pairing to an existing child Table column or a new column with a different name, click on the column name field and either:

·Type in the replacement name, or
·To select an existing column, click on the drop-down arrow and select the name from the list

 

 

Name

The default Foreign Key name is set by the Foreign Key Name Template.

To change the name to something other than the default provided by the template, simply overtype the value.

 

Define Foreign Key Name Template

 

 

On Delete

 

Click on the drop-down arrow and select the action (to maintain referrential integrity) the system should take when the Foreign Key is deleted.

 

 

On Update

Click on the drop-down arrow and select the action (to maintain referrential integrity) the system should take when the Foreign Key is updated.

 

 

Parent

Click on the drop-down arrow and select the cardinality of the parent Table in the Foreign Key.

 

 

Child

 

Click on the drop-down arrow and select the cardinality of the child Table in the Foreign Key.

 

Create?

If you want to create a Foreign Key Index at the same time as the Foreign Key, click on the drop-down arrow and set this property to True.

The name of the Foreign Key Index is controlled by the Foreign Key Index template, and the generated name is shown in the Name field underneath the Create? field.

 

 
Define Foreign Key Index template

 

Automatically show this screen when tables are joined

 

(For diagrammatic modeling) Select this checkbox to automatically display the dialog when an Association is created between two Tables.

 

OK

Click on this button to generate the Foreign Key operations.

 

 

Examples

This example shows simple Foreign Keys in a diagram:

PrimaryKeyDiagram

 

This example shows a Composite Foreign Key in a diagram:

ForeignKey

Notes

·For MySQL databases, Foreign Keys are supported for InnoDB Tables only
 
InnoDB is the default MySQL storage engine as of version 5.5; if you are modeling a MySQL database that is earlier than version 5.5 and you want to use Foreign Keys, you must set the Engine Tagged Value to InnoDB
·It is not possible to change the order of the involved columns in a composite Foreign Key, without first deleting the Foreign Key, changing the order of the involved columns in the constraint, and recreating the key

 

Learn more