Bug #103939 | Can't create table with composite PK if auto_increment column isn't first | ||
---|---|---|---|
Submitted: | 8 Jun 2021 8:04 | Modified: | 10 Jun 2021 15:17 |
Reporter: | Lukas Eder | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: DDL | Severity: | S3 (Non-critical) |
Version: | 8.0.24, 8.0.25, 5.7.34 | OS: | Any |
Assigned to: | CPU Architecture: | Any |
[8 Jun 2021 8:04]
Lukas Eder
[8 Jun 2021 9:43]
MySQL Verification Team
Hello Lukas Eder, Thank you for the report and test case. regards, Umesh
[10 Jun 2021 15:03]
Dmitry Lenev
Posted by developer: Hello! This is expected and documented limitation of auto-increment columns for InnoDB tables. Our manual says at https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html: --- 15.6.1.6 AUTO_INCREMENT Handling in InnoDB InnoDB provides a configurable locking mechanism that can significantly improve scalability and performance of SQL statements that add rows to tables with AUTO_INCREMENT columns. To use the AUTO_INCREMENT mechanism with an InnoDB table, an AUTO_INCREMENT column must be defined as part of an index such that it is possible to perform the equivalent of an indexed SELECT MAX(ai_col) lookup on the table to obtain the maximum column value. Typically, this is achieved by making the AUTO_INCREMENT column the first column of some table index. --- I agree that error message could be improved though.
[10 Jun 2021 15:17]
Lukas Eder
Thanks for the explanation. Would it perhaps make sense to add a synthetic index on the auto_increment column if it's not the first, as a workaround? I find the limitation rather arbitrary. If sequences were supported and AUTO_INCREMENT were backed by a sequence (with its own autonomous transaction), then this limitation wouldn't be there, I think?