in reply to Re: Unique ID
in thread Unique ID

So if i use a serial type then i do not have to use the sequence as well. Or do i have to use a combination of both?

Replies are listed 'Best First'.
Re: Re: Re: Unique ID
by lhoward (Vicar) on Jul 19, 2001 at 21:28 UTC
    The serial type creates and uses a sequence behing the scenes, but its operation is transparent to you. You do not need to create an additional sequence to use it. See... (using psql, Postgres's command line tool):
    test=# create table foo(ID serial NOT NULL,bar text, constraint foo_pk primary key (ID));
    NOTICE:  CREATE TABLE will create implicit sequence 'foo_id_seq' for SERIAL column 'foo.id'
    NOTICE:  CREATE TABLE/PRIMARY KEY will create implicit index 'foo_pk' for table 'foo'
    CREATE
    test=# insert into foo (bar) values ('abc');
    INSERT 27135 1
    test=# insert into foo (bar) values ('def');
    INSERT 27136 1
    test=# select * from foo;
     id | bar
    ----+-----
      1 | abc
      2 | def
    (2 rows)
    
    I find postgres sequences to be most useful when you want to share one unique ID across tables and when you don't have a master table to store it in (or don't have a table that will always be inserted first). Or when you want to assign something outside of the DB (at least at the time of assignment) a unique ID.