gpt4 book ai didi

mysql - insert ignore 无法阻止重复插入

转载 作者:行者123 更新时间:2023-12-01 00:49:25 26 4
gpt4 key购买 nike

我从表students 中选择数据并将其插入到tests 表中

 CREATE TABLE tests(
`student_id` int NOT NULL,
`student_iden_number` varchar(255) NOT NULL DEFAULT '000000',
`student_names` varchar(255) NOT NULL DEFAULT 'jane doe'
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

我正在使用这个代码

insert ignore into tests (student_id,student_iden_number,student_names) select student_id,student_iden_number,student_names from students;

但是,如果我运行插入忽略代码两次,则会插入重复记录。这是我的students

CREATE TABLE `students` (
`student_id` INT NOT NULL AUTO_INCREMENT,
`student_iden_number` varchar(255) NOT NULL DEFAULT '000000',
`student_names` varchar(255) NOT NULL DEFAULT 'jane doe',
`student_gender` ENUM('female', 'male') DEFAULT 'female',
`student_guardian_names` varchar(255) NOT NULL DEFAULT 'the guardian',
`student_guardian_telephone` varchar(255) NOT NULL DEFAULT '0000000',
`student_date_of_birth` DATE DEFAULT '00-00-0000',
`student_date_entered` DATE DEFAULT '00-00-0000',
`student_date_left` DATE DEFAULT '00-00-0000',
`student_current_status` ENUM('in-session', 'suspended', 'expelled-for-good', 'at-home-sick', 'cleared' ,'other') DEFAULT 'other',
`student_profile` varchar(255) DEFAULT 'lorem ipsum',
`student_picture` varchar(255) NOT NULL DEFAULT 'placeholder.png',
`student_has_a_medical_condition` ENUM('no', 'yes') DEFAULT 'no',
`student_name_of_condition` varchar(255) NOT NULL DEFAULT 'none',
`student_current_home_address` varchar(255) NOT NULL DEFAULT 'home address',
`student_current_home_latitude` varchar(255) NOT NULL DEFAULT '0000-00-000',
PRIMARY KEY (`student_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

为什么要插入重复数据而不是忽略它?

最佳答案

tests 表中没有任何主键或唯一键,因此您插入的任何内容都不会被视为重复项。 ignore 仅在尝试插入会导致重复键违规的行时发挥作用。

关于mysql - insert ignore 无法阻止重复插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659494/

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