Home Creating and Editing Objects

# Foreign keys

Currently there are two ways of creating foreign keys in SQLEditor. For simple column to column relationships it is easiest to link the fields together directly. For more complicated configurations including more than one pair of columns you should look at adding a foreign key object.

### Using Field level foreign keys

With this method foreign keys are created by dragging from one field to another field. You must ensure that you do not start the drag within the drag handle area of the field. When the drag is complete release the mouse button and a foreign key link will be added.

Not all databases support foreign keys and some only offer limited support. For example: MySQL requires that you use the INNODB table type if you want foreign key support.

Foreign key links can be configured to draw in several different styles.

See the Appearance Preferences Section for more details

Note that if you create a foreign key relation, the referenced field must be a unique or primary key.

SQLEditor will automatically set the field to be unique if necessary.

### Example

1. In this example we want to create a foreign key on the widget table that references the primary key of the sprocket table.
2. Create a new field to be the foreign key
3. Drag from a field on the widget table to the primary key of the sprocket table.
4. The new foreign key relation:

### Using a Foreign Key Object

For more complicated foreign key relationships you should use a foreign key object.

Foreign key objects appear next to fields in a table. They link together fields in the same table to fields in another table. You need to add the fields first, then setup the foreign key.

1. Select the table you want to link from
2. choose Object→Add Foreign key
3. Drag from the newly created foreign key object to the target table to create a connection.
4. Click on the foreign key to update the inspector window
5. Add a column pair by clicking the [+] button at the bottom of the foreign key inspector.
6. choose a column for source and target in the column pair.

Note:
Entries in foreign key objects need both source and target set or they will be ignored.

### Compound Foreign Key Example

In this example we have two tables, song and album. Songs are attached to albums using a compound foreign key on the album name and the album artist.