gooderp18绿色标准版
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

49 lines
4.6KB

  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 V1.79.1" /><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><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 12.4 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></hr></div><div class="sect1" id="DDL-DEFAULT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">5.2. Default Values</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 is
  4. created and no values are specified for some of the columns, those
  5. columns will be filled with their respective default values. A
  6. data manipulation command can also request explicitly that a column
  7. 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 the
  12. null value. This usually makes sense because a null value can
  13. be considered to represent unknown data.
  14. </p><p>
  15. In a table definition, default values are listed after the column
  16. data type. For example:
  17. </p><pre class="programlisting">
  18. CREATE 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 be
  26. evaluated whenever the default value is inserted
  27. (<span class="emphasis"><em>not</em></span> when the table is created). A common example
  28. 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 common
  30. 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 by
  32. something like:
  33. </p><pre class="programlisting">
  34. CREATE 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 values
  40. from a <em class="firstterm">sequence object</em> (see <a class="xref" href="functions-sequence.html" title="9.16. Sequence Manipulation Functions">Section 9.16</a>). This arrangement is sufficiently common
  41. that there's a special shorthand for it:
  42. </p><pre class="programlisting">
  43. CREATE 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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ddl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ddl-generated-columns.html">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">Home</a></td><td width="40%" align="right" valign="top"> 5.3. Generated Columns</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1