使用timestamp sql

时间:2016-12-09 12:39:28

标签: sql postgresql

尝试为我的表添加一个带有时间戳的行,其中包含

time timestamp without time zone NOT NULL

我使用的功能now(),但我一直在

ERROR: invalid input syntax for integer: "2016-12-09 14:32:14.111332"

我试过now()::timestamp对任何想法都没有帮助吗?

CREATE TABLE game (
   score integer,
   score2 integer,
  time timestamp without time zone NOT NULL,
  CHECK ((score is NULL) and (score is NULL)) 
);

insert into game values(2,3,now()); 谢谢!

2 个答案:

答案 0 :(得分:1)

我认为您的问题存在于CHECK ...

我通过一些调整测试了你的查询,但它确实有效。

工作脚本:

CREATE TABLE game (
  score integer,
  score2 integer,
  time2 timestamp without time zone NOT NULL,
  CHECK ((score is NOT NULL) and (score2 is NOT NULL)) 
);

Check working example here

P.s正如用户@Raul所提到的,小心变量名称不要使用保留字,时间可能会让你陷入困境

答案 1 :(得分:0)

My best guess is that 2016-12-09 14:32:14.111332 is not recognized as an ISO standard.

ISO 8601 uses the 24-hour clock system. The basic format is [hh][mm][ss] and the extended format is [hh]:[mm]:[ss]. Accepted would be: hh:mm:ss.sss, hh:mm:ss or hh:mm.

What bothers me is all those numbers at the end: 2016-12-09 14:32:14.111332 14.111332, I don't recall seeing timestamps like this, I might be wrong.

Maybe try inserting 2016-12-09 14:32:14 and see if it throws the same error. Also the name of the column should be changed, column with names the same as some keywords can cause statements to fail.