codekingpro/portable-devtools
115k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>5.2. Default Values</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="ddl-basics.html" title="5.1. Table Basics" /><link rel="next" href="ddl-generated-columns.html" title="5.3. Generated Columns" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">5.2. Default Values</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ddl-basics.html" title="5.1. Table Basics">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><th width="60%" align="center">Chapter 5. Data Definition</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ddl-generated-columns.html" title="5.3. Generated Columns">Next</a></td></tr></table><hr /></div><div class="sect1" id="DDL-DEFAULT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5.2. Default Values <a href="#DDL-DEFAULT" class="id_link">#</a></h2></div></div></div><a id="id-1.5.4.4.2" class="indexterm"></a><p>3 A column can be assigned a default value. When a new row is4 created and no values are specified for some of the columns, those5 columns will be filled with their respective default values. A6 data manipulation command can also request explicitly that a column7 be set to its default value, without having to know what that value is.8 (Details about data manipulation commands are in <a class="xref" href="dml.html" title="Chapter 6. Data Manipulation">Chapter 6</a>.)9 </p><p>10 <a id="id-1.5.4.4.4.1" class="indexterm"></a>11 If no default value is declared explicitly, the default value is the12 null value. This usually makes sense because a null value can13 be considered to represent unknown data.14 </p><p>15 In a table definition, default values are listed after the column16 data type. For example:17</p><pre class="programlisting">18CREATE TABLE products (19 product_no integer,20 name text,21 price numeric <span class="emphasis"><strong>DEFAULT 9.99</strong></span>22);23</pre><p>24 </p><p>25 The default value can be an expression, which will be26 evaluated whenever the default value is inserted27 (<span class="emphasis"><em>not</em></span> when the table is created). A common example28 is for a <code class="type">timestamp</code> column to have a default of <code class="literal">CURRENT_TIMESTAMP</code>,29 so that it gets set to the time of row insertion. Another common30 example is generating a <span class="quote">“<span class="quote">serial number</span>”</span> for each row.31 In <span class="productname">PostgreSQL</span> this is typically done by32 something like:33</p><pre class="programlisting">34CREATE TABLE products (35 product_no integer <span class="emphasis"><strong>DEFAULT nextval('products_product_no_seq')</strong></span>,36 ...37);38</pre><p>39 where the <code class="literal">nextval()</code> function supplies successive values40 from a <em class="firstterm">sequence object</em> (see <a class="xref" href="functions-sequence.html" title="9.17. Sequence Manipulation Functions">Section 9.17</a>). This arrangement is sufficiently common41 that there's a special shorthand for it:42</p><pre class="programlisting">43CREATE TABLE products (44 product_no <span class="emphasis"><strong>SERIAL</strong></span>,45 ...46);47</pre><p>48 The <code class="literal">SERIAL</code> shorthand is discussed further in <a class="xref" href="datatype-numeric.html#DATATYPE-SERIAL" title="8.1.4. Serial Types">Section 8.1.4</a>.49 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ddl-basics.html" title="5.1. Table Basics">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ddl.html" title="Chapter 5. Data Definition">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ddl-generated-columns.html" title="5.3. Generated Columns">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.1. Table Basics </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 5.3. Generated Columns</td></tr></table></div></body></html>