IBM Books

Adding foreign keys when creating a table

Use the Foreign Keys page of the Create Table notebook to add foreign keys to your table. A foreign key is a column or set of columns in a table whose values are required to match at least one primary key value of a row of its parent table. A referential constraint is the rule that the values of the foreign key are valid only if either:

You can add, change, or remove new foreign keys for your table. After the table is created, you must use the Alter function to add, change, or remove foreign keys.

Graphic of Controls push button appears here. Graphic of Overview push button appears here.
Figure rule not displayed.

Before you begin...
Figure rule not displayed.

To add new foreign keys:

  1. Click on the Foreign Keys tab.

  2. Click on Add. The Add Foreign Key window opens.

  3. In the Table schema box, specify the schema of the parent table.

  4. In the Table name box, specify the name of the parent table. The Primary key box displays a list of the primary key columns defined for the parent table.

  5. In the Available columns box, select a column or columns that you want to define as a foreign key. Each column that you select must match a primary key column for the parent table in meaning and data type.

  6. Click on the > push button to move the selected column or columns to the Foreign key box.

  7. In the On delete box, specify what action is to take place on the dependent table when a row is deleted from the parent table.

  8. In the On update box, specify what action is to take place on the dependent table when a row of the parent table is updated.

  9. Optional: In the Constraint name field, type a name for the constraint that you're defining.

  10. Click on Add to add the new foreign key.

  11. Repeat steps 3 through 10 to add another foreign key to your table.

  12. Click on Close to close the Add Foreign Key window.

  13. If you do not want to add check constraints, click on OK to create your table and close the Create Table notebook. Otherwise, continue on to add check constraints.

Figure rule not displayed.

Related information

[ Top of Page | Table of Contents | Glossary | Index ]

[ DB2 List of Books | Search the DB2 Books ]