Stupid.Fat.Cat
Stupid.Fat.Cat

Reputation: 11295

mySQL INSERT IGNORE doesn't "ignore"

From my understanding INSERT IGNORE inserts a new entry if it doesn't already exists, if it does, ignore it. So I've been trying to do that for a while and it doesn't seem to be working. Here's my attempt:

insert insert ignore into rss_feeds (md5sum) values ("1234");
Query OK, 1 row affected (0.00 sec)

mysql> insert ignore into rss_feeds (md5sum) values ("1234");
Query OK, 1 row affected (0.00 sec)

mysql> insert ignore into rss_feeds (md5sum) values ("1234");
Query OK, 1 row affected (0.00 sec)

mysql> insert ignore into rss_feeds (md5sum) values ("1234");
Query OK, 1 row affected (0.00 sec)

mysql> select * from rss_feeds where md5sum="1234";
+------+--------+----------+---------+----------+--------+--------+---------+
| link | source | headline | updated | inserted | md5sum | itemid | emailed |
+------+--------+----------+---------+----------+--------+--------+---------+
| NULL | NULL   | NULL     | NULL    | NULL     | 1234   |   NULL |    NULL |
| NULL | NULL   | NULL     | NULL    | NULL     | 1234   |   NULL |    NULL |
| NULL | NULL   | NULL     | NULL    | NULL     | 1234   |   NULL |    NULL |
| NULL | NULL   | NULL     | NULL    | NULL     | 1234   |   NULL |    NULL | 
+------+--------+----------+---------+----------+--------+--------+---------+
4 rows in set (0.00 sec)

Upvotes: 2

Views: 2939

Answers (2)

eggyal
eggyal

Reputation: 125855

As documented under INSERT Syntax:

without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row still is not inserted, but no error is issued.

You need to define a UNIQUE index on md5sum:

ALTER TABLE rss_feeds ADD UNIQUE INDEX (md5sum);

Upvotes: 8

Matthew Scragg
Matthew Scragg

Reputation: 4638

IGNORE will ignore errors produced by any unique key constraints. Basically it will just ignore any errors that occur doing the insert.

Upvotes: 0

Related Questions