Home > Database > Mysql Tutorial > How to Remove Composite Primary Keys in MySQL While Keeping an Auto-Increment Column?

How to Remove Composite Primary Keys in MySQL While Keeping an Auto-Increment Column?

Susan Sarandon
Release: 2024-12-31 03:28:10
Original
394 people have browsed it

How to Remove Composite Primary Keys in MySQL While Keeping an Auto-Increment Column?

Removing Primary Key from Multiple Columns in MySQL

Question:

How can I remove the primary keys from user_customer_id and permission_id while retaining the primary key for id in the user_customer_permission table, despite encountering an error when using the "alter table user_customer_permission drop primary key" command?

Explanation:

MySQL requires that an autoincrement column be part of the leftmost index. In this case, the id column is autoincrement, so removing the primary key from id would violate this requirement.

Solution:

To successfully remove the primary keys from user_customer_id and permission_id, follow these steps:

  1. Remove the autoincrement property from the id column:

    ALTER TABLE user_customer_permission MODIFY id INT NOT NULL;
    Copy after login
  2. Drop the primary key:

    ALTER TABLE user_customer_permission DROP PRIMARY KEY;
    Copy after login

Note: It's important to note that the table previously had a composite PRIMARY KEY covering all three columns (id, user_customer_id, permission_id). To ensure the uniqueness of the id column, you can make it the primary key again with autoincrement:

ALTER TABLE user_customer_permission MODIFY id INT NOT NULL PRIMARY KEY AUTO_INCREMENT;
Copy after login

By following these steps, you can successfully remove the primary keys from user_customer_id and permission_id while retaining the primary key for id.

The above is the detailed content of How to Remove Composite Primary Keys in MySQL While Keeping an Auto-Increment Column?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template