检查oracle中出生日期的约束条件 [英] Check Constraint on Date Of birth Column in oracle

查看:493
本文介绍了检查oracle中出生日期的约束条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想创建一个对出生日期(DOB)列有约束的表。



表示(今天 - 出生日期)> = 18年



我的桌子看起来像


Hi i want to create a table that has the constraint on date Of Birth(DOB) Column.

means (today-Date of birth)>= 18 years

my table looks like

create table EmpRegular_dtls
(
Emp_Id NVARCHAR2(20) not null,
EmpName NVARCHAR2(20) not null,
DOB date not null,
CONSTRAINT chk_DOB check((round((sysdate-DOB)/365))>=18)
);





但我收到了一个很大的错误.. ......





but i am getting a big error on this........

引用:

SQL错误:ORA-02436 :CHECK约束中错误指定的日期或系统变量

02436. 00000 - CHECK约束中错误指定的日期或系统变量

*原因:尝试使用在CREATE TABLE或ALTER TABLE语句中指定的检查约束不完全是
的日期常量或系统变量,例如USER,
。对于

示例,指定的日期没有世纪。

*操作:完全指定日期常量或系统变量。

设置事件10149允许约束,例如a1> '10 -MAY-96',

,允许在版本8之前创建一个错误。

SQL Error: ORA-02436: date or system variable wrongly specified in CHECK constraint
02436. 00000 - "date or system variable wrongly specified in CHECK constraint"
*Cause: An attempt was made to use a date constant or system variable,
such as USER, in a check constraint that was not completely
specified in a CREATE TABLE or ALTER TABLE statement. For
example, a date was specified without the century.
*Action: Completely specify the date constant or system variable.
Setting the event 10149 allows constraints like "a1 > '10-MAY-96'",
which a bug permitted to be created before version 8.





所以现在请给我一个确切的解决方案。

谢谢



so now please give me exact solution for this.
thanks

推荐答案

简单的方法是创建一个触发器做检查。请考虑以下内容

On simple way is to create a trigger to do the check. Consider the following
CREATE OR REPLACE TRIGGER trgEmpRegular_dtls
  BEFORE INSERT OR UPDATE ON EmpRegular_dtls
  FOR EACH ROW
BEGIN
  IF( ADD_MONTHS(:new.DOB, 18 * 12) < sysdate ) THEN
    RAISE_APPLICATION_ERROR( -20001, 'Person must be at least 18 years old.' );
  END IF;
END;



如果年龄小于18岁,则会抛出异常并且您可以抓住呼叫方的消息并将其显示给用户。



有关自定义错误的更多信息:处理PL / SQL错误 [ ^ ]


这篇关于检查oracle中出生日期的约束条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆