How to Create a Relationship in MySQL Workbench

To create a relationship in MySQL Workbench:

  1. Create a database model (either create a new model or reverse engineer an existing database)
  2. Viewing the database model, double click on the first table of the relationship
  3. The bottom pane will open with the table details. Click on the Foreign Keys tab
  4. In the left pane, select the foreign key field and referenced table
  5. In the middle pane, select the foreign column and referenced (primary key) column
  6. In the right pane, set any Update/Delete actions you’d like to occur when a primary key record is updated or deleted

The relationship is now established. Repeat steps 4 to 6 for any other foreign key columns in that table.

To create a database from this model:

  1. Export the updated schema to a script.
  2. Create a database from the script.

Example

Here is an example of establishing a relationship using a database model:

Screenshot of the Foreign Keys tab when modelling a database in MySQL Workbench
The “Foreign Keys” tab enables you to set up relationships between tables.

This example is showing two relationships have been established on the “Albums” table. Clicking on a foreign key in the left pane refreshes the middle and right panes with its details.