Re: The best option to insert data with primary id

From: mike(at)if-then-else(dot)pl
To: grandebuzon(at)gmail(dot)com
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: The best option to insert data with primary id
Date: 2010-12-06 15:27:25
Message-ID: 20101206162725.o3q9xfug2ioo08g8@horde.iq.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Quoting - <grandebuzon(at)gmail(dot)com>:

> I have a question about how best to insert and manipulate the table with
> primary key id for better productivity. I need to insert data into the table
> and get last id.
>
> 1. First option to take counter Postgres SEQUENCE:
> INSERT INTO table (id, ...) VALUES ((SELECT nextval ('seq_table')), ...)
> RETURNING (SELECT currval ('seq_table')) AS id

Much simplier:

INSERT INTO table (name, email, salary) VALUES ('joe', 'joe(at)example(dot)com',
100) RETURNING id;

Cheers,

-Mike

--
Michał Roszka
mike(at)if-then-else(dot)pl

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message mike 2010-12-06 16:21:31 Re: The best option to insert data with primary id
Previous Message - 2010-12-06 14:59:28 The best option to insert data with primary id