user198729
user198729

Reputation: 63636

What's wrong with this statement?

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 unique,
    ->  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.

Upvotes: 4

Views: 208

Answers (4)

raveren
raveren

Reputation: 18542

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.

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425371

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.

Upvotes: 6

Alex Brown
Alex Brown

Reputation: 42872

remove UNIQUE from the varchar.

Upvotes: -1

dmnkhhn
dmnkhhn

Reputation: 1023

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

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

Upvotes: 1

Related Questions