Composite Primary Key Setup Guide
A composite primary key in a database is a unique identifier formed by multiple columns combined together. To set a composite primary key in a database, you can use the following syntax when creating a table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
PRIMARY KEY (column1, column2)
);
In the above example, column1 and column2 together form a composite primary key. This means the combination values of these two columns must be unique and cannot be repeated. If more columns need to be added to the composite primary key, simply continue adding column names after PRIMARY KEY.
Additionally, you can use the ALTER TABLE command to add a composite key, as shown in the following example:
ALTER TABLE table_name
ADD CONSTRAINT pk_name PRIMARY KEY (column1, column2);
In the above example, pk_name is the name of the composite primary key to be added, and column1 and column2 are the columns to be included in the composite key.