File: ddl-default.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (65 lines) | stat: -rw-r--r-- 3,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>5.2.Default Values</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="ddl.html" title="Chapter5.Data Definition">
<link rel="prev" href="ddl.html" title="Chapter5.Data Definition">
<link rel="next" href="ddl-constraints.html" title="5.3.Constraints">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="ddl-default"></a>5.2.Default Values</h2></div></div></div>
<a name="id574642"></a><p>   A column can be assigned a default value.  When a new row is
   created and no values are specified for some of the columns, the
   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 <a href="dml.html" title="Chapter6.Data Manipulation">Chapter6, <i>Data Manipulation</i></a>.)
  </p>
<p>   <a name="id574671"></a>
   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.
  </p>
<p>   In a table definition, default values are listed after the column
   data type.  For example:
</p>
<pre class="programlisting">CREATE TABLE products (
    product_no integer,
    name text,
    price numeric <span><strong class="emphasis">DEFAULT 9.99</strong></span>
);</pre>
<p>
  </p>
<p>   The default value may be an expression, which will be
   evaluated whenever the default value is inserted
   (<span class="emphasis"><em>not</em></span> when the table is created).  A common example
   is that a <code class="type">timestamp</code> column may have a default of <code class="literal">now()</code>,
   so that it gets set to the time of row insertion.  Another common
   example is generating a &#8220;<span class="quote">serial number</span>&#8221; for each row.
   In <span class="productname">PostgreSQL</span> this is typically done by
   something like
</p>
<pre class="programlisting">CREATE TABLE products (
    product_no integer <span><strong class="emphasis">DEFAULT nextval('products_product_no_seq')</strong></span>,
    ...
);</pre>
<p>
   where the <code class="literal">nextval()</code> function supplies successive values
   from a <em class="firstterm">sequence object</em> (see <a href="functions-sequence.html" title="9.12.Sequence Manipulation Functions">Section9.12, &#8220;Sequence Manipulation Functions&#8221;</a>). This arrangement is sufficiently common
   that there's a special shorthand for it:
</p>
<pre class="programlisting">CREATE TABLE products (
    product_no <span><strong class="emphasis">SERIAL</strong></span>,
    ...
);</pre>
<p>
   The <code class="literal">SERIAL</code> shorthand is discussed further in <a href="datatype.html#datatype-serial" title="8.1.4.Serial Types">Section8.1.4, &#8220;Serial Types&#8221;</a>.
  </p>
</div></body>
</html>