What is the maximum columns number for a multicolumn primary key (MySQL)? Does it depend on column data type or engine?
I found this:
someone tried to create a PK with 2 columns and received this error:
"Specified key was too long; max key length is 1024 bytes" so it looks that the maximum it's 1024 bytes, it doesn't matter the columns number what really matter is the var type and the space assign it.
I have seen some examples like this:
create table OS_PROPERTYENTRY (entity_name VARCHAR(125) not null, entity_id BIGINT not null,
entity_key VARCHAR(255) not null date_val DATETIME, primary key (entity_name, entity_id, entity_key))
where
125 * (3 bytes) + 255 * (3 bytes) + 1 * (8 bytes) = 1148 bytes. So it's not possible to create a PK.
take a look here, they talk about it: https://jira.atlassian.com/browse/CONF-2783
Yes, it depends on storage engine.
MyISAM:
The maximum number of columns per index is 16. The maximum key length is 1000 bytes. This can also be changed by changing the source and recompiling. For the case of a key longer than 250 bytes, a larger key block size than the default of 1024 bytes is used.
InnoDB:
The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to 3072 bytes. This limit applies to the length of the combined index key in a multi-column index.