检查日期约束 [英] Check constraint on date

查看:218
本文介绍了检查日期约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用date列创建一个表.我想添加一个检查约束以验证日期是否在将来.

I am creating a table with date column. I would like to add a check constraint to validate if the date is in future.

create table test_table (
  schedule_date date not null,
  check (schedule_date >= TODAY)
);

上面的sql给我一个语法错误.

Above sql gives me a syntax error.

09:43:37  [CREATE - 0 row(s), 0.000 secs]  [Error Code: -201, SQL State: 42000]  A syntax error has occurred. 
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 1 errors]

如何在日期列上添加约束?

How do I add a constraint on date column?

推荐答案

您今天不能在检查约束中使用. 如官方文档所述:

You cannot use today in the check constraint. As the official documentation states:

检查约束是使用搜索条件定义的.搜索条件不能包含用户定义的例程,子查询,聚合,主机变量或rowid.此外,条件不能包含内置的变体函数CURRENT,USER,SITENAME,DBSERVERNAME或TODAY.

Check constraints are defined using search conditions. The search condition cannot contain user-defined routines, subqueries, aggregates, host variables, or rowids. In addition, the condition cannot contain the variant built-in functions CURRENT, USER, SITENAME, DBSERVERNAME, or TODAY.

我认为解决方案可以使用插入/更新触发器.

I think a solution can be using an insert/update trigger.

这篇关于检查日期约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆