Skip site navigation (1) Skip section navigation (2)

MySQL LAST_INSERT_ID() to Postgres

From: "Masis, Alexander \(US SSA\)" <alexander(dot)masis(at)baesystems(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: MySQL LAST_INSERT_ID() to Postgres
Date: 2008-08-28 16:14:59
Message-ID: 738lm5$ (view raw, whole thread or download thread mbox)
Lists: pgsql-general
I was mapping C++ application code that works with mySQL to work with
There were a number of articles on line regarding the conversion from
mySQL to Postgres like:

However, I found the most difficult issue was related to a MySQL's
If your code did not use LAST_INSERT_ID(), then you don't have to read
this post.
	In MySQL "LAST_INSERT_ID()" is a MySQL's syntax that returns the
last auto_increment type ID of the row(record) inserted in a table. 

	In other words, if your MySQL table had a auto_increment
datatype for a field, that field will automatically advance whenever a
new record(row) is inserted into that table.

	It is sometimes handy to know what is the value of that ID, that
has just been added to the table, so that that record(row) can be
addressed/updated later.

Well, in MySQL it's easy you just do:
	In Postgres, however it is not that simple. You have to know the
name of so called "insert sequence". Postgres has a system function for
that( SQL line below ).
In Postgres you will have to provide the table and column name(
"auto_increment" type in MySQL or "serial or bigserial" in Postgres).

Here is that SQL query that returns the last inserted ID:


Alexander Masis.


pgsql-general by date

Next:From: Joao Ferreira gmailDate: 2008-08-28 16:15:06
Subject: Re: Vaccuum best practice: cronjob or autovaccuum?
Previous:From: Tom LaneDate: 2008-08-28 16:07:05
Subject: Re: pg_dump problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group