Gurom
Gurom

Reputation: 61

How to ignore duplicate values for UNIQUE field?

I have this table

CREATE TABLE my_table 
(
    my_column varchar(10) UNIQUE
);

I want to insert duplicate values. PostgreSQL throws an ERROR about the UNIQUE column.

How to ignore the duplicate rows being inserted?

For example:

INSERT INTO my_table (my_column) 
VALUES ('cat'), ('dog'), ('cat') ;

I want to have table with this data after this statement:

cat
dog

Upvotes: 0

Views: 56

Answers (1)

user330315
user330315

Reputation:

Use on conflict

INSERT INTO my_table (my_column) 
VALUES 
('cat'), ('dog'),('cat') 
on conflict do nothing;

Upvotes: 3

Related Questions