Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I have a table with a unique key for two columns:

CREATE  TABLE `xpo`.`user_permanent_gift` (

`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,

`fb_user_id` INT UNSIGNED NOT NULL ,

`gift_id` INT UNSIGNED NOT NULL ,

`purchase_timestamp` TIMESTAMP NULL DEFAULT now() ,

PRIMARY KEY (`id`) ,

UNIQUE INDEX `user_gift_UNIQUE` (`fb_user_id` ASC, `gift_id` ASC) );

I want to insert a row into that table, but if the key exists, to do nothing! I don't want an error to be generated because the keys exist.

I know that there is the following syntax:

INSERT ... ON DUPLICATE KEY UPDATE ...

but is there something like:

INSERT ... ON DUPLICATE KEY DO NOTHING 

?

1 Answer

0 votes
by (40.7k points)

You can use INSERT IGNORE.

Or else, you can do ON DUPLICATE KEY UPDATE id=id.

Related questions

0 votes
1 answer
asked Jan 6, 2021 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 24, 2019 in SQL by Tech4ever (20.3k points)

Browse Categories

...