SirBT
SirBT

Reputation: 1698

Why isn't my INSERT IGNORE code NOT working?

I am trying to avoid inserting duplicates records into my table by using the PRIMARY KEY and INSERT IGNORE methods. As suggested @ http://www.tutorialspoint.com/mysql/mysql-handling-duplicates.htm

I added the PRIMARY KEY to the tables definition as shown below:

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("flightSched") or die(mysql_error());

mysql_query("CREATE TABLE Alteration(
id INT NOT NULL AUTO_INCREMENT, 

 timePeriod TIME default '00:00:00',
 depOrArriv VARCHAR(9),
 flightNo VARCHAR(9) NOT NULL,
 airline VARCHAR(20),
 dest VARCHAR(30),
 origin VARCHAR(30),
 depature VARCHAR(8),
 don VARCHAR(10),   
 arrivalTime VARCHAR(8),
 arrivalTimeSec VARCHAR(28),
 status VARCHAR(15) NOT NULL,

 image_type      varchar(25) not null default '',
 image           blob        not null,
 image_size      varchar(25) not null default '',
 image_name      varchar(50) not null default '',
PRIMARY KEY (id, flightNo, status)

)")
 or die(mysql_error());  

echo "Table Created!";

Find below the INSERT IGNORE code:

 mysql_query("INSERT IGNORE INTO Alteration 
                     (depOrArriv, flightNo, airline, origin, don, arrivalTime, arrivalTimeSec, status, image_type, image, image_size,                           image_name) 
                    VALUES('$depOrArriv', '$flightNo', '$airline', '$origin', '$don', '$arrivalTime', '$arrivalTime', '$status',                            '$image_type','$image', '$image_size', '$image_name' ) "); 
//                  or die(mysql_error());  

                    echo "Number of affected rows were: " . mysql_affected_rows();

While testing it I noticed that it STILL inserts duplicate records. Why is it still doing this? Can anyone help me point out what is wrong?

Any help is greatly appreciated. Looking forward to your feedback.

Upvotes: 3

Views: 1789

Answers (1)

Brendan Bullen
Brendan Bullen

Reputation: 11819

Your id column is auto incrementing which means each row is effectively unique when it is used in your key. You should inspect the data that you've inserted. You should see that each duplicate row actually has a separate and distinct id.

You can set a UNIQUE index on flightNo and status which would prevent the duplicate rows.

ALTER TABLE `Alteration` ADD UNIQUE (
    `flightNo` ,
    `status`
);

And then I would recommend just reducing your Primary Key to be id

UPDATE As requested, this is a modified version of your code with a unique index used to prevent the duplicates:

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("flightSched") or die(mysql_error());

mysql_query("CREATE TABLE Alteration(
 id INT NOT NULL AUTO_INCREMENT,
 timePeriod TIME default '00:00:00',
 depOrArriv VARCHAR(9),
 flightNo VARCHAR(9) NOT NULL,
 airline VARCHAR(20),
 dest VARCHAR(30),
 origin VARCHAR(30),
 depature VARCHAR(8),
 don VARCHAR(10),
 arrivalTime VARCHAR(8),
 arrivalTimeSec VARCHAR(28),
 status VARCHAR(15) NOT NULL,
 image_type varchar(25) not null default '',
 image blob not null,
 image_size varchar(25) not null default '',
 image_name varchar(50) not null default '',
 PRIMARY KEY (id),
 UNIQUE KEY `flightNo` (`flightNo`,`status`)
)") or die(mysql_error());

echo "Table Created!";

mysql_query("INSERT IGNORE INTO Alteration (depOrArriv, flightNo, airline, origin, don, arrivalTime, arrivalTimeSec, status, image_type, image, image_size, image_name) VALUES('$depOrArriv', '$flightNo', '$airline', '$origin', '$don', '$arrivalTime', '$arrivalTime', '$status', '$image_type','$image', '$image_size', '$image_name' )");

echo "Number of affected rows were: " . mysql_affected_rows();

Upvotes: 2

Related Questions