Oracle 约束日期检查 SQL

Oracle Constraint Date Check SQL

我想在 table 上添加约束以防止 insertion/update 记录基于某些条件。

更具体地说,如果插入或更新记录,如果活动为 'Y'

,则两个日期字段的年份必须相同
create table MY_TABLE
(
  id                      NUMBER not null,
  active                  CHAR(1) ,
  date_one                DATE,
  date_two                DATE
)


/* Allowed : both dates are 2014 AND flag id 'Y'*/
insert into MY_TABLE(id, active, date_one, date_two) 
VALUES (1, 'Y', to_date('20141201', 'yyyymmdd') , to_date('20140101', 'yyyymmdd');

/* DISAllowed : dates have different year AND flag id 'Y'*/
insert into MY_TABLE(id, active, date_one, date_two) 
VALUES (2, 'Y', to_date('20151201', 'yyyymmdd') , to_date('20140101', 'yyyymmdd');

/* Allowed : dates have different year AND flag id is NOT 'Y'*/
insert into MY_TABLE(id, active, date_one, date_two) 
VALUES (3, 'N', to_date('20151201', 'yyyymmdd') , to_date('20140101', 'yyyymmdd');

非常感谢

添加约束是设计一些逻辑来准确定义您要实现的业务逻辑的问题。在您的情况下,您希望 ACTIVEN 或日期中的年份必须相同。

您可以使用EXTRACT()函数来查看年份;即

SQL> select extract(year from sysdate) from dual;

EXTRACT(YEARFROMSYSDATE)
------------------------
                    2015

这使您的条件 active = 'N' or extract(year from date_one) = extract(year from date_two),然后您可以在 table DDL 中声明:

create table my_table (
    id number not null
  , active char(1)
  , date_one date
  , date_two date
  , constraint chk_dates check ( 
      active = 'N' or extract(year from date_one) = extract(year from date_two) )
    );

这给了你想要的结果:

SQL> insert into MY_TABLE(id, active, date_one, date_two)
  2  VALUES (1, 'Y', to_date('20141201', 'yyyymmdd') , to_date('20140101', 'yyyymmdd'));

1 row created.

SQL> insert into MY_TABLE(id, active, date_one, date_two)
  2  VALUES (2, 'Y', to_date('20151201', 'yyyymmdd') , to_date('20140101', 'yyyymmdd'));
insert into MY_TABLE(id, active, date_one, date_two)
*
ERROR at line 1:
ORA-02290: check constraint (REF.CHK_DATES) violated


SQL> insert into MY_TABLE(id, active, date_one, date_two)
  2  VALUES (3, 'N', to_date('20151201', 'yyyymmdd') , to_date('20140101', 'yyyymmdd'));

1 row created.

我还会在 ACTIVE 列上添加单独的约束,以确保它只能具有正确的值和您的主键

create table my_table (
    id number not null
  , active char(1)
  , date_one date
  , date_two date
  , constraint pk_my_table primary key (id)
  , constraint chk_my_table_active check ( active in ('Y', 'N') )
  , constraint chk_dates check ( 
      active = 'N' or extract(year from date_one) = extract(year from date_two) )
    );

如果您的 ACTIVE 列可以有比 Y 或 N 更多的值,那么您需要稍微更改约束以考虑到这一点;例如像下面这样的东西:

coalesce(active, 'X') <> 'Y' or extract(year from date_one) = extract(year from date_two)