MySQL error: key specification without a key length

前端 未结 16 1308
别跟我提以往
别跟我提以往 2020-11-22 08:41

I have a table with a primary key that is a varchar(255). Some cases have arisen where 255 characters isn\'t enough. I tried changing the field to a text, but I get the foll

16条回答
  •  野性不改
    2020-11-22 09:12

    You should define which leading portion of a TEXT column you want to index.

    InnoDB has a limitation of 768 bytes per index key and you won't be able to create an index longer than that.

    This will work fine:

    CREATE TABLE t_length (
          mydata TEXT NOT NULL,
          KEY ix_length_mydata (mydata(255)))
        ENGINE=InnoDB;
    

    Note that the maximum value of the key size depends on the column charset. It's 767 characters for a single-byte charset like LATIN1 and only 255 characters for UTF8 (MySQL only uses BMP which requires at most 3 bytes per character)

    If you need your whole column to be the PRIMARY KEY, calculate SHA1 or MD5 hash and use it as a PRIMARY KEY.

提交回复
热议问题