get primary key new inserted row postgresql

How to Get Primary Key of Newly Inserted Row in PostgreSQL

PostgreSQL is a popular database used by millions of websites and organizations. It allows you to easily insert, modify and delete data. In most database tables, each row is identified by unique primary key. Sometimes you may need to get primary key of newly inserted row in PostgreSQL. Here are the steps to do it.


How to Get Primary Key of Newly Inserted Row in PostgreSQL

There are several ways to get primary key of newly inserted row in PostgreSQL. However, one of the easies ways to do this is to use RETURNING clause as shown below. You can use it with INSERT, UPDATE and DELETE statements.

INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...) RETURNING ID;

The above query will return the ID of inserted row. Also it saves an additional query to your server.

The RETURNING clause was introduced in PostgreSQL 8.2.

Also read:

How to Get Primary Key of Newly Inserted Row in MySQL
How to Find Tables With Column Name in MySQL
How to Find my.cnf File Location in MySQL
How to Add Option to Select Using jQuery
How to Use JavaScript Variables in jQuery Selectors

Leave a Reply

Your email address will not be published.