2012-10-11 59 views
6

多列主键(MySQL)的最大列数是多少?它依赖于列数据类型还是引擎?MySQL多列主键

+0

其16 SQL Server 2008中..不知道生根粉的MySQL –

+0

[本文是从2006年(HTTP ://www.xaprb.com/blog/2006/04/17/max-key-length-in-mysql/),它表示主键的最大长度是1000字节。我不确定自那以后有没有改变。 – Taryn

+0

我认为MyiSAM的限制是1000字节,InnoDB引擎的限制是3072。 –

回答

6

是的,这取决于存储引擎。

  • 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.

+0

谢谢。很好的答案。 –

1

我发现这一点:

有人试图创建2列的PK和收到此错误:
“指定的键过长,最大密钥长度为1024个字节”,所以它看起来是最大它是1024个字节,列号无关紧要var类型和空间分配它真的很重要。

我看到一些示例是这样的:

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)) 

其中

125 *(3个字节)+ 255 *(3个字节)+ 1 *(8个字节)= 1148字节。所以创建一个PK是不可能的。

看看这里,他们谈论它:https://jira.atlassian.com/browse/CONF-2783