What are the methods for creating a composite primary key in MySQL?
In MySQL, you can create a composite primary key using the following methods:
- Specify multiple columns as primary keys when creating a table.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
PRIMARY KEY (column1, column2)
);
- Add a primary key constraint using the ALTER TABLE statement.
ALTER TABLE table_name
ADD PRIMARY KEY (column1, column2);
- Specify the PRIMARY KEY constraint when creating a table.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
PRIMARY KEY (column1, column2)
);