HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Creating a PostgreSQL SERIAL column using pgAdmin3

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlpgadmin3columncreatingserialusing

Problem

When I use pgAdmin3 to create a column of type serial, why does it instead create nextval('prices_id_seq'::regclass)?

Should we keep this type defined by pgAdmin? Manually create the table with SQL defining id column as type serial causes the pgAdmin SQL pane to again display it as nextval('prices_id_seq'::regclass).

pgAdmin SQL Pane

id integer NOT NULL DEFAULT nextval('prices_id_seq'::regclass)

Solution

This is not a pgAdmin feature but that of PostgreSQL. If you check your table from an other client (eg. psql) you will see the exact same column definition. Why is it so?


The data types serial and bigserial are not true types, but merely a
notational convenience for creating 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;


It is always useful to consult the documentation.

Code Snippets

CREATE TABLE tablename (
    colname SERIAL
);
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;

Context

StackExchange Database Administrators Q#40856, answer score: 4

Revisions (0)

No revisions yet.