How to create Foreign Key with Toad for IBM DB2

You can open window for create constraint in many ways.

First of them is opening Explorer by click on icon in main menu or by Tools -> Explorer.

Fig. 1. Explorer icon in menu

Now locate table in the Explorer, right click on it and select Create Constraint…

Fig. 2. Create Constraint in table context menu

Second way to open Create Constraint window is by Object Details. In the explorer select table and in viewer switch to Constraint tab. Locate and click Add constraint to a table button.

Fig. 3. Create Constraint in Object Details window

Another method is to open it from Database Diagram. In the diagram window click on table and select Table [Schema Name].[Table Name] -> Create Constraint...

Fig. 4. Create Constraint in Database Diagram

In new window change type of constraint to Foreign Key (layout will change). You can set name of the constraint, but if you leave it DB2 will name it automatically. In the Parent Table Information find for primary table (on the right side, you have preview of primary key columns). In This Table section select on which columns you define foreign key. Bear in mind that used columns in foreign key must have appropriate data type to primary key column data type. All columns from Primary Table must have equivalent column in foreign key constraint. At the bottom you can find additional options for foreign key behaviour when record in primary table will be deleted or updated.

Fig. 5. Create Constraint window

After that click Show Script button. In the window will appear generated script to execute, which you can run by hitting Execute button.

Fig. 6. Create Constraint script

0
There are no comments. Click here to write the first comment.