在做培训的时候,遇到一个很奇怪的问题:在给一个table加上constraint 去验证当前时间不允许小于系统时间。
create table mytable(
id number(4),
mydate date
);
alter table mytable add constraint chk_date check (mydate >=sysdate);
出现:
ORA-02436 date or system variable wrongly specified in check constraint。
经过google和请教别人得到此问题的2个解决方法:
1. 一般方法:使用Trigger去做验证。
2. 使用Oracle Visual column去验证
方法1代码如下:
create trigger mytrigger
before insert or update on mytable
for each row
begin
if inserting or updating(mydate) then
if :new.mydate >=sysdate then
raise_application_error(-20000,'mydate must be a date in the past.');
end if;
end if;
end;
这样在做Insert和Update的时候就可以验证当前Insert的时间是否被允许了
方法2步骤如下(Oracle版本必须是11g或者以上):
Step1: 添加一个Function去验证
create or replace function fun_check_date(tmpDate date)
is
begin
RETURN CASE WHEN empdate >= SYSDATE THEN 'N' ELSE 'Y' END;
end fun_check_date;
Step2: 添加visual column
ALTER TABLE mytable ADD (mydate_in_past_ind AS (CAST (fun_check_date(mydate) as varchar2(1))));
Step3: 添加constraint
ALTER TABLE mytable ADD CONSTRAINT myck1 CHECK (mydate_in_past_ind = 'Y')
这样就可以解决在约束条件中的对sysdate的比较。
分享到:
相关推荐
ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-01033:oracle initialization or shutdown in progress 解决方法 ora-...
使用工具IMPDP导入数据时ORA-39002、ORA-39070错误排查。使用工具IMPDP导入数据时ORA-39002、ORA-39070错误排查 使用工具IMPDP导入数据时ORA-39002、ORA-39070错误排查
Oracle 11gr2连Oracle 19c 报ORA-28040 ORA-01017解决方法
oracle网络配置(listener_ora-sqlnet_ora-tnsnames_ora).mht
ora-00604 错误 解决 方法 ora-00604 错误 解决 方法 ora-00604 错误 解决 方法 ora-00604 错误 解决 方法 ora-00604 错误 解决 方法ora-00604 错误 解决 方法
oracle数据库ora-01152和ora-01110的解决办法
Drop goldengate用户时,报ORA-00604 ORA-20782 ORA-06512错误
数据库导致连接出现异常的解决办法整理,这是 oracle连接问题的具体解决方案
Oracle Database on Linux Fails to Start - ORA-27154 ORA-27300 ORA-27301 ORA-27302: failure occurred at: sskgpbitsper (Doc ID 2789636.1)
ORACLE ORA-00132 ORA-00214
调整参数后引起,ORA-00838: Specified value of MEMORY_TARGET is too small, needs to be at least 3 2768M 的解决方案
oracle启动失败,ORA-00702报错,windows,linux系统下解决办法
客户端进行连接的时候,系统不定期出现ora-12520,ora-12516的连接问题, 问题解决方案建议: 1、增加process和session的连接数。 2、检查连接的应用,是不是有没有释放的连接。 3、将修改参数local_listener中的vip为...
离线误删空间文件导致的ORA-01033及ORA-01145问题的解决办法,在解决ORA-01033的过程中,又出现ORA-01145 * 第 1 行出现错误: ORA-01145: 除非启用了介质恢复, 否则不允许立即脱机 接着的解决步骤
ORA-12541 TNSno listener 的解决方案 ORA-12541 TNSno listener 的解决方案
在运行查询SELECT * FROM V$SESSION 会出现ORA-29275:部分多字节字符的错误,这是什么原因开始我不得其解,网上也没有介绍什么好办法。本文给出答案。
oracle数据库优化之后,报错报错“ora-00838”的处理方法
OGG之ORA-01403案例,包括这个案例所遇到的错误详解,对使用的参数进行了详细的解说
ORA-32001:write to spfile requested but no spfile is in use请求写入spfile,但没有使用spfile的解决方法 在输入以下语句中报了这样的错误: SQL>alter system set control_files=’/u01/app/oracle/oradata/prod/...