Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate value in a postgresql table

Tags:

sql

postgresql

I'm trying to modify a table inside my PostgreSQL database, but it says there is duplicate! what is the best way to find a duplicate value inside a table? kinda a select query?

like image 493
kevoroid Avatar asked Dec 06 '22 03:12

kevoroid


2 Answers

Try Like This

SELECT count(column_name), column_name 
from table_name 
group by column_name having count(column_name) > 1;
like image 192
cserepj Avatar answered Jan 08 '23 13:01

cserepj


If you try to change a value in a column that is part of the PRIMARY KEY or has a UNIQUE constraint and get this error there, then you should be able to find the conflicting row by

SELECT *
FROM your_table
WHERE conflicting_column = conflicting_value;

If conflicting_value is a character type, put it in single quotes (').

EDIT: To find out which columns are affected by the constraint, check this post.

like image 31
Peter Lang Avatar answered Jan 08 '23 11:01

Peter Lang