DB2 Version 9.7 for Linux, UNIX, and Windows

Adding foreign keys

You can use the Control Center or the ALTER TABLE statement to add foreign keys.

About this task

When a foreign key is added to a table, packages and cached dynamic SQL containing the following statements might be marked as invalid:

Procedure

Example

The following examples show the ALTER TABLE statement to add primary keys and foreign keys to a table:
   ALTER TABLE PROJECT
     ADD CONSTRAINT PROJECT_KEY
         PRIMARY KEY (PROJNO)
   ALTER TABLE EMP_ACT
     ADD CONSTRAINT ACTIVITY_KEY
         PRIMARY KEY (EMPNO, PROJNO, ACTNO)
     ADD CONSTRAINT ACT_EMP_REF
         FOREIGN KEY (EMPNO)
         REFERENCES EMPLOYEE
         ON DELETE  RESTRICT
     ADD CONSTRAINT ACT_PROJ_REF
         FOREIGN KEY (PROJNO)
         REFERENCES PROJECT
         ON DELETE CASCADE