Specifying default value for a field for table creation in postgresql

▼魔方 西西 提交于 2019-12-25 07:56:02

问题


Following is my table design:

CREATE TABLE "x"."y"(
    "z" timestamp NOT NULL,
    "a" Timestamp NOT NULL DEFAULT z + 18 months,
)
WITH (OIDS=FALSE)
;

How do I specify the default value of 'a'?

Can I specify this at the time of the table creation?


回答1:


As postgresql documentation states

The DEFAULT clause assigns a default data value for the column whose column definition it appears within. The value is any variable-free expression (subqueries and cross-references to other columns in the current table are not allowed). The data type of the default expression must match the data type of the column.

It's better to use e.g. rules (http://www.postgresql.org/docs/9.4/static/rules-update.html)



来源:https://stackoverflow.com/questions/30621482/specifying-default-value-for-a-field-for-table-creation-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!