Updating a primary key

Db2® interprets updating a primary key as a deletion, followed by an insertion of a new row the same as the deleted row but with the new primary key value.

If you update a row in a parent table, it can generate an error if there are rows in the dependent table with matching foreign key values and the delete restrict rule is specified for the relationship. The delete restrict rule prevents the deletion of the primary key value unless you first delete (or change) all the rows in the dependent table with foreign key values matching the primary key value.

In this situation:

  1. Repeat the row containing the original primary key value you want to change.
    Note: After the repeat operation, the new row is displayed immediately below the repeated row and is intensified.
  2. Change the primary key in the new row to the required value.
  3. Use the SAVE primary command to verify that there are no other Db2® errors.
  4. Delete the original row.
  5. Start a File Manager/Db2 Edit session for the dependent table by either using the RE prefix command, or REDIT primary command from the error information panel. Usually, only those rows that would be affected by the deletion of the primary key in the parent table are displayed (see following note).
  6. Either delete the displayed rows (see following note), or change the values of the foreign keys to another valid value.
  7. Press the Exit function key (F3) to validate and commit your changes.
  8. Return to the Edit session for the primary table and proceed with deletion of the primary key value.
Note:
  1. In most cases, FM/Db2 shows only those rows that would be affected by the deletion of the primary key in the parent table. The exception is the situation where the parent table has a self-referencing constraint that might cause the deletion of other rows in the parent table if a primary or parent key value is deleted or changed. In this situation, FM/Db2 shows all rows and you must select the dependent rows.
  2. If the parent or dependent tables are also parent or dependent tables in other relationships, changes to the primary or foreign key values can result in other errors.

Related references