oracle写时间判断条件

qq_36668382 2016-12-28 10:05:14
假如现在我有个createtime的列,我想让时间在1990-1-1至2099-1-1的数据可以导入。在这个时间范围外的数据无法导入,可以实现吗(可能是个很菜鸟的问题
...全文
670 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
有意1314 2017-04-27
  • 打赏
  • 举报
回复
alter table test add constraint ck_cdate check(createtime between date'1990-01-01' and date'2015-12-31');
卖水果的net 2016-12-28
  • 打赏
  • 举报
回复

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 dropped

SQL> 
卖水果的net 2016-12-28
  • 打赏
  • 举报
回复
考虑使用 check 吗 ?

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧