Sql 在postgress中使用时间戳

Sql 在postgress中使用时间戳,sql,postgresql,Sql,Postgresql,我试图在postgres中构建和填充一个表。最终产品应该是这样的 这些是表应该具有的列和我认为它们应该使用的数据类型(如果您有关于更好地拟合数据类型的建议,我想知道它们) id整数 名称VARCHAR(60) 性别(1) 年龄整数 日期时间戳 采用日期时间戳 我用这个命令创建了表 pets=# CREATE TABLE cats ( id integer,name text,gender text, age integer, intake_date timestamp, adoption_

我试图在postgres中构建和填充一个表。最终产品应该是这样的

这些是表应该具有的列和我认为它们应该使用的数据类型(如果您有关于更好地拟合数据类型的建议,我想知道它们)

  • id整数
  • 名称VARCHAR(60)
  • 性别(1)
  • 年龄整数
  • 日期时间戳
  • 采用日期时间戳
我用这个命令创建了表

pets=# CREATE TABLE cats (
id integer,name text,gender text, age integer, intake_date timestamp, 
adoption_date timestamp);
然后尝试在这个命令中添加我的所有行并得到

pets=# INSERT INTO cats (id,name,gender,age, intake_date,adoption_date)
VALUES
(00001, 'Mushi', 'M', 1,2016-01-09, 2016-03-22)
(00002, 'Seashell' , 'F', 7,2016-01-09)
(00003,'Azul', 'M', 3, 2016-01-11, 2016-04-17)
(00004, 'Victoire' ,'M', 7, 2016-01-11, 2016-09-01)
(00005, 'Nala', 'F', 12016-01-12);
ERROR:  syntax error at or near "("
LINE 4: (00002, 'Seashell' , 'F', 7,2016-01-09)
然后,我部分意识到postgres会有问题,因为一些
adoption\u date
列保留为空白。所以我试着至少添加一行

pets=# INSERT INTO cats (id,name,gender,age, intake_date,adoption_date)
VALUES
(00001,'Mushi','M', 1, 2016-01-09, 2016-03-22);
并得到了一个错误的

ERROR:  column "intake_date" is of type timestamp without time zone but 
expression is of type integer
LINE 3: (00001,'Mushi','M', 1, 2016-01-09, 2016-03-22);
                           ^
HINT:  You will need to rewrite or cast the expression.
所以我的三个问题或未知是

  • 如何创建一个表,该表中的列的数据类型为timestamp,在条目处留空
  • 我应该如何输入时间戳
  • 我是否应该使用更好的数据类型

  • 时间戳数据应在单引号内传递

    INSERT INTO cats (id,name,gender,age, intake_date,adoption_date)
    VALUES(00001, 'Mushi', 'M', 1,'2016-01-09', '2016-03-22');
    
    要插入空白值时,只需使用
    NULL

    INSERT INTO cats (id,name,gender,age, intake_date,adoption_date)
    VALUES(00002, 'Seashell' , 'F', 7,'2016-01-09',null);
    

    创建如下表(为插入时要保留为空的列指定
    默认Null


    在时间戳周围加引号,即
    '2016-01-09'
    ,整数不需要用0填充,即
    1
    而不是
    0001
    。除非列上指定了
    非空
    主键
    约束,否则它将接受空。数据类型很好。ID列可以是一个
    序列
    ,因此它将在您输入行时自动生成,如上所述,不要用前导零填充整数,这只是一种浪费。
    CREATE TABLE cats1 (
    id integer
    ,name text
    ,gender text
    ,age integer
    ,intake_date timestamp
    ,adoption_date timestamp default Null
    );