简体   繁体   English

为什么我的INSERT IGNORE代码不能正常工作?

[英]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. 我试图通过使用PRIMARY KEY和INSERT IGNORE方法避免将重复记录插入到我的表中。 As suggested @ http://www.tutorialspoint.com/mysql/mysql-handling-duplicates.htm 正如@ http://www.tutorialspoint.com/mysql/mysql-handling-duplicates.htm所建议的那样

I added the PRIMARY KEY to the tables definition as shown below: 我将PRIMARY KEY添加到表定义中,如下所示:

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: 在下面找到INSERT IGNORE代码:

 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. 期待您的反馈意见。

Your id column is auto incrementing which means each row is effectively unique when it is used in your key. 您的id列是自动递增的,这意味着每个行在您的密钥中使用时都是唯一的。 You should inspect the data that you've inserted. 您应该检查已插入的数据。 You should see that each duplicate row actually has a separate and distinct id . 您应该看到每个重复行实际上都有一个单独且不同的id

You can set a UNIQUE index on flightNo and status which would prevent the duplicate rows. 您可以在flightNostatus上设置UNIQUE索引,以防止重复的行。

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

And then I would recommend just reducing your Primary Key to be id 然后我建议只将主键减少为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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM