是否可以使用CHECK约束来防止任何日期落在星期日?我不想使用触发器.
是的,检查约束可以检查星期几不是星期日.这是一个例子:
create table date_test (entry_date date); alter table date_test add constraint day_is_not_sunday check ( to_char(entry_date,'DAY','NLS_DATE_LANGUAGE = ENGLISH') not like 'SUNDAY%');
- SUNDAY右边有空格,所以需要rtrim来匹配字符串.
insert into date_test values(to_date('2008-10-12','YYYY-MM-DD')); --Sunday insert into date_test values(to_date('2008-10-11','YYYY-MM-DD')); insert into date_test values(to_date('2008-10-10','YYYY-MM-DD')); insert into date_test values(to_date('2008-10-09','YYYY-MM-DD')); insert into date_test values(to_date('2008-10-08','YYYY-MM-DD')); insert into date_test values(to_date('2008-10-07','YYYY-MM-DD')); insert into date_test values(to_date('2008-10-06','YYYY-MM-DD'));
当您尝试插入星期日的日期时,它会说:
ORA-02290: check constraint (SYS.DAY_IS_NOT_SUNDAY) violated