gpt4 book ai didi

mysql - 插入两个表之间自动递增不重置

转载 作者:行者123 更新时间:2023-11-30 22:16:44 24 4
gpt4 key购买 nike

我有这个脚本:

#create departments table
CREATE TABLE `departments` (
`department_id` int(11) NOT NULL AUTO_INCREMENT,
`department_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`department_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

#inserts - departments table
INSERT INTO `departments`(`department_name`)VALUES("Parks and recreation");
INSERT INTO `departments`(`department_name`)VALUES("Portable immediate treatment");
INSERT INTO `departments`(`department_name`)VALUES("Veterinary");
INSERT INTO `departments`(`department_name`)VALUES("Gardening");
INSERT INTO `departments`(`department_name`)VALUES("Business Registration");
INSERT INTO `departments`(`department_name`)VALUES("Environment");
INSERT INTO `departments`(`department_name`)VALUES("Sanitation");
INSERT INTO `departments`(`department_name`)VALUES("Recycling");
INSERT INTO `departments`(`department_name`)VALUES("all");

#create departments_in_langueage table
CREATE TABLE `departments_in_language` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`department_id` int(11) DEFAULT NULL,
`language` varchar(45) DEFAULT NULL,
`description` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `departments_in_language_department_id_idx` (`department_id`),
CONSTRAINT `departments_in_language_department_id` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

#inserts - departments_in_language table
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("1","hebrew","חזות העיר");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("2","hebrew","ניידת טיפול מיידי");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("3","hebrew","ווטרינריה");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("4","hebrew","גינון");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("5","hebrew","רישוי עסקים");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("6","hebrew","איכות הסביבה");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("7","hebrew","תברואה");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("8","hebrew","מיחזור");
INSERT INTO `departments_in_language`(`department_id`,`language`,`description`)VALUES("9","hebrew","כללי");

Departments_in_language 的字段名为“department_id”(外键)。问题:当我运行这个脚本时,我得到了一些错误:

Cannot add or update a child row: a foreign key constraint fails (`teres`.`departments_in_language`, CONSTRAINT `departments_in_language_department_id` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`) ON DELETE NO ACTION ON UPDATE NO )

我看到department_in_language的Id不是从1开始的,这意味着“自动”的计数器不会在插入之间重置;这就是错误的原因。我能做什么?

最佳答案

您的表创建查询将 AUTO_INCREMENT 设置为 10 AND 9。它应该设置为 1。

#create departments table
CREATE TABLE `departments` (
[...]
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

#create departments_in_langueage table
CREATE TABLE `departments_in_language` (
[...]
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

关于mysql - 插入两个表之间自动递增不重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37993863/

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