mysql_insert_id alternative for postgresql

From the PostgreSQL point of view, in pseudo-code: * $insert_id = INSERT…RETURNING foo_id;– only works for PostgreSQL >= 8.2. * INSERT…; $insert_id = SELECT lastval(); — works for PostgreSQL >= 8.1 * $insert_id = SELECT nextval(‘foo_seq’); INSERT INTO table (foo…) values ($insert_id…) for older PostgreSQL (and newer PostgreSQL) pg_last_oid() only works where you have OIDs. … Read more

MySQL ON DUPLICATE KEY – last insert id?

Check this page out: https://web.archive.org/web/20150329004325/https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html At the bottom of the page they explain how you can make LAST_INSERT_ID meaningful for updates by passing an expression to that MySQL function. From the MySQL documentation example: If a table contains an AUTO_INCREMENT column and INSERT … UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. … Read more

How to get the insert ID in JDBC?

If it is an auto generated key, then you can use Statement#getGeneratedKeys() for this. You need to call it on the same Statement as the one being used for the INSERT. You first need to create the statement using Statement.RETURN_GENERATED_KEYS to notify the JDBC driver to return the keys. Here’s a basic example: public void … Read more