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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux常用命令

    linux常用命令linux常用命令

    2022年4月25日
    43
  • tabnine激活码(破解版激活)

    tabnine激活码(破解版激活),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    52
  • Liunx常用命令

    Liunx常用命令Liunx常用命令

    2022年4月22日
    32
  • allure 命令行参数说明

    allure 命令行参数说明一、查看allure命令的帮助文档allure-hallure命令的语法格式allure[options][command][commandoptions]options列表Options:–help命令行帮助文档-q,–quiet切换至安静模式Default:false-v,–verbose切换至冗长模式Default:false–ve…

    2022年7月26日
    23
  • 代码也浪漫——Python烟花秀[通俗易懂]

    代码也浪漫——Python烟花秀[通俗易懂]下午太困,无意中看到一段用Tkinter库写的放烟花的程序,就跟着跑了一遍。设计理念:通过让画面上一个粒子分裂为X数量的粒子来模拟爆炸效果。粒子会发生“膨胀”,意思是它们会以恒速移动且相互之间的角度相等。这样就能让我们以一个向外膨胀的圆圈形式模拟出烟花绽放的画面。经过一定时间后,粒子会进入“自由落体”阶段,也就是由于重力因素它们开始坠落到地面,仿若绽放后熄灭的烟花。 首先我们写一个粒子…

    2022年9月14日
    2
  • java中将Object类型转换成String类型[通俗易懂]

    java中将Object类型转换成String类型[通俗易懂]1.String.valueOf(Object)从下图(a图)的String类的valueOf(Object)的源码可以看到,当传入的值为null的时候返回的是“null”字符串,而不是null,所以在这里如果想判断这个string的值不为空的时候,应该用字段串相关判断不为空的方法例如用equals方法。示例如下(b图)a图:string类valueOf(Object)的源码…

    2022年5月18日
    137

发表回复

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

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