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

标签 sql postgresql unique-constraint

假设我有一张表 :employers 和 :employee。我想添加以下内容:

ALTER TABLE employers ADD UNIQUE (employee_id);

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

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

最佳答案

创建部分唯一索引

create unique index index_unique_employer_id on employers(employer_id)
where employer_id > 10

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

关于sql - 当已经有重复的旧记录时添加唯一性约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40159331/

相关文章:

python - 根据重复提取一定数量的行?

sql - Oracle RDBMS 记录是否有索引或时间戳?如何获取?

php - MySQL如何将表中的数据直接检索到变量?

python - SQLAlchemy "default"与 "server_default"性能对比

ruby-on-rails - 查找多个关联匹配的 Active Record 对象(相同字段)

postgresql - 使用函数索引优化 PostgreSQL?

sql - 如何在多列的唯一索引中允许一列为 NULL 值

mysql - 数据库设计 : need composite key + foreign key Part 2

java - Postgres 在尝试截断表时提示缺少列

sql - 动态链接服务器查询