假如现在我有个createtime的列,我想让时间在1990-1-1至2099-1-1的数据可以导入。在这个时间范围外的数据无法导入,可以实现吗(可能是个很菜鸟的问题)

解决方案 »

  1.   

    考虑使用 check 吗 ?
      

  2.   


    SQL> 
    SQL> create table test(id int, createtime date);
    Table created
    SQL> alter table test add constraint ck_cdate
      2  check(createtime between date'1990-01-01' and date'2015-12-31');
    Table altered
    SQL> -- 成功
    SQL> insert into test values(1, date'1995-02-05');
    1 row inserted
    SQL> insert into test values(2, date'2014-02-05');
    1 row inserted
    SQL> -- 下面两条失败
    SQL> insert into test values(3, date'2025-02-05');
    insert into test values(3, date'2025-02-05')
    ORA-02290: 违反检查约束条件 (ORACLE.CK_CDATE)
    SQL> insert into test values(4, date'1900-02-05');
    insert into test values(4, date'1900-02-05')
    ORA-02290: 违反检查约束条件 (ORACLE.CK_CDATE)
    SQL> select * from test;
                                         ID CREATETIME
    --------------------------------------- -----------
                                          1 1995/2/5
                                          2 2014/2/5
    SQL> drop table test purge;
    Table droppedSQL> 
      

  3.   

    alter table test add constraint ck_cdate
    check(createtime between date'1990-01-01' and date'2015-12-31');