DB2 Version 9.7 for Linux, UNIX, and Windows

Adding unique constraints

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

About this task

Unique constraints can be added to an existing table. The constraint name cannot be the same as any other constraint specified within the ALTER TABLE statement, and must be unique within the table (this includes the names of any referential integrity constraints that are defined). Existing data is checked against the new condition before the statement succeeds.

Procedure

To add unique constraints using the Control Center:

  1. Open the Alter Table notebook:
    1. From the Control Center, expand the object tree until you find the Tables folder.
    2. Click the Tables folder. Any existing tables are displayed in the pane on the right side of the window.
    3. Right-click the table you want and select Alter from the pop-up menu. The Alter Table notebook opens.
  2. On the Check Constraints page, click Add.
  3. On the Add Check Constraint window, complete the necessary information.

Example

To define dimensions using the command line, use the ADD CONSTRAINT option of the ALTER TABLE statement. For example, the following SQL statement adds a unique constraint to the EMPLOYEE table that represents a new way to uniquely identify employees in the table:
   ALTER TABLE EMPLOYEE
      ADD CONSTRAINT NEWID UNIQUE(EMPNO,HIREDATE)