How to add a primary key to a table in MySQL?

In MySQL, you can use the ALTER TABLE statement to add a primary key to a table. Here is an example:

ALTER TABLE table_name
ADD PRIMARY KEY (column_name);

In the above example, you need to replace “table_name” with the name of the table where you want to add a primary key, and replace “column_name” with the name of the column to be used as the primary key.

Additionally, if you want to add a composite primary key to an existing table, you can use a statement similar to the following:

ALTER TABLE table_name
ADD PRIMARY KEY (column1, column2);

In the example above, column1 and column2 are the names of the columns that are to be used as composite primary keys.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds