赞
踩
已存在数据的表,发现某字段缺失唯一性约束,现在要添加唯一性约束,操作如下:
-- 添加唯一性约束,延迟生效
alter table t add constraint uq_t_col1 unique (col1) using index
deferrable enable novalidate;
-- alter table t add constraint pk_t primary key (col1)
-- deferrable enable novalidate;
-- 查看重复数据
select col1,count(*) from t
group by col1 having count(*)>1;
-- 数据滤重
delete from t where rowid not in (
select min(rowid) from t group by col1);
-- 修改约束状态,恢复正常
alter table t enable validate constraint uq_t_col1;
使用参数deferrable指定延迟生效:
deferrable disable novalidate
--不生效,不校验已有数据
deferrable disable validate
--不生效,校验已有数据
deferrable enable novalidate
--生效,不校验已有数据
deferrable enable validate
--生效,校验已有数据,也就是默认状态
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。