#1071 - Specified key was too long; max key length is 767 bytes

后端 未结 2 2089
醉梦人生
醉梦人生 2021-01-15 13:35

I this SQL query to create a table:

CREATE TABLE IF NOT EXISTS `local_sysDB`.`hashtags` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `hashtag` VARCHAR(255) NOT NU         


        
相关标签:
2条回答
  • 2021-01-15 14:01

    "Hashes" are usually hex, not UTF-8. Hashes are usually much shorter than 255.

    If either applies, then...

    `hashtag`
           VARCHAR(160)           -- this
           CHARACTER SET ascii    -- and/or this
    

    would be a solution that would work on any version without any of the innodb settings indicated.

    (Note: 191 is the cutoff for VARCHAR with utf8mb4, but few standard hashes need that much.)

    0 讨论(0)
  • 2021-01-15 14:26

    I just learned a workaround... Get 5.5.14 or 5.6.3 (or later), do the SETs indicated here, and use DYNAMIC or COMPRESSED:

    SET GLOBAL innodb_file_per_table = ON,
               innodb_file_format = Barracuda,
               innodb_large_prefix = ON;
    CREATE TABLE so29676724 (
      `id` INT NOT NULL AUTO_INCREMENT,
      `hashtag` VARCHAR(255) NOT NULL COMMENT 'hashtag must be unique. Must be saved without #',
       PRIMARY KEY (`id`),
      UNIQUE INDEX `hashtags_hashtag` (`hashtag` ASC)
    )
    ENGINE = InnoDB
    DEFAULT CHARACTER SET  utf8mb4
    ROW_FORMAT = COMPRESSED;
    
    SHOW CREATE TABLE so29676724\G
    
    mysql> CREATE TABLE so29676724 (
        ->   `id` INT NOT NULL AUTO_INCREMENT,
        ->   `hashtag` VARCHAR(255) NOT NULL COMMENT 'hashtag must be unique. Must be saved without #',
        ->    PRIMARY KEY (`id`),
        ->   UNIQUE INDEX `hashtags_hashtag` (`hashtag` ASC)
        -> )
        -> ENGINE = InnoDB
        -> DEFAULT CHARACTER SET  utf8mb4
        -> ROW_FORMAT = COMPRESSED;
    Query OK, 0 rows affected (0.09 sec)
    
    0 讨论(0)
提交回复
热议问题