在Oracle中如何用约束如:
alter table table_name add constraint constraint_name check((cloumn_name - sysdate)>=0)
这样执行结果提示有错误,说约束指定有误。
除了用触发器,谁能用以上类似的约束做的话请帮忙指导!
alter table table_name add constraint constraint_name check((cloumn_name - sysdate)>=0)
这样执行结果提示有错误,说约束指定有误。
除了用触发器,谁能用以上类似的约束做的话请帮忙指导!
解决方案 »
- AIX 6.1 + 10.2.0.5 + ASM是否稳定?
- windows 7 上能否安装运行 oracle 9i ???
- sql语句 如何写 内容下
- 请教一个关于存储过程返回数据集的问题
- 请问在Oracle里面,能否给用户授权让他只能看到指定的表和视图之类的权限?
- 我的存储过程exec执行后 表中无数据...
- 关于Oracle9i特殊列名的问题
- Oracle 11g 数据库已经发布下载
- 如何把A表的数据,每隔一天就导出到一个文本文件里?
- 请问oracle中数据表最多可以有多少个字段?
- PLS-00428: 在此 SELECT 语句中缺少 INTO 子句
- 急! 急! 急!一个比较复杂的统计查询!
Unfortunately you can't use SYSDATE in a check constraint as it is a dynamic Oracle variable. A better way to enforce this constraint would be to use a trigger. For example:CREATE OR REPLACE TRIGGER trg_emp_dob BEFORE INSERT OR UPDATE ON emp
BEGIN
IF :new.dob > SYSDATE THEN
RAISE_APPLICATION_ERROR (num => -20000, msg => 'DOB Cannot be in the future');
END IF;
END;不过嘿嘿,我可不信邪,万事问大哥啊,于是在asktom上找到了这个解决方案:
Just create an additional column using as default SYSDATE. Then you can use this column to compare.
And if you want to hide this column create a view around it...SQL> create table t
2 (x date,
3 y date default sysdate,
4 constraint x_y check
5 (
6 x >= y
7 )
8 );Table created.SQL>
SQL> create view v as select x from t;View created.SQL>
SQL> insert into v values (sysdate);1 row created.SQL>
SQL> commit;Commit complete.SQL>
SQL> insert into v values (sysdate-1);
insert into v values (sysdate-1)
*
ERROR at line 1:
ORA-02290: check constraint (ADBM.X_Y) violated总之oracle不能非常漂亮的解决这个问题,这里两个方案你看着选吧。ps:你比较日期的时候为啥不直接用cloumn_name <sysdate。ps2:为了5分我找遍全世界啊,还不快快拿来?
行不行?