如何创建一个包含日期小于今天日期

时间:2017-04-03 22:53:23

标签: sql database postgresql constraints relational-database

如何在PostgreSQL中创建一个日期少于今天日期属性的表?

这就是我想要做的事情:

CREATE TABLE Player(
dob DATE CHECK(dob < today's date) NOT NULL
/*other attributes and constraints omitted*/
);

这可能吗?

提前谢谢!

2 个答案:

答案 0 :(得分:0)

所有归功于@Gordon Linoff的回答。

他建议我使用PostgreSQL提供的now()函数。

now()函数返回当前日期和时间。

有关详细信息,请参阅https://www.postgresql.org/docs/8.0/static/functions-datetime.html

我的结果表如下所示:

CREATE TABLE Player(
dob DATE CHECK(dob < now()) NOT NULL
/*other attributes and constraints omitted*/
);

答案 1 :(得分:0)

执行:

check (dob < current_date)

它必须是current_date而不是now(),因为date将始终(几乎)低于nowdate加上time 1}}

select
    current_date,
    current_date::timestamp,
    now(),
    current_date < now(),
    current_date < current_date;
    date    |      timestamp      |              now              | ?column? | ?column? 
------------+---------------------+-------------------------------+----------+----------
 2017-04-04 | 2017-04-04 00:00:00 | 2017-04-04 11:54:39.703732+00 | t        | f