A primary key constraint designates a column as the primary key of a table or view. A composite primary key designates a combination of columns as the primary key. A primary key combines a Not Null and unique constraint in one declaration. Therefore, to satisfy a primary key constraint, no primary key value can appear in more than one row in the table, and no column that is part of the primary key can contain Null. A table or view can have only one primary key.
Typically, a primary key for a table is defined in the CREATE TABLE statement. Below is the MySQL syntax for creating a table with a primary key:
CREATE TABLE table_name
(
column1 column_definition,
column2 column_definition,
...
CONSTRAINT [constraint_name]
PRIMARY KEY [ USING BTREE | HASH ] (column1, column2, ... column_n)
);
You can add a primary key to a table with the help of the ALTER TABLE statement. Below is the MySQL syntax for adding a primary key to the existing table:
ALTER TABLE table_name
ADD CONSTRAINT [ constraint_name ]
PRIMARY KEY [ USING BTREE | HASH ] (column1, column2, ... column_n)
Select Delete on the shortcut menu.
-or-
dbForge Studio for MySQL allows managing MySQL and MariaDB primary keys as well as foreign keys in a comprehensive and convenient GUI.
To add, delete, or alter a MySQL primary key on a table:
1. On a diagram, right-click the table to which you want to add a primary or foreign key.
2. Select an operation you want to perform from the context menu that appears.
3. You can add, drop, or change primary or foreign keys settings in the Configuration window that opens.