Add a foreign key constraint
A foreign key constraint is added to a dependent table. In order to create a foreign key
constraint, a primary or unique key, called a parent key in a foreign key constraint, must already
exist in the parent table.
If you are adding a foreign key constraint to a new table, follow the instructions for creating a table
and begin with step 6 when you get to the Table Definition dialog.
- In the System i Navigator window, expand the system you want to use.
- Expand Databases.
- Expand the database and schema that you want to work with.
- Click the Table container.
- Right-click the table to which you want to add the key and select Definition.
- On the Table Definition dialog, select the Foreign Key Constraints tab.
- On the Foreign Key Constraints tab, click New.
- On the New Foreign Key Constraint dialog, specify a name for the constraint. If no name is
specified, the system automatically generates a name.
- Select the column that you want to be dependent on the parent key value in the parent table.
- Select the schema that contains the parent table.
- Select the table that contains the parent key.
- Select the parent key to reference.
- Select the Delete action.
- Select the Update action (The action for insert is default)
- Click OK to return to the Table Definition dialog.
- Click OK to create the Foreign Key constraint
Note: You may modify a constraint only if it has been defined during your current table editing
session. If you added the constraint and then clicked OK on either the New Table dialog or Table
Properties dialog, then you have read only access for the constraint. If you want to change the
constraint properties, you must drop the constraint and then recreate it with the appropriate changes.