开发者

Duplicate value in a postgresql table

开发者 https://www.devze.com 2022-12-16 09:29 出处:网络
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?Try Li

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?


Try Like This

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号