gpt4 book ai didi

mysql - SQL 错误 : 1170

转载 作者:行者123 更新时间:2023-11-30 21:58:09 25 4
gpt4 key购买 nike

运行以下代码时出现以下错误...这是怎么回事?

Error Code:1170. BLOB/TEXT column 'message" used in key specification without a key length.

-- MySQL Script generated by MySQL Workbench
-- Sun Jun 4 21:25:42 2017
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(255) NOT NULL,
`last_name` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`created_at` DATETIME NOT NULL,
`updates_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `first_name_UNIQUE` (`first_name` ASC),
UNIQUE INDEX `last_name_UNIQUE` (`last_name` ASC),
UNIQUE INDEX `email_UNIQUE` (`email` ASC),
UNIQUE INDEX `password_UNIQUE` (`password` ASC),
UNIQUE INDEX `created_at_UNIQUE` (`created_at` ASC),
UNIQUE INDEX `updates_at_UNIQUE` (`updates_at` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`messages`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`messages` (
`id` INT NOT NULL AUTO_INCREMENT,
`users_id` INT NOT NULL,
`message` TEXT NOT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_messages_users_idx` (`users_id` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC),
UNIQUE INDEX `users_id_UNIQUE` (`users_id` ASC),
UNIQUE INDEX `message_UNIQUE` (`message` ASC),
UNIQUE INDEX `created_at_UNIQUE` (`created_at` ASC),
UNIQUE INDEX `updated_at_UNIQUE` (`updated_at` ASC),
CONSTRAINT `fk_messages_users`
FOREIGN KEY (`users_id`)
REFERENCES `mydb`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`comments`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`comments` (
`id` INT NOT NULL AUTO_INCREMENT,
`users_id` INT NOT NULL,
`messages_id` INT NOT NULL,
`comment` TEXT NOT NULL,
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_comments_users1_idx` (`users_id` ASC),
INDEX `fk_comments_messages1_idx` (`messages_id` ASC),
UNIQUE INDEX `id_UNIQUE` (`id` ASC),
UNIQUE INDEX `users_id_UNIQUE` (`users_id` ASC),
UNIQUE INDEX `messages_id_UNIQUE` (`messages_id` ASC),
UNIQUE INDEX `comment_UNIQUE` (`comment` ASC),
UNIQUE INDEX `created_at_UNIQUE` (`created_at` ASC),
UNIQUE INDEX `updated_at_UNIQUE` (`updated_at` ASC),
CONSTRAINT `fk_comments_users1`
FOREIGN KEY (`users_id`)
REFERENCES `mydb`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_comments_messages1`
FOREIGN KEY (`messages_id`)
REFERENCES `mydb`.`messages` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

最佳答案

InnoDB 索引的最大长度默认为 767 字节。

当您在最大长度为 65535 字节的 TEXT 列上声明唯一索引时,它超出了最大索引长度很多。

UNIQUE INDEX `message_UNIQUE` (`message` ASC),

您可以启用 innodb_large_prefix,这使得创建 3072 字节的索引成为可能,这对于完整的 TEXT 列来说也太小了。

您可以为 TEXT 列使用前缀索引,但这不是强制唯一性的好解决方案。

INDEX `message` (`message`(255) ASC),

您应该重新考虑是否需要对 TEXT 列进行唯一约束。

另见:

关于mysql - SQL 错误 : 1170,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com