简体   繁体   English

在MySQL上创建表时的两个主键

[英]Two primary keys when creating a table on MySQL

I'm new to MySql! 我是MySql的新手! I'm trying to create a table with 2 primary key or what is usually called a composite key. 我正在尝试创建一个具有2个主键或通常称为复合键的表。

CREATE TABLE MovedProduct
(
   MigrationId nvarchar(150)   NOT NULL,
   ContextKey nvarchar(300)  NOT NULL,
   Model varbinary(65535) NOT NULL,
   ProductVersion nvarchar(32) NOT NULL,
   primary key(MigrationId,ContextKey)
)

but I'm getting an error that I can't figure out. 但是我遇到了一个我不知道的错误。

Error Code: 1071. Specified key was too long; max key length is 1000 bytes

I don't what to decrease the keys size because they can get up to their respective size. 我没有减小键的大小的方法,因为它们可以达到各自的大小。

Is there something special I need to do in order to have my table created? 为了创建表我需要做些特别的事情吗?

You should reduce you field length for the involved fields .. because some charset don't use only one byte for a char but more so .. when these fileds will stored could exeed the limit for keys length 您应该减少涉及字段的字段长度..因为某些字符集不只将一个字节用于一个字符,而是更多..当这些文件存储时,可能会超出密钥长度的限制

eg : 例如:

CREATE TABLE MovedProduct
(
   MigrationId nvarchar(64)   NOT NULL,
   ContextKey nvarchar(128)  NOT NULL,
   Model varbinary(65535) NOT NULL,
   ProductVersion nvarchar(32) NOT NULL,
   primary key(MigrationId,ContextKey)
)

you your charset use 3 bytes for store a char then in your sample you have 3*150 + 3*400 = 1650 您的字符集使用3个字节存储一个字符,那么在您的样本中,您有3 * 150 + 3 * 400 = 1650
instead 3*64 + 3*128 = 576 而是3 * 64 + 3 * 128 = 576

If you don't need utf8, then explicitly specify latin`: 如果不需要utf8,则显式指定latin`:

MigrationId varchar(150)  CHARACTER SET latin1  NOT NULL,
ContextKey  varchar(300)  CHARACTER SET latin1  NOT NULL,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM