MySQL Primary Key: How to Specify
In MySQL, you can specify a table’s primary key using the following method:
- Specify the primary key when creating the table.
CREATE TABLE table_name (
id INT PRIMARY KEY,
column1 datatype,
column2 datatype,
...
);
- Add a primary key constraint to an existing table.
ALTER TABLE table_name
ADD PRIMARY KEY (id);
In the example above, the “id” column in the table is designated as the primary key. You can choose the appropriate column as the primary key based on the actual situation.