我有下表:
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.
在强制转换为小写字母后会发生索引冲突的情况下,我可以使用哪种程序删除行?
答案 0 :(得分:3)
如果表中有source
和'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);