pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Question re: serial
3+ messages / 3 participants
[nested] [flat]

* Question re: serial
@ 2004-04-24 17:24 Michael Satterwhite <michael@weblore.com>
  2004-04-24 17:37 ` Re: Question re: serial Devrim GUNDUZ <devrim@gunduz.org>
  2004-04-24 17:44 ` Re: Question re: serial Bruno Wolff III <bruno@wolff.to>
  0 siblings, 2 replies; 3+ messages in thread

From: Michael Satterwhite @ 2004-04-24 17:24 UTC (permalink / raw)
  To: pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I have a table with a primary key defined as serial unique. When I insert a 
row into the table, I need to get the value of that row back so that I can 
use it to link rows in another table to it. How do I do that in Postgresql? I 
know that I can define a sequence and insert the value manually, but is there 
a way to query the value of the most recently inserted serial column of a 
row?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)

iD8DBQFAiqK0jeziQOokQnARArJKAJ0SEs8DO6ETa6yZI2Wq6SOmydEwzQCeIwLF
on3NovbSaHa+tBrvrynyQfI=
=d0AB
-----END PGP SIGNATURE-----




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Question re: serial
  2004-04-24 17:24 Question re: serial Michael Satterwhite <michael@weblore.com>
@ 2004-04-24 17:37 ` Devrim GUNDUZ <devrim@gunduz.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Devrim GUNDUZ @ 2004-04-24 17:37 UTC (permalink / raw)
  To: Michael Satterwhite <michael@weblore.com>; +Cc: pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Hi,

On Sat, 24 Apr 2004, Michael Satterwhite wrote:

> I have a table with a primary key defined as serial unique. When I insert a
> row into the table, I need to get the value of that row back so that I can
> use it to link rows in another table to it. How do I do that in Postgresql? I
> know that I can define a sequence and insert the value manually, but is there
> a way to query the value of the most recently inserted serial column of a
> row?

See currval() and nextval() functions:

Regards,
- -- 
Devrim GUNDUZ	       
devrim@gunduz.org				devrim.gunduz@linux.org.tr 
			http://www.TDMSoft.com
			http://www.gunduz.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFAiqXrtl86P3SPfQ4RAsxkAKCQ9oL9Jo4a7VZwqyI4YU6L7i3nEACgv/wM
0ce7up3AVctCYyksk7OYIpY=
=8Hr5
-----END PGP SIGNATURE-----




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Question re: serial
  2004-04-24 17:24 Question re: serial Michael Satterwhite <michael@weblore.com>
@ 2004-04-24 17:44 ` Bruno Wolff III <bruno@wolff.to>
  1 sibling, 0 replies; 3+ messages in thread

From: Bruno Wolff III @ 2004-04-24 17:44 UTC (permalink / raw)
  To: Michael Satterwhite <michael@weblore.com>; +Cc: pgsql-sql

On Sat, Apr 24, 2004 at 12:24:03 -0500,
  Michael Satterwhite <michael@weblore.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> I have a table with a primary key defined as serial unique. When I insert a 
> row into the table, I need to get the value of that row back so that I can 
> use it to link rows in another table to it. How do I do that in Postgresql? I 
> know that I can define a sequence and insert the value manually, but is there 
> a way to query the value of the most recently inserted serial column of a 
> row?

When you use a serial type there a sequence is created for you. You
can reference the most recent value of that sequence within the current
session using currval.

You can use the \d command in psql to see what sequence is being used to
calculate the default value.



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2004-04-24 17:44 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-04-24 17:24 Question re: serial Michael Satterwhite <michael@weblore.com>
2004-04-24 17:37 ` Devrim GUNDUZ <devrim@gunduz.org>
2004-04-24 17:44 ` Bruno Wolff III <bruno@wolff.to>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox