]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/datatype-numeric.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / datatype-numeric.html
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>8.1. Numeric Types</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="datatype.html" title="Chapter 8. Data Types" /><link rel="next" href="datatype-money.html" title="8.2. Monetary Types" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.1. Numeric Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="datatype.html" title="Chapter 8. Data Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="datatype-money.html" title="8.2. Monetary Types">Next</a></td></tr></table><hr /></div><div class="sect1" id="DATATYPE-NUMERIC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.1. Numeric Types <a href="#DATATYPE-NUMERIC" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-INT">8.1.1. Integer Types</a></span></dt><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL">8.1.2. Arbitrary Precision Numbers</a></span></dt><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-FLOAT">8.1.3. Floating-Point Types</a></span></dt><dt><span class="sect2"><a href="datatype-numeric.html#DATATYPE-SERIAL">8.1.4. Serial Types</a></span></dt></dl></div><a id="id-1.5.7.9.2" class="indexterm"></a><p>
3     Numeric types consist of two-, four-, and eight-byte integers,
4     four- and eight-byte floating-point numbers, and selectable-precision
5     decimals.  <a class="xref" href="datatype-numeric.html#DATATYPE-NUMERIC-TABLE" title="Table 8.2. Numeric Types">Table 8.2</a> lists the
6     available types.
7    </p><div class="table" id="DATATYPE-NUMERIC-TABLE"><p class="title"><strong>Table 8.2. Numeric Types</strong></p><div class="table-contents"><table class="table" summary="Numeric Types" border="1"><colgroup><col class="col1" /><col class="col2" /><col class="col3" /><col class="col4" /></colgroup><thead><tr><th>Name</th><th>Storage Size</th><th>Description</th><th>Range</th></tr></thead><tbody><tr><td><code class="type">smallint</code></td><td>2 bytes</td><td>small-range integer</td><td>-32768 to +32767</td></tr><tr><td><code class="type">integer</code></td><td>4 bytes</td><td>typical choice for integer</td><td>-2147483648 to +2147483647</td></tr><tr><td><code class="type">bigint</code></td><td>8 bytes</td><td>large-range integer</td><td>-9223372036854775808 to +9223372036854775807</td></tr><tr><td><code class="type">decimal</code></td><td>variable</td><td>user-specified precision, exact</td><td>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</td></tr><tr><td><code class="type">numeric</code></td><td>variable</td><td>user-specified precision, exact</td><td>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</td></tr><tr><td><code class="type">real</code></td><td>4 bytes</td><td>variable-precision, inexact</td><td>6 decimal digits precision</td></tr><tr><td><code class="type">double precision</code></td><td>8 bytes</td><td>variable-precision, inexact</td><td>15 decimal digits precision</td></tr><tr><td><code class="type">smallserial</code></td><td>2 bytes</td><td>small autoincrementing integer</td><td>1 to 32767</td></tr><tr><td><code class="type">serial</code></td><td>4 bytes</td><td>autoincrementing integer</td><td>1 to 2147483647</td></tr><tr><td><code class="type">bigserial</code></td><td>8 bytes</td><td>large autoincrementing integer</td><td>1 to 9223372036854775807</td></tr></tbody></table></div></div><br class="table-break" /><p>
8     The syntax of constants for the numeric types is described in
9     <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS" title="4.1.2. Constants">Section 4.1.2</a>.  The numeric types have a
10     full set of corresponding arithmetic operators and
11     functions. Refer to <a class="xref" href="functions.html" title="Chapter 9. Functions and Operators">Chapter 9</a> for more
12     information.  The following sections describe the types in detail.
13    </p><div class="sect2" id="DATATYPE-INT"><div class="titlepage"><div><div><h3 class="title">8.1.1. Integer Types <a href="#DATATYPE-INT" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.9.6.2" class="indexterm"></a><a id="id-1.5.7.9.6.3" class="indexterm"></a><a id="id-1.5.7.9.6.4" class="indexterm"></a><a id="id-1.5.7.9.6.5" class="indexterm"></a><a id="id-1.5.7.9.6.6" class="indexterm"></a><a id="id-1.5.7.9.6.7" class="indexterm"></a><p>
14      The types <code class="type">smallint</code>, <code class="type">integer</code>, and
15      <code class="type">bigint</code> store whole numbers, that is, numbers without
16      fractional components, of various ranges.  Attempts to store
17      values outside of the allowed range will result in an error.
18     </p><p>
19      The type <code class="type">integer</code> is the common choice, as it offers
20      the best balance between range, storage size, and performance.
21      The <code class="type">smallint</code> type is generally only used if disk
22      space is at a premium.  The <code class="type">bigint</code> type is designed to be
23      used when the range of the <code class="type">integer</code> type is insufficient.
24     </p><p>
25      <acronym class="acronym">SQL</acronym> only specifies the integer types
26      <code class="type">integer</code> (or <code class="type">int</code>),
27      <code class="type">smallint</code>, and <code class="type">bigint</code>.  The
28      type names <code class="type">int2</code>, <code class="type">int4</code>, and
29      <code class="type">int8</code> are extensions, which are also used by some
30      other <acronym class="acronym">SQL</acronym> database systems.
31     </p></div><div class="sect2" id="DATATYPE-NUMERIC-DECIMAL"><div class="titlepage"><div><div><h3 class="title">8.1.2. Arbitrary Precision Numbers <a href="#DATATYPE-NUMERIC-DECIMAL" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.9.7.2" class="indexterm"></a><a id="id-1.5.7.9.7.3" class="indexterm"></a><a id="id-1.5.7.9.7.4" class="indexterm"></a><p>
32      The type <code class="type">numeric</code> can store numbers with a
33      very large number of digits. It is especially recommended for
34      storing monetary amounts and other quantities where exactness is
35      required.  Calculations with <code class="type">numeric</code> values yield exact
36      results where possible, e.g.,  addition, subtraction, multiplication.
37      However, calculations on <code class="type">numeric</code> values are very slow
38      compared to the integer types, or to the floating-point types
39      described in the next section.
40     </p><p>
41      We use the following terms below:  The
42      <em class="firstterm">precision</em> of a <code class="type">numeric</code>
43      is the total count of significant digits in the whole number,
44      that is, the number of digits to both sides of the decimal point.
45      The <em class="firstterm">scale</em> of a <code class="type">numeric</code> is the
46      count of decimal digits in the fractional part, to the right of the
47      decimal point.  So the number 23.5141 has a precision of 6 and a
48      scale of 4.  Integers can be considered to have a scale of zero.
49     </p><p>
50      Both the maximum precision and the maximum scale of a
51      <code class="type">numeric</code> column can be
52      configured.  To declare a column of type <code class="type">numeric</code> use
53      the syntax:
54 </p><pre class="programlisting">
55 NUMERIC(<em class="replaceable"><code>precision</code></em>, <em class="replaceable"><code>scale</code></em>)
56 </pre><p>
57      The precision must be positive, while the scale may be positive or
58      negative (see below).  Alternatively:
59 </p><pre class="programlisting">
60 NUMERIC(<em class="replaceable"><code>precision</code></em>)
61 </pre><p>
62      selects a scale of 0.  Specifying:
63 </p><pre class="programlisting">
64 NUMERIC
65 </pre><p>
66      without any precision or scale creates an <span class="quote">“<span class="quote">unconstrained
67      numeric</span>”</span> column in which numeric values of any length can be
68      stored, up to the implementation limits.  A column of this kind will
69      not coerce input values to any particular scale, whereas
70      <code class="type">numeric</code> columns with a declared scale will coerce
71      input values to that scale.  (The <acronym class="acronym">SQL</acronym> standard
72      requires a default scale of 0, i.e., coercion to integer
73      precision.  We find this a bit useless.  If you're concerned
74      about portability, always specify the precision and scale
75      explicitly.)
76     </p><div class="note"><h3 class="title">Note</h3><p>
77       The maximum precision that can be explicitly specified in
78       a <code class="type">numeric</code> type declaration is 1000.  An
79       unconstrained <code class="type">numeric</code> column is subject to the limits
80       described in <a class="xref" href="datatype-numeric.html#DATATYPE-NUMERIC-TABLE" title="Table 8.2. Numeric Types">Table 8.2</a>.
81      </p></div><p>
82      If the scale of a value to be stored is greater than the declared
83      scale of the column, the system will round the value to the specified
84      number of fractional digits.  Then, if the number of digits to the
85      left of the decimal point exceeds the declared precision minus the
86      declared scale, an error is raised.
87      For example, a column declared as
88 </p><pre class="programlisting">
89 NUMERIC(3, 1)
90 </pre><p>
91      will round values to 1 decimal place and can store values between
92      -99.9 and 99.9, inclusive.
93     </p><p>
94      Beginning in <span class="productname">PostgreSQL</span> 15, it is allowed
95      to declare a <code class="type">numeric</code> column with a negative scale.  Then
96      values will be rounded to the left of the decimal point.  The
97      precision still represents the maximum number of non-rounded digits.
98      Thus, a column declared as
99 </p><pre class="programlisting">
100 NUMERIC(2, -3)
101 </pre><p>
102      will round values to the nearest thousand and can store values
103      between -99000 and 99000, inclusive.
104      It is also allowed to declare a scale larger than the declared
105      precision.  Such a column can only hold fractional values, and it
106      requires the number of zero digits just to the right of the decimal
107      point to be at least the declared scale minus the declared precision.
108      For example, a column declared as
109 </p><pre class="programlisting">
110 NUMERIC(3, 5)
111 </pre><p>
112      will round values to 5 decimal places and can store values between
113      -0.00999 and 0.00999, inclusive.
114     </p><div class="note"><h3 class="title">Note</h3><p>
115       <span class="productname">PostgreSQL</span> permits the scale in a
116       <code class="type">numeric</code> type declaration to be any value in the range
117       -1000 to 1000.  However, the <acronym class="acronym">SQL</acronym> standard requires
118       the scale to be in the range 0 to <em class="replaceable"><code>precision</code></em>.
119       Using scales outside that range may not be portable to other database
120       systems.
121      </p></div><p>
122      Numeric values are physically stored without any extra leading or
123      trailing zeroes.  Thus, the declared precision and scale of a column
124      are maximums, not fixed allocations.  (In this sense the <code class="type">numeric</code>
125      type is more akin to <code class="type">varchar(<em class="replaceable"><code>n</code></em>)</code>
126      than to <code class="type">char(<em class="replaceable"><code>n</code></em>)</code>.)  The actual storage
127      requirement is two bytes for each group of four decimal digits,
128      plus three to eight bytes overhead.
129     </p><a id="id-1.5.7.9.7.13" class="indexterm"></a><a id="id-1.5.7.9.7.14" class="indexterm"></a><a id="id-1.5.7.9.7.15" class="indexterm"></a><p>
130      In addition to ordinary numeric values, the <code class="type">numeric</code> type
131      has several special values:
132 </p><div class="literallayout"><p><br />
133 <code class="literal">Infinity</code><br />
134 <code class="literal">-Infinity</code><br />
135 <code class="literal">NaN</code><br />
136 </p></div><p>
137      These are adapted from the IEEE 754 standard, and represent
138      <span class="quote">“<span class="quote">infinity</span>”</span>, <span class="quote">“<span class="quote">negative infinity</span>”</span>, and
139      <span class="quote">“<span class="quote">not-a-number</span>”</span>, respectively. When writing these values
140      as constants in an SQL command, you must put quotes around them,
141      for example <code class="literal">UPDATE table SET x = '-Infinity'</code>.
142      On input, these strings are recognized in a case-insensitive manner.
143      The infinity values can alternatively be spelled <code class="literal">inf</code>
144      and <code class="literal">-inf</code>.
145     </p><p>
146      The infinity values behave as per mathematical expectations.  For
147      example, <code class="literal">Infinity</code> plus any finite value equals
148      <code class="literal">Infinity</code>, as does <code class="literal">Infinity</code>
149      plus <code class="literal">Infinity</code>; but <code class="literal">Infinity</code>
150      minus <code class="literal">Infinity</code> yields <code class="literal">NaN</code> (not a
151      number), because it has no well-defined interpretation.  Note that an
152      infinity can only be stored in an unconstrained <code class="type">numeric</code>
153      column, because it notionally exceeds any finite precision limit.
154     </p><p>
155      The <code class="literal">NaN</code> (not a number) value is used to represent
156      undefined calculational results.  In general, any operation with
157      a <code class="literal">NaN</code> input yields another <code class="literal">NaN</code>.
158      The only exception is when the operation's other inputs are such that
159      the same output would be obtained if the <code class="literal">NaN</code> were to
160      be replaced by any finite or infinite numeric value; then, that output
161      value is used for <code class="literal">NaN</code> too.  (An example of this
162      principle is that <code class="literal">NaN</code> raised to the zero power
163      yields one.)
164     </p><div class="note"><h3 class="title">Note</h3><p>
165       In most implementations of the <span class="quote">“<span class="quote">not-a-number</span>”</span> concept,
166       <code class="literal">NaN</code> is not considered equal to any other numeric
167       value (including <code class="literal">NaN</code>).  In order to allow
168       <code class="type">numeric</code> values to be sorted and used in tree-based
169       indexes, <span class="productname">PostgreSQL</span> treats <code class="literal">NaN</code>
170       values as equal, and greater than all non-<code class="literal">NaN</code>
171       values.
172      </p></div><p>
173      The types <code class="type">decimal</code> and <code class="type">numeric</code> are
174      equivalent.  Both types are part of the <acronym class="acronym">SQL</acronym>
175      standard.
176     </p><p>
177      When rounding values, the <code class="type">numeric</code> type rounds ties away
178      from zero, while (on most machines) the <code class="type">real</code>
179      and <code class="type">double precision</code> types round ties to the nearest even
180      number.  For example:
181
182 </p><pre class="programlisting">
183 SELECT x,
184   round(x::numeric) AS num_round,
185   round(x::double precision) AS dbl_round
186 FROM generate_series(-3.5, 3.5, 1) as x;
187   x   | num_round | dbl_round
188 ------+-----------+-----------
189  -3.5 |        -4 |        -4
190  -2.5 |        -3 |        -2
191  -1.5 |        -2 |        -2
192  -0.5 |        -1 |        -0
193   0.5 |         1 |         0
194   1.5 |         2 |         2
195   2.5 |         3 |         2
196   3.5 |         4 |         4
197 (8 rows)
198 </pre><p>
199     </p></div><div class="sect2" id="DATATYPE-FLOAT"><div class="titlepage"><div><div><h3 class="title">8.1.3. Floating-Point Types <a href="#DATATYPE-FLOAT" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.9.8.2" class="indexterm"></a><a id="id-1.5.7.9.8.3" class="indexterm"></a><a id="id-1.5.7.9.8.4" class="indexterm"></a><a id="id-1.5.7.9.8.5" class="indexterm"></a><a id="id-1.5.7.9.8.6" class="indexterm"></a><p>
200      The data types <code class="type">real</code> and <code class="type">double precision</code> are
201      inexact, variable-precision numeric types. On all currently supported
202      platforms, these types are implementations of <acronym class="acronym">IEEE</acronym>
203      Standard 754 for Binary Floating-Point Arithmetic (single and double
204      precision, respectively), to the extent that the underlying processor,
205      operating system, and compiler support it.
206     </p><p>
207      Inexact means that some values cannot be converted exactly to the
208      internal format and are stored as approximations, so that storing
209      and retrieving a value might show slight discrepancies.
210      Managing these errors and how they propagate through calculations
211      is the subject of an entire branch of mathematics and computer
212      science and will not be discussed here, except for the
213      following points:
214      </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
215         If you require exact storage and calculations (such as for
216         monetary amounts), use the <code class="type">numeric</code> type instead.
217        </p></li><li class="listitem"><p>
218         If you want to do complicated calculations with these types
219         for anything important, especially if you rely on certain
220         behavior in boundary cases (infinity, underflow), you should
221         evaluate the implementation carefully.
222        </p></li><li class="listitem"><p>
223         Comparing two floating-point values for equality might not
224         always work as expected.
225        </p></li></ul></div><p>
226     </p><p>
227      On all currently supported platforms, the <code class="type">real</code> type has a
228      range of around 1E-37 to 1E+37 with a precision of at least 6 decimal
229      digits. The <code class="type">double precision</code> type has a range of around
230      1E-307 to 1E+308 with a precision of at least 15 digits. Values that are
231      too large or too small will cause an error. Rounding might take place if
232      the precision of an input number is too high. Numbers too close to zero
233      that are not representable as distinct from zero will cause an underflow
234      error.
235     </p><p>
236      By default, floating point values are output in text form in their
237      shortest precise decimal representation; the decimal value produced is
238      closer to the true stored binary value than to any other value
239      representable in the same binary precision. (However, the output value is
240      currently never <span class="emphasis"><em>exactly</em></span> midway between two
241      representable values, in order to avoid a widespread bug where input
242      routines do not properly respect the round-to-nearest-even rule.) This value will
243      use at most 17 significant decimal digits for <code class="type">float8</code>
244      values, and at most 9 digits for <code class="type">float4</code> values.
245     </p><div class="note"><h3 class="title">Note</h3><p>
246       This shortest-precise output format is much faster to generate than the
247       historical rounded format.
248      </p></div><p>
249      For compatibility with output generated by older versions
250      of <span class="productname">PostgreSQL</span>, and to allow the output
251      precision to be reduced, the <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a>
252      parameter can be used to select rounded decimal output instead. Setting a
253      value of 0 restores the previous default of rounding the value to 6
254      (for <code class="type">float4</code>) or 15 (for <code class="type">float8</code>)
255      significant decimal digits. Setting a negative value reduces the number
256      of digits further; for example -2 would round output to 4 or 13 digits
257      respectively.
258     </p><p>
259      Any value of <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a> greater than 0
260      selects the shortest-precise format.
261     </p><div class="note"><h3 class="title">Note</h3><p>
262       Applications that wanted precise values have historically had to set
263       <a class="xref" href="runtime-config-client.html#GUC-EXTRA-FLOAT-DIGITS">extra_float_digits</a> to 3 to obtain them. For
264       maximum compatibility between versions, they should continue to do so.
265      </p></div><a id="id-1.5.7.9.8.15" class="indexterm"></a><a id="id-1.5.7.9.8.16" class="indexterm"></a><p>
266      In addition to ordinary numeric values, the floating-point types
267      have several special values:
268 </p><div class="literallayout"><p><br />
269 <code class="literal">Infinity</code><br />
270 <code class="literal">-Infinity</code><br />
271 <code class="literal">NaN</code><br />
272 </p></div><p>
273      These represent the IEEE 754 special values
274      <span class="quote">“<span class="quote">infinity</span>”</span>, <span class="quote">“<span class="quote">negative infinity</span>”</span>, and
275      <span class="quote">“<span class="quote">not-a-number</span>”</span>, respectively. When writing these values
276      as constants in an SQL command, you must put quotes around them,
277      for example <code class="literal">UPDATE table SET x = '-Infinity'</code>.  On input,
278      these strings are recognized in a case-insensitive manner.
279      The infinity values can alternatively be spelled <code class="literal">inf</code>
280      and <code class="literal">-inf</code>.
281     </p><div class="note"><h3 class="title">Note</h3><p>
282       IEEE 754 specifies that <code class="literal">NaN</code> should not compare equal
283       to any other floating-point value (including <code class="literal">NaN</code>).
284       In order to allow floating-point values to be sorted and used
285       in tree-based indexes, <span class="productname">PostgreSQL</span> treats
286       <code class="literal">NaN</code> values as equal, and greater than all
287       non-<code class="literal">NaN</code> values.
288      </p></div><p>
289      <span class="productname">PostgreSQL</span> also supports the SQL-standard
290      notations <code class="type">float</code> and
291      <code class="type">float(<em class="replaceable"><code>p</code></em>)</code> for specifying
292      inexact numeric types.  Here, <em class="replaceable"><code>p</code></em> specifies
293      the minimum acceptable precision in <span class="emphasis"><em>binary</em></span> digits.
294      <span class="productname">PostgreSQL</span> accepts
295      <code class="type">float(1)</code> to <code class="type">float(24)</code> as selecting the
296      <code class="type">real</code> type, while
297      <code class="type">float(25)</code> to <code class="type">float(53)</code> select
298      <code class="type">double precision</code>.  Values of <em class="replaceable"><code>p</code></em>
299      outside the allowed range draw an error.
300      <code class="type">float</code> with no precision specified is taken to mean
301      <code class="type">double precision</code>.
302     </p></div><div class="sect2" id="DATATYPE-SERIAL"><div class="titlepage"><div><div><h3 class="title">8.1.4. Serial Types <a href="#DATATYPE-SERIAL" class="id_link">#</a></h3></div></div></div><a id="id-1.5.7.9.9.2" class="indexterm"></a><a id="id-1.5.7.9.9.3" class="indexterm"></a><a id="id-1.5.7.9.9.4" class="indexterm"></a><a id="id-1.5.7.9.9.5" class="indexterm"></a><a id="id-1.5.7.9.9.6" class="indexterm"></a><a id="id-1.5.7.9.9.7" class="indexterm"></a><a id="id-1.5.7.9.9.8" class="indexterm"></a><a id="id-1.5.7.9.9.9" class="indexterm"></a><div class="note"><h3 class="title">Note</h3><p>
303       This section describes a PostgreSQL-specific way to create an
304       autoincrementing column.  Another way is to use the SQL-standard
305       identity column feature, described at <a class="xref" href="ddl-identity-columns.html" title="5.3. Identity Columns">Section 5.3</a>.
306      </p></div><p>
307      The data types <code class="type">smallserial</code>, <code class="type">serial</code> and
308      <code class="type">bigserial</code> are not true types, but merely
309      a notational convenience for creating unique identifier columns
310      (similar to the <code class="literal">AUTO_INCREMENT</code> property
311      supported by some other databases). In the current
312      implementation, specifying:
313
314 </p><pre class="programlisting">
315 CREATE TABLE <em class="replaceable"><code>tablename</code></em> (
316     <em class="replaceable"><code>colname</code></em> SERIAL
317 );
318 </pre><p>
319
320      is equivalent to specifying:
321
322 </p><pre class="programlisting">
323 CREATE SEQUENCE <em class="replaceable"><code>tablename</code></em>_<em class="replaceable"><code>colname</code></em>_seq AS integer;
324 CREATE TABLE <em class="replaceable"><code>tablename</code></em> (
325     <em class="replaceable"><code>colname</code></em> integer NOT NULL DEFAULT nextval('<em class="replaceable"><code>tablename</code></em>_<em class="replaceable"><code>colname</code></em>_seq')
326 );
327 ALTER SEQUENCE <em class="replaceable"><code>tablename</code></em>_<em class="replaceable"><code>colname</code></em>_seq OWNED BY <em class="replaceable"><code>tablename</code></em>.<em class="replaceable"><code>colname</code></em>;
328 </pre><p>
329
330      Thus, we have created an integer column and arranged for its default
331      values to be assigned from a sequence generator.  A <code class="literal">NOT NULL</code>
332      constraint is applied to ensure that a null value cannot be
333      inserted.  (In most cases you would also want to attach a
334      <code class="literal">UNIQUE</code> or <code class="literal">PRIMARY KEY</code> constraint to prevent
335      duplicate values from being inserted by accident, but this is
336      not automatic.)  Lastly, the sequence is marked as <span class="quote">“<span class="quote">owned by</span>”</span>
337      the column, so that it will be dropped if the column or table is dropped.
338     </p><div class="note"><h3 class="title">Note</h3><p>
339         Because <code class="type">smallserial</code>, <code class="type">serial</code> and
340         <code class="type">bigserial</code> are implemented using sequences, there may
341         be "holes" or gaps in the sequence of values which appears in the
342         column, even if no rows are ever deleted.  A value allocated
343         from the sequence is still "used up" even if a row containing that
344         value is never successfully inserted into the table column.  This
345         may happen, for example, if the inserting transaction rolls back.
346         See <code class="literal">nextval()</code> in <a class="xref" href="functions-sequence.html" title="9.17. Sequence Manipulation Functions">Section 9.17</a>
347         for details.
348       </p></div><p>
349      To insert the next value of the sequence into the <code class="type">serial</code>
350      column, specify that the <code class="type">serial</code>
351      column should be assigned its default value. This can be done
352      either by excluding the column from the list of columns in
353      the <code class="command">INSERT</code> statement, or through the use of
354      the <code class="literal">DEFAULT</code> key word.
355     </p><p>
356      The type names <code class="type">serial</code> and <code class="type">serial4</code> are
357      equivalent: both create <code class="type">integer</code> columns.  The type
358      names <code class="type">bigserial</code> and <code class="type">serial8</code> work
359      the same way, except that they create a <code class="type">bigint</code>
360      column.  <code class="type">bigserial</code> should be used if you anticipate
361      the use of more than 2<sup>31</sup> identifiers over the
362      lifetime of the table. The type names <code class="type">smallserial</code> and
363      <code class="type">serial2</code> also work the same way, except that they
364      create a <code class="type">smallint</code> column.
365     </p><p>
366      The sequence created for a <code class="type">serial</code> column is
367      automatically dropped when the owning column is dropped.
368      You can drop the sequence without dropping the column, but this
369      will force removal of the column default expression.
370     </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datatype.html" title="Chapter 8. Data Types">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-money.html" title="8.2. Monetary Types">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 8. Data Types </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 8.2. Monetary Types</td></tr></table></div></body></html>