MySQL Specified key was too long

2019-07-18 14:05发布

I don't understand the error I'm getting. The only field longer than 767 is password but it's not an index or anything.

mysql> CREATE TABLE users (
    ->         id INTEGER NOT NULL AUTO_INCREMENT,
    ->         email VARCHAR(256) NOT NULL,
    ->         password VARCHAR(1024) NOT NULL,
    ->         date_added INTEGER,
    ->         PRIMARY KEY (id),
    ->         UNIQUE (email)
    -> );
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

1条回答
对你真心纯属浪费
2楼-- · 2019-07-18 14:26

VARCHAR(1024)

MySQL stores VARCHAR values as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A VARCHAR column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

Prior to MySQL 5.0.3, a VARCHAR column with a length specification greater than 255 is converted to the smallest TEXT type that can hold values of the given length. For example, VARCHAR(500) is converted to TEXT, and VARCHAR(200000) is converted to MEDIUMTEXT.

Ref: http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html

http://dev.mysql.com/doc/refman/5.0/en/char.html

查看更多
登录 后发表回答