Postgres小写列并删除重复项

问题描述 投票:0回答:1

我有下表:

Customers
---------
name          text
object_id     integer
created_time  timestamp with time zone 

Indexes:
    "my_index" UNIQUE CONSTRAINT, btree (name, object_id, created_time)

唯一索引工作正常,但后来我得到了重复的数据,如:

Name  |  object_id  |  created_time
------------------------------------
john  | 1           | 2018-02-28 15:42:14.30573+00
JOHN  | 1           | 2018-02-28 15:42:14.30573+00

所以我尝试用名称列小写我的所有数据:

UPDATE customers SET name=lower(name) WHERE name != LOWER(name);

但是这个过程产生了错误,因为现在我违反了索引:

ERROR:  duplicate key value violates unique constraint "my_index"
DETAIL:  Key (name, object_id, created_time)=(john, 1, 2018-02-28 15:42:14.30573+00) already exists.

我可以使用什么样的过程来删除在转换为小写后生成索引违规的行?

postgresql indexing lowercase
1个回答
3
投票

如果你在桌子上有'JOHN''John'而不是'john'它会变得混乱。这是一个解决方案。

insert into customers
  select distinct lower("name") ,object_id,created_time from customers
    where name <> lower(name)
      and not (lower("name") ,object_id,created_time)
       in (select * from customers);

delete from customers where name <> lower(name);

之后考虑:

alter table customers alter column name type citext;
© www.soinside.com 2019 - 2024. All rights reserved.