暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 阻止表中的check_date列的将来日期

askTom 2016-02-02
272

问题描述

我有一个名为DAY_INSP的表和一个允许将来日期的列INS_DATE。我想通过只允许今天的日期或更早的日期来防止这种情况。是否需要添加触发器或约束来执行此操作?如有任何帮助,我们将不胜感激

专家解答

不能在检查约束中使用sysdate。您可以使用触发器执行此操作:

SQL> create table t (
  2    dt date
  3  );
SQL>
SQL> alter table t add constraint ck check (dt < sysdate);
alter table t add constraint ck check (dt < sysdate)
                                            *
ERROR at line 1:
ORA-02436: date or system variable wrongly specified in CHECK constraint


SQL>
SQL> create or replace trigger tt
  2  before insert or update on t
  3  for each row
  4  begin
  5    if :new.dt > sysdate then
  6      raise_application_error(-20001, 'Date in the future!');
  7    end if;
  8  end;
  9  /
SQL> show err
No errors.
SQL>
SQL> insert into t values (sysdate+1);
insert into t values (sysdate+1)
            *
ERROR at line 1:
ORA-20001: Date in the future!
ORA-06512: at "CHRIS.TT", line 3
ORA-04088: error during execution of trigger 'CHRIS.TT'

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论