Short answer
No. A MySQL table can have only one PRIMARY KEY. If you try to define a second one, MySQL stops with ERROR 1068 (42000): Multiple primary key defined. MariaDB returns the same error.
You can still get the effect many people are after. A single primary key can cover two or more columns, which is called a composite primary key. In many tables, that is what the design needs.
Why a second primary key fails
This statement tries to mark two columns as the primary key:
CREATE TABLE enrollment (
student_id INT PRIMARY KEY,
course_id INT PRIMARY KEY
);
-- ERROR 1068 (42000): Multiple primary key defined
A PRIMARY KEY written on a column definition marks that one column as the table’s primary key. The second one asks for a second primary key, and MySQL refuses.
One primary key across two columns
Name both columns in one PRIMARY KEY clause, after the column list:
CREATE TABLE enrollment (
student_id INT,
course_id INT,
enrolled_on DATE,
PRIMARY KEY (student_id, course_id)
);
The key is the pair of values. A student can enroll in many courses, and a course can have many students, but each student and course pair appears only once.
MySQL also makes each column in the key NOT NULL, even if you leave that off. For the definition and the column order details, see What is a composite primary key?
When a UNIQUE key is the better fit
Sometimes a second column has to be unique without being the row’s identifier. For that, use a UNIQUE key. A table can have several UNIQUE keys and still have only one primary key:
CREATE TABLE member (
member_id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL,
UNIQUE KEY uq_member_email (email)
);
Here member_id identifies each row, and no two members can share an email address.
This is the usual pattern for a table with an AUTO_INCREMENT id. The id becomes the primary key, and any other column that must be unique gets a UNIQUE key. A table can also have only one AUTO_INCREMENT column.
The main difference from a primary key is NOT NULL. A primary key can’t hold NULL. A UNIQUE column that allows NULL can hold NULL in more than one row.
Adding a key to a table that already has one
If a table already has a primary key, ALTER TABLE can’t add another:
ALTER TABLE member ADD PRIMARY KEY (email);
-- ERROR 1068 (42000): Multiple primary key defined
To enforce uniqueness on email instead, add a UNIQUE key:
ALTER TABLE member ADD UNIQUE KEY uq_member_email (email);
This fails if the column already has duplicate values, so fix those first.
If the primary key itself needs to change, you can drop it and add the new one in the same ALTER TABLE statement. Check first whether another table’s foreign key depends on it, because MySQL usually refuses to drop a key that a foreign key uses. On a large table, rebuilding the key can take a long time, so test on a copy first.
Foreign keys that point at a composite key
A child table can reference a composite primary key by listing all of its columns, in the same order:
CREATE TABLE grade (
student_id INT,
course_id INT,
final_grade CHAR(2),
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id, course_id) REFERENCES enrollment (student_id, course_id)
) ENGINE=InnoDB;
Each grade row must match an existing enrollment row. Here the same two columns are both the primary key and a foreign key. Whether one column can play both roles is covered in Can PK and FK be the same?
If the terms are new, What is a PK and FK? defines both.
Foreign key checks are done by InnoDB, and the parent and child tables must use the same storage engine. For how the engines differ, see types of mysql tables and storage engines.
Check the keys on a table
To see every key a table has, including the primary key and any UNIQUE keys, run:
SHOW CREATE TABLE enrollment;
Look at the output before you change keys. It shows what is there now, which is the starting point for any change.
Quick answers
- A table has one
PRIMARY KEY. That key can span several columns. - Two columns that together identify a row: one composite
PRIMARY KEY. - Another column that must be unique: a
UNIQUEkey. - A table with an
AUTO_INCREMENTid: the id is the primary key, and other unique columns getUNIQUEkeys.