How to Create a Reference and Foreign Key

Top Previous Next

To create a reference between tables:

1. Click the icon-new-reference icon on the Palette toolbar. Your mouse cursor will change its appearance.

2. Click on the table (a referencing table) that will have a foreign key.

3. Then click on the second table (referenced table) whose constraint (e.g. Primary key) will be referenced by the new foreign key.

4. The Join tab of the Reference Editor will be shown. You can choose columns of a referenced and referencing table participated in the reference. You can choose <Auto Create Column> on the list of referencing table columns to Database Designer for PostgreSQL create a new column in the referencing table that will have a foreign key set on it. The properties of auto column will be copied from the respective column of the referenced table.

To create a self reference (that links columns at the same table), click the icon-new-reference icon on the Palette toolbar. Then click the same table two times. To create N:M references, please refer to Creating a Many-to-Many Reference.

Reference Creation in Details

On reference creation, Database Designer for PostgreSQL performs the following actions:

1. Creates new column(s) in the referencing table, their parameters (name, data type) will be copied from the primary key constraint of the referenced table. If the referencing table already has column(s) analogous to the primary key(s) of the referenced table, this column(s) will be used as foreign-key column(s).

2. If there is no primary key or unique constraints in the referenced table, a standard primary key column will be created in the referenced table.

3. Creates foreign key constraint in the referencing table that refers to the referenced table primary key(s).

See the diagram database options to find out more about disabling some of above actions.

See the Notation topic to find out more about reference symbol on the diagram.

See also:
Diagram: Notation | Database Options | Creating a Many-to-Many Reference
Diagram Objects: Reference Editor