在Oracle中如何用约束如:
alter table table_name add constraint constraint_name check((cloumn_name - sysdate)>=0)
这样执行结果提示有错误,说约束指定有误。
除了用触发器,谁能用以上类似的约束做的话请帮忙指导!

解决方案 »

  1.   

    这是网上能够搜索到的比较可信的结果:
    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分我找遍全世界啊,还不快快拿来?
      

  2.   

    alter table table_name add constraint constraint_name check(cloumn_name<sysdate)
    行不行?