当已有重复的旧记录时添加唯一性约束

LOC*_*OCK 6 sql postgresql unique-constraint

假设我有一张表:雇主和:雇员。我想添加以下内容:

ALTER TABLE employers ADD UNIQUE (employee_id);
Run Code Online (Sandbox Code Playgroud)

这在开发中运行良好,但由于生产中已经存在重复记录,因此它在生产中不起作用。

有没有办法添加唯一性约束的序列号,即仅对新记录施加唯一性约束?

Clo*_*eto 4

创建部分唯一索引

create unique index index_unique_employer_id on employers(employer_id)
where employer_id > 10
Run Code Online (Sandbox Code Playgroud)

https://www.postgresql.org/docs/current/static/sql-createindex.html