MySQL INSERT IGNORE or ON DUPLICATE KEY DO_NOTHING

already searched for such topics and found 2 different solutions, but no one works.

My table has a structure | ID (auto_increment primary_key) | UID (int) | FAV_ID (int) |

I need to insert a new record in this FAV_TABLE if the UID and FAV_ID (both) already exist.

An example of my request:

INSERT INTO FAV_TABLE (uid, fav_id) VALUES ($u_id,$s_id) ON DUPLICATE KEY UPDATE  uid = uid 

or this

INSERT IGNORE FAV_TABLE (uid, fav_id) VALUES ($u_id,$s_id);

As the mysql manuals say, this query does not add a record only if PRIMARY_KEY matches. And I need a request not to add an entry if the uid + fav_id pair is unique. Any solutions? Thanks you

+5
source share
2 answers

You need to add UNIQUE KEYto these columns:

ALTER TABLE FAV_TABLE ADD UNIQUE KEY(uid, fav_id);
+4
source

INSERT IGNOREor ON DUPLICATE KEYonly works when duplicating a unique key.

UNIQUE uid fav_id. , , .

+2

All Articles