Skip to content
Advertisement

Avoid duplicates in postgreSQL

I’m using postgreSQL version 10.3 and I have a table like this:

I want to make numerous INSERT and make sure there are no duplicates. So I thought about using INSERT INTO ... ON CONFLICT DO NOTHING;.

The idea is to insert the record only if the values on the columns first | second | third | fourth | fifth are not already present in the table. The problem is that the id is incremented automatically, so each record is different and therefore the INSERT always takes place.

Here we see what I mean:

The record (2 | 11 | 22 | 33 | 44 | 55) is equal to the record (4 | 1 | 2 | 3 | 4 | 5). They differ only for the id. I would not like the record (4 | 1 | 2 | 3 | 4 | 5) to be inserted. How can I do?

Thank you

Advertisement

Answer

Use UNIQUE CONSTRAINT:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement