How to create foreign keys in DB2 using IBM Data Studio

Introduction

This tutorial will show you how to create foreign keys in DB2 using IBM Data Studio.

There are three options in IBM Data Studio called Activities from which you can create a foreign key.

  1. Administer Databases
  2. Develop SQL and Routines
  3. Tune Queries

All these options can be selected from the Activity option menu buttons.

Using the Administer Databases activity

  1. In the left pane of the screen that is displayed, expand the All Databases node and select the appropriate database to expand its objects tree, then right-click on Constraints and select the Create Foreign Key option.

  1. Select the table where the foreign key will be created. To do this, expand the node of the appropriate database, select the table and click on the OK button.

  1. Now select the parent table. As in the previous step, expand the node of the appropriate database, select the corresponding table and click on the OK button.

  1. Two panes are shown. The upper one shows the foreign key to be created and the lower one allows you to configure the foreign key in the Details option. Once done, save your work using the Save Changes button.

After saving any changes, you need to generate a DDL to deploy the changes. You can do this using any of the following two options:

  1. If the change plan is active and displayed in the change plan toolbar (you are viewing the previous screen), click on the Generate DDL button located in the upper pane.

  1. If the change plan is not active (you are viewing a different screen than the previous one), in the Administration Explorer, click the Change Plans folder in the database. From the Object List, right-click on the change plan to deploy and select the Review and Deploy option.

Now you will see a progress screen and then you will get the review and deploy screen, where you can review the commands that will be executed. Select the option Deploy the change commands and click on the Finish button.

Using the Develop SQL and Routines or the Tune Queries activity

  1. In the left pane of the screen that is displayed, select the Data Source Explorer tab and expand the Database Connections node.
  2. Select the appropriate database to expand its objects tree.
  3. Expand the Schemas node and the schema name.
  4. Expand the Tables node.
  5. Select the corresponding table and right click on the Constraints container.
  6. Choose the Create, Foreign Key option in the context menu.

  1. A window is displayed to select the table where the foreign key will be created. From this point, the steps are the same as those described in the previous section: select the parent table, configure the foreign key, save your work and generate a DDL to deploy the changes.
Comments are only visible when the visitor has consented to statistics cookies. To see and add comments please accept statistics cookies.
0
There are no comments. Click here to write the first comment.