开发者

What's wrong with this statement?

开发者 https://www.devze.com 2022-12-17 22:56 出处:网络
mysql> create table newsgroup( ->id integer unsigned NOT NULL AUTO_INCREMENT, ->creater integer unsigned NOT NULL,
mysql> create table newsgroup(
    ->  id integer unsigned NOT NULL AUTO_INCREMENT,
    ->  creater integer unsigned NOT NULL,
    ->  coremember integer unsigned DEFAULT NULL,
    ->  name varchar(300) not null un开发者_运维知识库ique,
    ->  description text,
    ->  created datetime not null,
    ->  PRIMARY KEY (id)
    -> );
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
mysql>

I changed 300 to 250,and it's ok.But I don't really understand.


Your database encoding is set to UTF8

An UTF8 character may take up to 3 bytes in MySQL, so 767 bytes is 255 characters.

Creating a UNIQUE index on such long text fields is not recommended.

Instead, create a plain prefixed index

CREATE INDEX ix_newsgroup_name ON newsgroup (name (30))

, which is enough for the prefixed searches, and add another column to store the MD5 hash which would ensure uniqueness.


767 bytes is the stated prefix limititation for InnoDB tables. :)

See here: http://dev.mysql.com/doc/refman/5.1/en/create-index.html


You're using utf-8 or even a heavier charset and thus every symbol is represented by one, two, three or four bytes. In MySQL utf8 stands for max 3-byte sequence, and utf8mb4 for max 4-byte sequence.


remove UNIQUE from the varchar.

0

精彩评论

暂无评论...
验证码 换一张
取 消