MySQL指定的密钥太长

MySQL指定的密钥太长

问题描述:

我不明白我得到的错误.长度大于767的唯一字段是password,但它不是索引或任何内容.

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

VARCHAR(1024)

MySQL将VARCHAR值存储为1字节或2字节长的前缀以及数据.长度前缀指示值中的字节数.如果值要求不超过255个字节,则VARCHAR列将使用一个长度字节;如果值可能需要不超过255个字节,则VARCHAR列将使用两个长度字节.

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.

在MySQL 5.0.3之前,长度指定大于 255 的VARCHAR列将转换为可以容纳给定长度值的最小TEXT类型.例如,VARCHAR(500)转换为TEXT,而VARCHAR(200000)转换为MEDIUMTEXT.

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.

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

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