Oracle唯一索引在字段全部为NULL时,不做唯一性判断,允许重复插入,而在8t中即使均为NULL值也会做重复值判断,在某些场景下客户会存在此类需求,在数据量不大不存在性能问题的情况下可以考虑通过如下方式进行替代

示例表

create table "informix".secconstitute

(

id decimal(20,0) not null ,

code varchar(32),

name varchar(64),

sec_id decimal(20,0),

meas_id decimal(20,0),

constitute_type decimal(10,0),

order_no decimal(10,0),

meas_value decimal(22,6),

ttc_therm decimal(22,6),

region_id decimal(10,0),

replicate_flag decimal(10,0),

download_region decimal(10,0),

operator_str varchar(64),

factor_str varchar(64),

if_reverse decimal(3,0),

condi_type decimal(10,0),

primary key (id) constraint "informix".pk_secconstitute

) extent size 64 next size 64 lock mode row;

revoke all on "informix".secconstitute from "public" as "informix";

create unique index "informix".idx_secid_measid_constitutetype_orderno

on "informix".secconstitute (sec_id,meas_id,constitute_type,

order_no) using btree in dbs3;

思路

1.删除原唯一索引替换为普通索引维持索引功能

2.通过触发器调用SPL进行非NULL值的唯一性判断,必要时中止操作

代码如下

drop index if exists index_438_1;

create index index_438_1 on secconstitute(sec_id,meas_id,constitute_type,order_no);

drop procedure if exists p4_ti_secconstitute_proc;

create procedure p4_ti_secconstitute_proc() referencing new as new for secconstitute

define v_str varchar(100);

define v_col varchar(100);

define v_sql lvarchar(500);

define v_count int8;

define v_flag int;

let v_col='';

let v_str='';

let v_count=0;

let v_sql='';

if new.sec_id is not null or new.meas_id is not null or new.constitute_type is not null or new.order_no is not null then

if new.sec_id is not null then

let v_col='sec_id';

let v_str='sec_id='||new.sec_id;

else

let v_col='sec_id';

let v_str='sec_id is null';

end if;

if new.meas_id is not null then

if v_col != '' then

let v_col=v_col||','||'meas_id';

else

let v_col='meas_id';

end if;

if v_str != '' then

let v_str=v_str||' and meas_id='||new.meas_id;

else

let v_str='meas_id='||new.meas_id;

end if;

else

if v_col != '' then

let v_col=v_col||','||'meas_id';

else

let v_col='meas_id';

end if;

if v_str != '' then

let v_str=v_str||' and meas_id is null';

else

let v_str='meas_id is null';

end if;

end if;

if new.constitute_type is not null then

if v_col != '' then

let v_col=v_col||','||'constitute_type';

else

let v_col='constitute_type';

end if;

if v_str != '' then

let v_str=v_str||' and constitute_type='||new.constitute_type;

else

let v_str='constitute_type='||new.constitute_type;

end if;

else

if v_col != '' then

let v_col=v_col||','||'constitute_type';

else

let v_col='constitute_type';

end if;

if v_str != '' then

let v_str=v_str||' and constitute_type is null';

else

let v_str='constitute_type is null';

end if;

end if;

if new.order_no is not null then

if v_col != '' then

let v_col=v_col||','||'order_no';

else

let v_col='order_no';

end if;

if v_str != '' then

let v_str=v_str||' and order_no='||new.order_no;

else

let v_str='order_no='||new.order_no;

end if;

else

if v_col != '' then

let v_col=v_col||','||'order_no';

else

let v_col='order_no';

end if;

if v_str != '' then

let v_str=v_str||' and order_no is null';

else

let v_str='order_no is null';

end if;

end if;

if v_str != '' then

let v_sql='select count(*) from secconstitute where '||v_str||' group by '||v_col;

prepare p from v_sql;

declare c cursor for p;

open c;

fetch c into v_count;

free p;

close c;

free c;

end if;

if v_count >1 then

raise exception -746,0,'Duplicated Sec_Id,Meas_Id,Constitue_Type,Order_No Value Founded!!';

end if;

end if;

end procedure;

oracle普通索引改唯一索引,Oracle唯一索引功能替代相关推荐

  1. oracle 唯一约束 和 唯一索引

    唯一性约束 唯一性约束指表中一个字段或者多个字段联合起来能够唯一标识一条记录的约束.联合字段中,可以包含空值. 注:在Oracle中,唯一性约束最多可以有32列. 唯一性约束可以在创建表时或使用ALT ...

  2. oracle主键和唯一索引,Oracle 主键、唯一键与唯一索引的区别

    如果我们让主键约束或者唯一键约束失效,Oracle自动创建的唯一索引是否会受到影响? SQL> drop table test purge; Table dropped. SQL> cre ...

  3. oracle唯一索引能删除吗,Oracle:ora-02429:无法用于删除强制唯一/主键的索引 解决...

    今天打算删除orcale数据库中无用的表空间,发现报错,查资料删除,写个过程留着备用. 1.drop tablespace dldata INCLUDING CONTENTS CASCADE CONS ...

  4. Oracle 删除表空间错误 提示:ora-02429:无法删除用于强制唯一/主键的索引

    sql>droptablespace xxx ora-01549:表空间非空,请使用INCLUDING CONTENTS 选项 sql>droptablespace xxx INCLUDI ...

  5. 各种oracle索引类型介绍,各种Oracle索引类型介绍

    逻辑上: Single column 单行索引 Concatenated 多行索引 Unique 唯一索引 NonUnique 非唯一索引 Function-based函数索引 Domain 域索引 ...

  6. oracle树子类遍历父类_不懂数据库索引的底层原理?那是因为你心里没点b树

    前几天下班回到家后正在处理一个白天没解决的bug,厕所突然传来对象的声音: 对象:xx,你有<时间简史>吗? 我:我去!妹子,你这啥癖好啊,我有时间也不会去捡屎啊! 对象:...人家说的是 ...

  7. Oracle索引梳理系列(八)- 索引扫描类型及分析(高效索引必备知识)

    理解oracle索引扫描类型的特点以及具体触发的条件,对于通过合理地使用索引,进行sql优化至关重要(例如组合索引的引导列的选择问题). 在总结索引扫描类型前,需要再次强调关于索引特点的几个关键点: ...

  8. c oracle 多条语句,Oracle 实践:如何编写一条 sql 语句获取数据表的全部索引信息(兼容 Oracle 19c、Oracle 11g)...

    一.引言 部门使用 Oracle 已经有一些时日,最近在工作中遇到了这么一个需求: 我们希望拿到某些数据表的全部索引信息,对索引信息进行检查,检查是否有漏掉没有创建的索引 这个需求,核心的点在于,我需 ...

  9. oracle的优化适用于mysql吗_性能优化之数据库优化,适用于Sqlite、Mysql、Oracle、Sql server,详细介绍了索引和事务及部分针对Sqlite的优化...

    本文为性能优化的第一篇--数据库性能优化,原理适用于大部分数据库包括Sqlite.Mysql.Oracle.Sql server,详细介绍了索引(优缺点.分类.场景.规则)和事务,最后介绍了部分单独针 ...

  10. Oracle教程之管理索引(一)--Oracle管理索引

    1.索引的创建语法: CREATE UNIUQE | BITMAP INDEX <schema>.<index_name> ON <schema>.<tabl ...

最新文章

  1. (拆点+最小路径覆盖) bzoj 2150
  2. virtualbox+vagrant学习-2(command cli)-16-vagrant snapshot命令
  3. this.Index = this._count - 1
  4. 中石油训练赛 - Russian Dolls on the Christmas Tree(树上启发式合并/主席树)
  5. 福州大学计算机学院董晨老师,福州大学代表队高分斩获第三届福建省高校网络空间安全大赛冠军...
  6. bzoj1925: [Sdoi2010]地精部落 [dp]
  7. el-input的使用
  8. (四)2005年我的第一次软件行业创业,烧掉30万、2年时间打水漂的惨痛教训
  9. html 自定义标签 ios,iOS标签 | 菜鸟教程
  10. 安恒赛php_安恒四月赛部分Writeup
  11. 云计算业务优势扩大,阿里云业务持续高速增长
  12. GNSS精密轨道产品sp3与精密钟差产品clk批量下载(Linux Shell)
  13. Spring中实现HTTP缓存
  14. 记录93年男孩日常消费网店收入
  15. 微信信息如何在服务器加密,微信 消息加解密说明
  16. Toast的使用详解
  17. PLC模拟量数据的处理
  18. 2019河北单招计算机考试模拟试题,2019年河北单招文科数学模拟试题(一)【含答案】.docx...
  19. 树莓派3B+安装wordpress
  20. Wyn Enterprise 嵌入式商业智能和报表软件

热门文章

  1. k8s 删除资源卡住
  2. AutoCAD 二次开发
  3. 重装电脑xp系统找不到服务器,技术员为你解答XP系统提示“没有启动服务器服务”的修复方案...
  4. 2021-01-07
  5. Chrome调试器中的“警告:显示了临时标题”
  6. JVM07 - 方法区
  7. coverity java_coverity检测不到代码 | 学步园
  8. CMSInitiatingOccupancyFraction计算释疑
  9. TSINGSEE青犀视频开发AI人脸对比如何输出多个对比相似度的人脸?
  10. Struts2概述及与Struts1的对比