DB2 Composite Key Setup Guide
You can set up a composite key in DB2 by following these steps:
When creating a table, define a composite key: Use the PRIMARY KEY keyword in the create table statement to specify a composite key, listing all the column names that need to be included in the primary key. For example:
CREATE TABLE table_name (
column1 INT,
column2 VARCHAR(50),
column3 DATE,
PRIMARY KEY (column1, column2)
);
2. Adding a primary key constraint: If a table has already been created, you can also add a primary key constraint using the ALTER TABLE statement. For example:
ALTER TABLE table_name
ADD CONSTRAINT pk_name PRIMARY KEY (column1, column2);
In the above example, pk_name is the name of the primary key constraint, and column1 and column2 are the column names of the composite primary key. By following these steps, you can successfully set a composite primary key in DB2.