The scenario involves a MySQL table where the id column serves as an auto-increment field for visual convenience, while the memberid column acts as the actual unique key. However, the attempt to define the table with PRIMARY KEY (memberid) results in an error (1075) stating there can only be one auto column and it must be a key.
To resolve the issue, it is possible to have an auto-incrementing column that is not the PRIMARY KEY, provided an index (key) is defined on it. Here's the modified table definition:
CREATE TABLE members (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
memberid VARCHAR(30) NOT NULL,
`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
firstname VARCHAR(50) NULL,
lastname VARCHAR(50) NULL,
PRIMARY KEY (memberid),
KEY (id) # or: UNIQUE KEY (id)
) ENGINE = MYISAM;
By adding a KEY or UNIQUE KEY index on the id column, the auto-increment functionality is maintained while the memberid column becomes the primary key, allowing efficient queries based on the memberid value.
The best choice depends on the relative importance of performance and disk space. If performance is paramount, maintaining the auto-incrementing id column and using an index on memberid offers a balance:
However, if disk space is a significant concern, consider removing the id column altogether and relying on the memberid column as both the primary key and auto-incrementing field. This approach sacrifices some performance for improved space utilization. Ultimately, the choice between performance and space depends on the specific requirements of the application.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3