如何使用日期小于今天日期的属性创建表格
如何使用PostgreSQL中日期小于当前日期的属性创建表格?如何使用日期小于今天日期的属性创建表格
这就是我想要做的事:
CREATE TABLE Player(
dob DATE CHECK(dob < today's date) NOT NULL
/*other attributes and constraints omitted*/
);
这可能吗?
预先感谢您!
务必:
check (dob < current_date)
它需要current_date
不now()
作为date
总是(几乎)比now
下这是一个date
加一个time
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
所有功劳归于@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*/
);
这是错误的。正确的版本是'dob
啊,是的,我猜current_date将是正确的,因为它返回当前日期。但now()函数会返回当前的日期和时间。如果我使用now()而不是current_date,它会有所作为吗? @ClodoaldoNeto – 48502104
检查我的答案 –
你有'check(dob
不,我没有遇到任何问题
check (dob 这正是我正在寻找的。谢谢! @GordonLinoff – 48502104