Question (or feature request) on serial datatype

From: "Kian Spongsveen (spam account)" <spam(at)kian(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Question (or feature request) on serial datatype
Date: 2003-07-22 10:24:00
Message-ID: 200307221224.00878.spam@kian.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

It seems the serial datatype is a frequently asked questions for novices
introduced to PostgreSQL. It appears that the competing RDBMSes are simpler
to handle in that respect, when you want to insert data and have a column
autoincremented, then fetch the value that was inserted to use in the next
insert.

In Sybase (ASE and ASA) and MS SQL I can define a column as identity, do an
insert and then select @@identity to find out what was generated for me. If I
replace identity columns with type serial as a simple search and replace, I
find that I still have to know the underlying sequence name. What is the
simplest way to do this?

What I would like to do is either:
create table foo(
colA serial,
bar varchar(255)
);
begin tran
insert into foo (bar) values ('abc');
select currval(colA) from foo;
commit
I assume here that nextval would be implicitly called by the insert so I
could directly call currval *after* inserting. Doing a manual nextval()
before the insert is OK, too, but from my understanding it needs the sequence
name and not the column name?

or,
create table foo(
colA serial,
bar varchar(255)
);
begin tran
insert into foo (bar) values ('abc');
select currval(seqname(colA)) from foo;
commit

Where seqname returns the underlying sequence name for the serial column.

The reason this is needed is that during the initial design phase a column
name is decided, the actual implementation on each RDBMS comes much later.
When the RDBMS is decided, you want to hide the annoyances of each platform
as much as possible. Having to know at a later phase that a certain sequence
name has been associated with the serial column is unneccessary for the
front-end developers. I can't find any simple way of programatically finding
the sequence name either. I believe this is much simpler in Sybase ASE/ASA,
Microsoft Access/SQL Server and MySQL so PostgreSQL should improve the
functionality too?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dima Tkach 2003-07-22 13:18:42 Re: can anybody help me please
Previous Message Ron Johnson 2003-07-22 09:56:06 Re: Need help to take PGSql Dump