Foreign Keys & Relationships

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.

Creating a Foreign Key

There are two ways to create a foreign key in DbSchema.

From the Table Dialog

  1. Double-click a table on the diagram to open the Table Editor.
  2. Switch to the Foreign Keys tab.
  3. Click Add, choose the referenced table, and map the columns.
Foreign Key dialog in DbSchema

By Drag & Drop on the Diagram

  1. Hover over the referencing column — a small connector handle appears on the right edge.
  2. Drag from that handle to the target column in the referenced table.
  3. DbSchema creates the foreign key and draws the relationship line immediately.

You can double-click any relationship line on the diagram to open the Foreign Key Editor and review or modify its settings.

Foreign Key options in DbSchema

Pointing a FK to a Specific Column

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.

Pointing a foreign key to a specific column on the diagram

Cardinality Types

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 NULLUNIQUERelationship typeDiagram line
NoNo1:n — one to many (optional)Dashed, crow's foot
NoYes1:0..1 — one to zero or oneDashed
YesNo1:n — one to many (mandatory)Solid, crow's foot
YesYes1:1 — one to oneSolid

You can switch the FK notation on the diagram from Diagram → FK Notation (Crow's Foot, Barker, UML, and others).

FK Actions

FK actions define what happens to referencing rows when the referenced row is deleted or updated. Configure these in the Foreign Key Editor:

  • NO ACTION (default) — raises an error if the referenced row is still in use. Delete child rows first.
  • CASCADE — automatically deletes or updates the referencing rows when the referenced row changes.
  • SET NULL — sets the referencing column to NULL when the referenced row is deleted.
  • SET DEFAULT — sets the referencing column to its default value (supported by some databases).

Virtual Foreign Keys

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.

Virtual foreign key in DbSchema

Composite Foreign Keys

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