next up previous contents index
Next: Manually Numbering Rows Up: Numbering Rows Previous: Using Sequences to Number

   
Serial Column Type

An even easier way to use sequences exists. If you define a column of type SERIAL, a sequence will be automatically created, and a proper DEFAULT  will be assigned to the column. Figure [*] shows an example.

 

        test=> CREATE TABLE customer (
        test(>           customer_id SERIAL,
        test(>           name CHAR(30)
        test(> );
        NOTICE:  CREATE TABLE will create implicit sequence 'customer_customer_id_seq' for SERIAL column 'customer.customer_id'
        NOTICE:  CREATE TABLE/UNIQUE will create implicit index 'customer_customer_id_key' for table 'customer'
        CREATE
        test=> \d customer
                                          Table "customer"
          Attribute  |   Type   |                           Extra                            
        -------------+----------+------------------------------------------------------------
         customer_id | int4     | not null default nextval('customer_customer_id_seq'::text)
         name        | char(30) | 
        Index: customer_customer_id_key
        test=> INSERT INTO customer (name) VALUES ('Car Wash');      
        INSERT 19152 1
        test=> SELECT * FROM customer;
         customer_id |              name              
        -------------+--------------------------------
                   1 | Car Wash                      
        (1 row)
 

The first NOTICE line indicates that a sequence was created for the SERIAL column. Do not be concerned about the second NOTICE line in the figure. (Indexes  are covered in Section [*].)   


Bruce Momjian
2001-05-09