MySQL 1068: Multiple primary keys defined.

When encountering the error "1068: Multiple primary keys defined" in a MySQL database, the immediate action to take is to investigate the table structure where the error occurred to identify duplicate primary key definitions. You can execute the following steps:

  1. Identify the problematic table: If the error message does not specify which table has the issue, review your recent SQL queries or the script that triggered the error to find the table in question.



  1. Check the table structure: Use the `DESCRIBE` or `SHOW CREATE TABLE` command to view the current structure of the table, including all defined keys. For example, if the table name is `yourtablename`, run:


DESCRIBE yourtablename;
or
SHOW CREATE TABLE yourtablename;

  1. Look for multiple primary key definitions: In the output of the above commands, check for more than one column or a set of columns marked as `PRIMARY KEY`. MySQL does not allow more than one primary key for a table, so you should identify any redundant or mistakenly added primary key definitions.



  1. Remove the extra primary key: If you find an extra primary key definition, decide which primary key is unnecessary or incorrect. To remove the extra primary key, use the `ALTER TABLE` statement. For example, if an incorrect primary key is on the column `columnname` in `yourtable_name`, you can drop it using:


ALTER TABLE yourtablename DROP PRIMARY KEY;
Note: This command drops the current primary key. If you intend to drop a specific primary key when having a composite primary key or if you're going to modify the primary key, ensure you're taking the correct action as per your table's requirements.

  1. Add or redefine the primary key (if necessary): If you needed to drop the incorrect primary key and now must define a new one or correct it, use the `ALTER TABLE` command to add the primary key. For example:


ALTER TABLE yourtablename ADD PRIMARY KEY (column_name);
Or for a composite primary key:
ALTER TABLE yourtablename ADD PRIMARY KEY (column1, column2);

  1. Test your changes: After making the adjustments, rerun the previous operation or script that resulted in the "1068: Multiple primary keys defined" error to ensure the issue is resolved.



This sequence of actions should resolve the immediate error by correcting the table's primary key configuration.

Never debug

MySQL

manually again

Let Dr. Droid create custom investigation plans for your infrastructure.

Start Free POC (15-min setup) →
Automate Debugging for
MySQL
See how Dr. Droid creates investigation plans for your infrastructure.

MORE ISSUES

Made with ❤️ in Bangalore & San Francisco 🏢

Doctor Droid