sql - 如何在 Postgres 9.4 中忽略没有唯一约束的重复项?
问题描述
我目前在我们的旧数据库(postgres 9.4)表中面临一个问题,其中包含一些重复的行。我想确保不再生成重复的行。
但我也想保留已经生成的重复行。因此,我无法对这些列(多列)应用唯一约束。
我创建了一个触发器,它将检查该行是否已存在并相应地引发异常。但是在处理并发事务时它也会失败。
例子 :
TAB1
col1 | col2 | col3 |
------------------------------------
1 | A | B | --
2 | A | B | -- already present duplicates for column col2 and col3(allowed)
3 | C | D |
INSERT INTO TAB1 VALUES(4 , 'A' , 'B') ; -- This insert statement will not be allowed.
注意:由于数据库版本较旧,我无法在冲突中使用。
解决方案
大概,您不希望新行重复历史行。如果是这样,您可以这样做,但它需要修改表并添加新列。
alter table t add duplicate_seq int default 1;
然后更新此列以识别现有重复项:
update t
set duplicate_seq = seqnum
from (select t.*, row_number() over (partition by col order by col) as seqnum
from t
) tt
where t.<primary key> = tt.<primary key>;
现在,创建一个唯一索引或约束:
alter table t add constraint unq_t_col_seq on t(col, duplicate_seq);
插入行时,不要为duplicate_seq
. 默认值为1
. 这将与任何现有值冲突 - 或与最近输入的重复值冲突。历史重复将被允许。
推荐阅读
- c# - 查找我的 WPF 应用程序正在运行的当前显示屏幕尺寸
- visual-studio-code - 如何在 vscode tasks.json“args”键中为某些任务命令添加 shell 命令
- sql - GBQ SQL:如果 A 列中的值 X <> 0,则在 B 列中计数不同
- linux - 无法从 bash 命令写入 .service 文件
- oracle - 在 PLSQL 的 PROCEDURE 中使用 IF 和 UPDATE
- r - 合并两个具有共同 x 轴的 coord_polar 图并添加 x 轴比例尺
- mysql - 假日数据规范化
- python - 尝试在 Dataframe 中的列上应用 count() 或 len() 函数时出现类型错误
- java - Spring JDBC 中具有不同源和目标数据库的多租户
- javascript - Discord Bot 提醒或警报