1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.57 2001/08/07 22:41:49 petere Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.58 2001/08/16 20:38:53 tgl Exp $
3
3
-->
4
4
5
5
<chapter id="datatype">
@@ -199,10 +199,16 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.57 2001/08/07 22:41:49 pe
199
199
200
200
<row>
201
201
<entry><type>serial</type></entry>
202
- <entry></entry>
202
+ <entry><type>serial4</type>< /entry>
203
203
<entry>autoincrementing four-byte integer</entry>
204
204
</row>
205
205
206
+ <row>
207
+ <entry><type>serial8</type></entry>
208
+ <entry></entry>
209
+ <entry>autoincrementing eight-byte integer</entry>
210
+ </row>
211
+
206
212
<row>
207
213
<entry><type>text</type></entry>
208
214
<entry></entry>
@@ -394,10 +400,17 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.57 2001/08/07 22:41:49 pe
394
400
</row>
395
401
396
402
<row>
397
- <entry>serial </entry>
403
+ <entry>serial4 </entry>
398
404
<entry>4 bytes</entry>
399
405
<entry>Identifier or cross-reference</entry>
400
- <entry>0 to +2147483647</entry>
406
+ <entry>1 to 2147483647</entry>
407
+ </row>
408
+
409
+ <row>
410
+ <entry>serial8</entry>
411
+ <entry>8 bytes</entry>
412
+ <entry>Identifier or cross-reference</entry>
413
+ <entry>1 to 9223372036854775807</entry>
401
414
</row>
402
415
</tbody>
403
416
</tgroup>
@@ -413,17 +426,27 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.57 2001/08/07 22:41:49 pe
413
426
</para>
414
427
415
428
<para>
416
- The <type>bigint</type> type may not be available on all platforms since
417
- it relies on compiler support for eight-byte integers.
429
+ The <type>bigint</type> type may not function correctly on all platforms,
430
+ since it relies on compiler support for eight-byte integers. On a machine
431
+ without such support, <type>bigint</type> acts the same
432
+ as <type>integer</type> (but still takes up eight bytes of storage).
418
433
</para>
419
434
420
435
<sect2 id="datatype-serial">
421
- <title>The Serial Type </title>
436
+ <title>The Serial Types </title>
422
437
423
438
<indexterm zone="datatype-serial">
424
439
<primary>serial</primary>
425
440
</indexterm>
426
441
442
+ <indexterm zone="datatype-serial">
443
+ <primary>serial4</primary>
444
+ </indexterm>
445
+
446
+ <indexterm zone="datatype-serial">
447
+ <primary>serial8</primary>
448
+ </indexterm>
449
+
427
450
<indexterm>
428
451
<primary>auto-increment</primary>
429
452
<see>serial</see>
@@ -435,9 +458,9 @@ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.57 2001/08/07 22:41:49 pe
435
458
</indexterm>
436
459
437
460
<para>
438
- The <type>serial</type> type is a special-case type constructed by
439
- <productname>Postgres</productname> from other existing components.
440
- It is typically used to create unique identifiers for table entries .
461
+ The <type>serial</type> datatypes are not truly types, but are a
462
+ notational convenience for setting up unique identifier columns
463
+ in tables .
441
464
In the current implementation, specifying
442
465
443
466
<programlisting>
@@ -449,18 +472,33 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceabl
449
472
<programlisting>
450
473
CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
451
474
CREATE TABLE <replaceable class="parameter">tablename</replaceable>
452
- (<replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq');
453
- CREATE UNIQUE INDEX <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_key on <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable>);
475
+ (<replaceable class="parameter">colname</replaceable> integer DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq') UNIQUE NOT NULL;
454
476
</programlisting>
455
477
478
+ Thus, we have created an integer column and arranged for its default
479
+ values to be assigned from a sequence generator. UNIQUE and NOT NULL
480
+ constraints are applied to ensure that explicitly-inserted values
481
+ will never be duplicates, either.
482
+
456
483
<caution>
457
484
<para>
458
485
The implicit sequence created for the <type>serial</type> type will
459
486
<emphasis>not</emphasis> be automatically removed when the
460
487
table is dropped.
461
488
</para>
462
489
</caution>
490
+ </para>
463
491
492
+ <para>
493
+ The type names <type>serial</type> and <type>serial4</type> are
494
+ equivalent: both create <type>integer</type> columns. The type
495
+ name <type>serial8</type> works just the same way, except that it
496
+ creates a <type>bigint</type> column. <type>serial8</type> should
497
+ be used if you anticipate use of more than 2^31 identifiers over
498
+ the lifetime of the table.
499
+ </para>
500
+
501
+ <para>
464
502
Implicit sequences supporting the <type>serial</type> are
465
503
not automatically dropped when a table containing a serial type
466
504
is dropped. So, the following commands executed in order will likely fail:
0 commit comments