Home Explore Blog CI



postgresql

3rd chunk of `doc/src/sgml/ddl.sgml`
e9201ebbec0d68014b3b566e4b5b89ed7fe76b7e46bae25f0000000100000fa4
  linkend="ddl-alter"/> later in this chapter.
  </para>

  <para>
   With the tools discussed so far you can create fully functional
   tables.  The remainder of this chapter is concerned with adding
   features to the table definition to ensure data integrity,
   security, or convenience.  If you are eager to fill your tables with
   data now you can skip ahead to <xref linkend="dml"/> and read the
   rest of this chapter later.
  </para>
 </sect1>

 <sect1 id="ddl-default">
  <title>Default Values</title>

  <indexterm zone="ddl-default">
   <primary>default value</primary>
  </indexterm>

  <para>
   A column can be assigned a default value.  When a new row is
   created and no values are specified for some of the columns, those
   columns will be filled with their respective default values.  A
   data manipulation command can also request explicitly that a column
   be set to its default value, without having to know what that value is.
   (Details about data manipulation commands are in <xref linkend="dml"/>.)
  </para>

  <para>
   <indexterm><primary>null value</primary><secondary>default value</secondary></indexterm>
   If no default value is declared explicitly, the default value is the
   null value.  This usually makes sense because a null value can
   be considered to represent unknown data.
  </para>

  <para>
   In a table definition, default values are listed after the column
   data type.  For example:
<programlisting>
CREATE TABLE products (
    product_no integer,
    name text,
    price numeric <emphasis>DEFAULT 9.99</emphasis>
);
</programlisting>
  </para>

  <para>
   The default value can be an expression, which will be
   evaluated whenever the default value is inserted
   (<emphasis>not</emphasis> when the table is created).  A common example
   is for a <type>timestamp</type> column to have a default of <literal>CURRENT_TIMESTAMP</literal>,
   so that it gets set to the time of row insertion.  Another common
   example is generating a <quote>serial number</quote> for each row.
   In <productname>PostgreSQL</productname> this is typically done by
   something like:
<programlisting>
CREATE TABLE products (
    product_no integer <emphasis>DEFAULT nextval('products_product_no_seq')</emphasis>,
    ...
);
</programlisting>
   where the <literal>nextval()</literal> function supplies successive values
   from a <firstterm>sequence object</firstterm> (see <xref
   linkend="functions-sequence"/>). This arrangement is sufficiently common
   that there's a special shorthand for it:
<programlisting>
CREATE TABLE products (
    product_no <emphasis>SERIAL</emphasis>,
    ...
);
</programlisting>
   The <literal>SERIAL</literal> shorthand is discussed further in <xref
   linkend="datatype-serial"/>.
  </para>
 </sect1>

 <sect1 id="ddl-identity-columns">
  <title>Identity Columns</title>

  <indexterm zone="ddl-identity-columns">
   <primary>identity column</primary>
  </indexterm>

  <para>
   An identity column is a special column that is generated automatically from
   an implicit sequence.  It can be used to generate key values.
  </para>

  <para>
   To create an identity column, use the <literal>GENERATED ...
   AS IDENTITY</literal> clause in <command>CREATE TABLE</command>, for example:
<programlisting>
CREATE TABLE people (
    id bigint <emphasis>GENERATED ALWAYS AS IDENTITY</emphasis>,
    ...,
);
</programlisting>
   or alternatively
<programlisting>
CREATE TABLE people (
    id bigint <emphasis>GENERATED BY DEFAULT AS IDENTITY</emphasis>,
    ...,
);
</programlisting>
   See <xref linkend="sql-createtable"/> for more details.
  </para>

  <para>
   If an <command>INSERT</command> command is executed on the table with the
   identity column and no value is explicitly specified for the identity
   column, then a value generated by the implicit sequence is inserted.  For
   example, with the above definitions and assuming additional appropriate
   columns, writing
<programlisting>
INSERT INTO people (name,

Title: Default Values and Identity Columns
Summary
This section discusses default values for table columns, including how to define them, how they are used when creating new rows, and the significance of the null value as a default. It also covers expressions as default values, with examples such as using CURRENT_TIMESTAMP for timestamps and generating serial numbers using sequence objects. The section further introduces identity columns, which are automatically generated from implicit sequences for generating key values, including examples of creating them using GENERATED ALWAYS AS IDENTITY and GENERATED BY DEFAULT AS IDENTITY.