Re: Alternative Serial

From: "Milen A(dot) Radev" <milen(at)radev(dot)net>
To: pgadmin-support(at)postgresql(dot)org
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Alternative Serial
Date: 2007-03-11 14:47:52
Message-ID: et14pj$5u5$1@sea.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgadmin-support pgsql-sql

Ezequias Rodrigues da Rocha написа:
> Hi list,
>
> I know that serials must be created like this.

Actually no.

> Create the Serial

There is no db object 'Serial' you could create independently - you
probably mean 'sequence'.

>
> Create a field table and reffers to the pre-created serial.

You could and should create a field of (pseudo-)type 'serial' and be
done with it.

>
> Now I noticed that it is possible to use the default value. I am almost
> quite sure that this is an alternative way and non polite.

The old and still working way to create a field that would function as
surrogate key is to create a sequence, create a field in the table, mark
it integer and set its default value to be the 'nextval' of the created
sequence. That was too much typing for some (I'm included) and later a
syntax sugar was added - the pseudo-type 'serial'.

So the following syntax:

CREATE TABLE example (
example_sid SERIAL NOT NULL PRIMARY KEY,
...
)

is equivalen to:

CREATE SEQUENCE example_example_sid_seq;

CREATE TABLE example (
example_sid INTEGER PRIMARY KEY DEFAULT
NEXVAL)('example_example_sid_seq'),
...
)

As you could see - very convenient for the 99.9% of the cases.

>
> I would like to know if I am correct and what does it means:
>
> nextval('mySerial_id'::regclass)
>
> What is regclass ?

Read here about 'regclass' (there is a note about it) -
http://www.postgresql.org/docs/8.2/static/functions-sequence.html, but
that should not be important to you.

--
Milen A. Radev

In response to

Responses

Browse pgadmin-support by date

  From Date Subject
Next Message Ezequias Rodrigues da Rocha 2007-03-11 15:08:59 Re: Alternative Serial
Previous Message Ezequias Rodrigues da Rocha 2007-03-11 14:32:07 Re: On 19010 pgAdmin doesn't run

Browse pgsql-sql by date

  From Date Subject
Next Message Ezequias Rodrigues da Rocha 2007-03-11 15:08:59 Re: Alternative Serial
Previous Message Ezequias Rodrigues da Rocha 2007-03-11 14:20:33 Alternative Serial