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:

  1. Specify multiple columns as primary keys when creating a table.
CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    PRIMARY KEY (column1, column2)
);
  1. Add a primary key constraint using the ALTER TABLE statement.
ALTER TABLE table_name
ADD PRIMARY KEY (column1, column2);
  1. Specify the PRIMARY KEY constraint when creating a table.
CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    PRIMARY KEY (column1, column2)
);
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds