A foreign key defines a relationship between two tables by linking a column in one table to the primary key (or unique column) of another. DbSchema lets you create and visualise foreign keys directly on the diagram, and represents them as connector lines between tables.
There are two ways to create a foreign key in DbSchema.
You can double-click any relationship line on the diagram to open the Foreign Key Editor and review or modify its settings.
When a table has multiple candidate columns, you can drag the FK line endpoint to point it at the exact column you want. This is useful for composite keys and for making the diagram easier to read.
In the physical design, cardinality is determined automatically by the NOT NULL and UNIQUE constraints on the referencing column. You can set these in the Column Editor. In the logical design, you can set cardinality manually.
| NOT NULL | UNIQUE | Relationship type | Diagram line |
|---|---|---|---|
| No | No | 1:n — one to many (optional) | Dashed, crow's foot |
| No | Yes | 1:0..1 — one to zero or one | Dashed |
| Yes | No | 1:n — one to many (mandatory) | Solid, crow's foot |
| Yes | Yes | 1:1 — one to one | Solid |
You can switch the FK notation on the diagram from Diagram → FK Notation (Crow's Foot, Barker, UML, and others).
FK actions define what happens to referencing rows when the referenced row is deleted or updated. Configure these in the Foreign Key Editor:
NULL when the referenced row is deleted.If the database does not enforce foreign key constraints (e.g. MongoDB, MySQL MyISAM, or legacy
schemas), you can create a virtual foreign key in DbSchema. Virtual FKs exist only in the
.dbs model file — no constraint is created in the database. They are fully recognised by the
Query Builder and Relational Data Explorer
for joins and table navigation.
To create a virtual FK, drag from a column in one table to the target column in another table while holding the mouse button. DbSchema will prompt you to choose between a real or virtual FK.
A composite foreign key spans two or more columns on each side. This is used when the primary key of the referenced table is itself composite. In the Foreign Key Editor, each referencing column is paired with the corresponding referenced column. The referenced side must have a primary key or unique index defined on the same set of columns.
See also: Diagrams · Logical Design · Query Builder