MySQL:Specified key was too long; max key length is 767 bytes.

Construction of the table statement:

CREATE TABLE IF Not EXISTS api (
    api  varchar(500) not null ,
    method varchar(50) not null default 'POST',

    PRIMARY key (api,method)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

Error: 

Specified key was too long; max key length is 767 bytes.

the reason:

  Here I use the api, method to do the primary key api table above, will be indexed according to these two fields, charset is utf8, which is a three-byte characters, then the index of total bytes: 500 * 3 + 3 * 50 = 1650 bytes, the index is required mysql 767 bytes.

solve:

  The api field to varchar (200), method to varchar (20)

 

Or you can first n characters defined as an index field, reference: https://blog.csdn.net/u010429286/article/details/80418666  to solve

UNIQUE KEY `uniq_store_code` (`record_date`,`store_code`(20),`sku_division_code`(20),`abc`(20))

Means that the subsequently store_code, sku_division_code, abc three character field 20 from the former as a unique index, so that length is not exceeded.

 

Guess you like

Origin www.cnblogs.com/feiquan/p/11571745.html