Re: alter table serial->int

From: "Igor Neyman" <ineyman(at)perceptron(dot)com>
To: "Erik Aronesty" <erik(at)q32(dot)com>, <pgsql-admin(at)postgresql(dot)org>
Subject: Re: alter table serial->int
Date: 2007-11-08 20:04:59
Message-ID: F4C27E77F7A33E4CA98C19A9DC6722A2025AB018@EXCHANGE.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Eric,

Don't be so defensive.
Here is an abstract from documentation:

"8.1.4. Serial Types

The data types serial and bigserial are not true types, but merely a
notational convenience for setting up unique identifier columns (similar
to the AUTO_INCREMENT property supported by some other databases). In
the current implementation, specifying

CREATE TABLE tablename (
colname SERIAL
);

is equivalent to specifying:

CREATE SEQUENCE tablename_colname_seq;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;

Thus, we have created an integer column and arranged for its default
values to be assigned from a sequence generator. A NOT NULL constraint
is applied to ensure that a null value cannot be explicitly inserted,
either. (In most cases you would also want to attach a UNIQUE or PRIMARY
KEY constraint to prevent duplicate values from being inserted by
accident, but this is not automatic.) Lastly, the sequence is marked as
"owned by" the column, so that it will be dropped if the column or table
is dropped.

Note: Prior to PostgreSQL 7.3, serial implied UNIQUE. This is no
longer automatic. If you wish a serial column to be in a unique
constraint or a primary key, it must now be specified, same as with any
other data type.

To insert the next value of the sequence into the serial column, specify
that the serial column should be assigned its default value. This can be
done either by excluding the column from the list of columns in the
INSERT statement, or through the use of the DEFAULT key word.

The type names serial and serial4 are equivalent: both create integer
columns. The type names bigserial and serial8 work just the same way,
except that they create a bigint column. bigserial should be used if you
anticipate the use of more than 231 identifiers over the lifetime of the
table.

The sequence created for a serial column is automatically dropped when
the owning column is dropped. You can drop the sequence without dropping
the column, but this will force removal of the column default
expression. "

which proves that you don't understand "serial" data type.

Igor

-----Original Message-----
From: pgsql-admin-owner(at)postgresql(dot)org
[mailto:pgsql-admin-owner(at)postgresql(dot)org] On Behalf Of Erik Aronesty
Sent: Thursday, November 08, 2007 11:14 AM
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: [ADMIN] alter table serial->int

> "Erik Aronesty" <erik(at)q32(dot)com> writes:
> > for some odd reason when i try to change a table from serial to just

> > plain "int with a default" postgres seems to ignore me.
>
> What PG version?

(PostgreSQL) 8.1.10

> The fact that you even tried that suggests that you don't understand
> very well what "serial" is. See the manual ...

I tried to change the data type first. Which implies I know what it
means.

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message König, Monika 2007-11-09 10:20:23 configure using libxml
Previous Message Alvaro Herrera 2007-11-08 19:57:21 Re: alter table serial->int