PostgreSQL Default Value

Source: Internet
Author: User
Tags postgresql table definition

A field can be assigned a default value. If a new row of data is created, and the values of some fields are not declared, the fields are populated with their respective default values. A data modification command can also explicitly require that a field be set to its default value without knowing beforehand what the default value is.

If the default value is not explicitly declared, the default value is NULL. It is usually reasonable to do so because NULL means "unknown".

In a table definition, the default value is listed after the field data type. Like what:

CREATE TABLE Products (    product_no integer,    name text,    DEFAULT 9.99);

The default value can be an expression that is evaluated when the default value is inserted ( not when the table is created). A common example is that a timestamp field may have a default value of Current_timestamp, which represents the moment when a row is inserted. Another common example is generating a "serial number" for each line. In PostgreSQL, it is usually generated using a method similar to the following:

CREATE TABLE Products (    DEFAULT nextval (' products_product_no_seq '),    ...);

Here the Nextval () provides a successor value from a sequence object . This practice is so common that we have a special abbreviation for this purpose:

CREATE TABLE Products (    SERIAL,    ...);

PostgreSQL Default Value

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.