Reputation: 833
I am selecting data from a table students
and inserting it into the tests
table like
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;
I am using this code
insert ignore into tests (student_id,student_iden_number,student_names) select student_id,student_iden_number,student_names from students;
However if i run the insert ignore code twice,duplicates records are inserted.This is my students
table
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;
Why is it inserting the duplicate data instead of ignoring it?.
Upvotes: 0
Views: 56
Reputation: 71422
You don't have any primary key or unique keys on your tests
table, so nothing you insert will ever be considered a duplicate. The ignore
only comes into play when trying to insert a row that would cause duplicate key violations.
Upvotes: 5