Help Tables, columns and indexes

Foreign keys

A foreign key ties columns in one table (the referencing, or child, table) to columns in another (the referenced, or parent, table). In bonSQL foreign keys are made and changed on the 3 Foreign Keys tab of the table designer of the child table.

The table designer, Foreign Keys tab

Opening it

Foreign keys need a storage engine that has them. If the table’s engine does not, bonSQL says:

The selected table does not support foreign keys. Table engine must be InnoDB, PBXT, SolidDB or ndbcluster (if ndbcluster engine version is greater than or equal to 7.3).

and the tab cannot be opened. For a new table, choosing such an engine at the top of the designer means the foreign keys on this tab are left out of the statement.

The grid

Each row is one foreign key. Type in the empty row at the bottom to add one, or click the green + button above the grid (Alt+Ins).

Grid columnWhat to enter
Constraint NameThe key’s name. Leave it empty and bonSQL writes no name, so the server chooses one.
Referencing ColumnsThe columns in this table. Click the … button in the cell, tick the columns, put them in order with Up and Down, and click OK.
Referenced DatabaseThe parent table’s database.
Referenced TableThe parent table, from the base tables of that database.
Referenced ColumnsThe parent’s columns. Click the … button in the cell: the list holds the columns of the parent table’s indexes, and bonSQL ticks the first index whose column types match the referencing columns.
On UpdateThe on update rule: No Action, Cascade, Set null or Restrict.
On DeleteThe on delete rule, with the same choices.

Leave On Update or On Delete empty and bonSQL writes no rule, so the server’s default applies. What each rule does is decided by the server; its manual has the details.

The number of referencing and referenced columns must match, and they are paired in order: the first referencing column with the first referenced column, and so on.

Rules on existing keys

When the server stores a key with its default rule, SHOW CREATE TABLE leaves the rule out. The default is not the same everywhere, so bonSQL fills the gap with the server’s own: Restrict on MariaDB and No Action on MySQL. A MariaDB key created with RESTRICT therefore shows as Restrict, not as an empty cell.

Saving

On Save, bonSQL checks every row and stops with one of these if something is missing:

MessageFix
No referencing columns selected for the foreign keyChoose Referencing Columns.
Referenced database not selectedChoose Referenced Database.
Referenced table not selectedChoose Referenced Table.
No referenced columns selected for the foreign keyChoose Referenced Columns.
The number of referencing and referenced columns do not matchTick the same number on both sides.

New keys are written as add constraint ... foreign key (...) references ...(...) with on update and on delete when set, inside the designer’s ALTER TABLE, or inside CREATE TABLE for a new table.

Changing a foreign key

A foreign key cannot be changed in place; bonSQL drops it and creates it again.

Dropping a foreign key

Tick the box at the start of the row and click the red - button above the grid (Alt+Del). bonSQL asks Do you want to drop the selected Foreign Key(s)?; the key is dropped with drop foreign key when you click Save. If no box is ticked, the button drops the row the cursor is in, after asking Do you want to drop this foreign key?.

Deleting a column on the Columns tab that a foreign key uses also changes the key; bonSQL warns first.

Tips