2012-10-11 57 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