views:

50

answers:

4

Hello

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?

A: 

Yes.

SELECT * FROM tablenamegoeshere WHERE fieldicareabout="valueitalreadyhas"
Ignacio Vazquez-Abrams
thank you for this simple query
omid8bimo
Don't thank me for this. Just learn what an index is and how it works.
Ignacio Vazquez-Abrams
A: 

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.

Peter Lang
i dont know the conflicting_value! i just "ERROR: duplicate key value violates unique constraint "user_audit_log_pkey"
omid8bimo
+1  A: 

First of all, determine which fields in your table have to be unique. This may be something marked as a Primary Key, a unique index based on one or more fields or a check constraint, again based on one or more fields.

Once you've done that, look at what you're trying to insert and work out whether it busts any of the unique rules.

And yes, SELECT statements will help you determine what's wrong here. Use those to determine whether you are able to commit the row.

Paul Alan Taylor
A: 

SELECT count(column_name), column_name from table_name group by column_name having count(column_name) > 1;

cserepj
what it is supposed to do? i get 0 rows!
omid8bimo