oracle普通索引改唯一索引,Oracle唯一索引功能替代[通俗易懂]

oracle普通索引改唯一索引,Oracle唯一索引功能替代[通俗易懂]Oracle唯一索引在字段全部为NULL时,不做唯一性判断,允许重复插入,而在8t中即使均为NULL值也会做重复值判断,在某些场景下客户会存在此类需求,在数据量不大不存在性能问题的情况下可以考虑通过如下方式进行替代示例表createtable”informix”.secconstitute(iddecimal(20,0)notnull,codevarchar(32),namevar…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/192910.html原文链接:https://javaforall.net

(0)
上一篇 2025年12月5日 下午8:43
下一篇 2025年12月5日 下午9:22


相关推荐

  • Tomcat日志乱码问题

    Tomcat日志乱码问题昨天本来准备更新一下Tomcat版本,但是发现新版本的日志打印中文会出现乱码(Tomcat自身打印的日志),不管是使用bat脚本启动还是在Idea中启动,都是乱码。研究了一个晚上,百度上的那些方式都试遍了,都是设置各种JVM启动参数,发现并没有卵用。在使用bat文件启动Tomcat时,Tomcat目录下的logs文件夹会生成相应的日志文件,发现旧版本生成的日志文件编码是GBK,而Windows控…

    2022年6月20日
    42
  • 运放电流检测采样电路电压采样电路

    运放电流检测采样电路电压采样电路输入输出电压检测输入输出电压通过运放LMC6482采用差分电路将输出电压按比例缩小至ADC能够采样的范围,再使用ADC采样,软件解算出输出电压。输入电压采样是通过MCU内部运放按比例缩小在送到ADC进行采样的,具体电路如图3.5.1所示。输出电压检测电路如图3.4.1所示。输出电流检测➢输出电流检测电路通过运放LMC6482采样差分放大电路实现;采样电阻放在低端,若采样电阻放在高端,会有较大的共模电压使采样电流不准确,采样电阻为10m????,由于采样电阻较小,采样电阻上的压降较小,不利于直

    2022年6月2日
    240
  • Ant 下载、安装、使用、教程全面了解「建议收藏」

    Ant 下载、安装、使用、教程全面了解「建议收藏」Eclipse内置了Ant。Ant是一种类似于批处理程序的软件包,它主要繁琐的工作是编写和调试自动处理脚本(一个XML文件),但只要有了这个脚本,我们就可以一键完成所有的设定工作。本节还是以myswt这个应用程序项目的打包为例,用Ant来完成“编译->打成JAR包->复制项目引用库->复制本地化文件swt-win32-3063.dll->输出API文档”这五步

    2022年7月13日
    16
  • wing是什么_nativepage原理

    wing是什么_nativepage原理给定 n 本书,编号为 1∼n。在初始状态下,书是任意排列的。在每一次操作中,可以抽取其中连续的一段,再把这段插入到其他某个位置。我们的目标状态是把书按照 1∼n 的顺序依次排列。求最少需要多少次操作。输入格式第一行包含整数 T,表示共有 T 组测试数据。每组数据包含两行,第一行为整数 n,表示书的数量。第二行为 n 个整数,表示 1∼n 的一种任意排列。同行数之间用空格隔开。输出格式每组数据输出一个最少操作次数。如果最少操作次数大于或等于 5 次,则输出 5 or more。每个

    2022年8月8日
    9
  • Jquery.DataTable使用

    Jquery.DataTable使用Jquery DataTable 插件使用 DataTable 插件是一款基于 jquery 的表格插件 其官网地址为 http datatables net 下面简单描述该插件的使用方法安装 DataTable 从官网下载最新版本的插件 再在页面中应用相应的 js 和 css 文件 DataTable 使用 DataTable 支持多种使用方法分别是基于静态 html 表格一个例子 tableid table id ex

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号