Keywords: MySQL | Composite Primary Key | Auto-increment | Error 1075 | Table Structure Modification
Abstract: This technical article provides an in-depth analysis of composite primary key removal in MySQL, focusing on error 1075 causes and resolutions. Through practical case studies, it demonstrates proper handling of auto-increment columns in composite keys, explains MySQL's indexing requirements, and offers complete operational procedures with best practice recommendations.
Problem Background and Error Analysis
In MySQL database design, composite primary keys are commonly used, but when table structure adjustments are needed, various constraint limitations may arise. This article explores how to properly remove partial columns from composite primary keys through a typical user permission table case study.
The original table structure shows a composite primary key containing three columns:
mysql> describe user_customer_permission;
+------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_customer_id | int(11) | NO | PRI | NULL | |
| permission_id | int(11) | NO | PRI | NULL | |
+------------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
MySQL Auto-increment Column Constraints
MySQL imposes strict constraint requirements on columns with auto_increment attributes. When users attempt to directly execute ALTER TABLE user_customer_permission DROP PRIMARY KEY;, the system returns error 1075: "Incorrect table definition; there can be only one auto column and it must be defined as a key".
The fundamental cause of this error lies in MySQL's requirement that auto-increment columns must be the leftmost part of some index. In composite primary key scenarios, while the id column is part of the primary key, removing the entire primary key would cause the auto-increment column to lose index support, violating MySQL's design constraints.
Solution and Implementation Steps
Based on best practices, we recommend a step-by-step implementation approach:
First, remove the auto-increment property from the id column:
ALTER TABLE user_customer_permission MODIFY id INT NOT NULL;
This operation converts the id column from an auto-increment integer to a regular integer type while maintaining the NOT NULL constraint. After this step, the auto-increment column constraint conditions are satisfied.
Next, remove the composite primary key:
ALTER TABLE user_customer_permission DROP PRIMARY KEY;
This operation will now execute successfully, completely removing the original three-column composite primary key.
Data Integrity Restoration
After removing the composite primary key, if it's confirmed that the id column maintains uniqueness in the actual data, it can be re-established as an auto-increment primary key:
ALTER TABLE user_customer_permission MODIFY id INT NOT NULL PRIMARY KEY AUTO_INCREMENT;
This operation restores the auto-increment特性 and primary key constraint of the id column while ensuring data integrity.
Alternative Solution Analysis
Another viable approach is single-statement operation:
ALTER TABLE `user_customer_permission` DROP PRIMARY KEY, ADD PRIMARY KEY (`id`);
This method uses atomic operations to simultaneously remove the old primary key and establish a new one, avoiding temporary loss of auto-increment properties. However, in production environments, the step-by-step approach is recommended for better control over the operation process and error handling.
Best Practice Recommendations
When performing table structure modifications, it's advised to follow these best practices:
- Execute DDL operations during off-peak hours to avoid impacting online services
- Back up data in advance to ensure quick recovery if operations fail
- Validate operational procedures in test environments to confirm no side effects
- Monitor system resource usage during operations
- Verify data integrity and business logic correctness after operations
Technical Principles Deep Dive
MySQL's requirement that auto-increment columns must be the leftmost part of an index is based on performance optimization considerations. Auto-increment columns are typically used to generate unique identifiers, and without index support, the cost of maintaining auto-increment sequences would significantly increase. In the InnoDB storage engine, table data is organized in primary key order, with auto-increment columns as the leftmost part of the index optimizing insertion performance and data storage efficiency.
Composite primary key design should be based on business requirements, ensuring each record can be uniquely identified. In permission management scenarios, the composite primary key (id, user_customer_id, permission_id) may reflect specific business logic, but adjustments require careful evaluation of impacts on existing applications.