pg.ddx.io pgsql-general@postgresql.org mailing list archivehelp / color / mirror / Atom feed
'Identifier' columns 2+ messages / 2 participants [nested] [flat]
* 'Identifier' columns @ 2018-08-14 02:19 David Favro <postgres@meta-dynamic.com> 2018-08-14 07:17 ` Re: 'Identifier' columns Laurenz Albe <laurenz.albe@cybertec.at> 0 siblings, 1 reply; 2+ messages in thread From: David Favro @ 2018-08-14 02:19 UTC (permalink / raw) To: pgsql-general@lists.postgresql.org A couple of questions about auto-assigned identifier columns, forgive my ignorance, I'm used to other methods to create IDs... 1. If creating a new application [i.e. no "legacy" reasons to do anything] using PostgreSQL 10, when creating an "auto-assigned integer ID" column, what are the advantages/disadvantages of using the 'SERIAL' datatype [or equivalent explicitly created SEQUENCE w/ nextval() used as default for column] versus the SQL-standard 'integer GENERATED AS IDENTITY'? All other things being equal, it would seem a no-brainer to follow the standard. 2. When using the SQL-standard 'integer GENERATED AS IDENTITY' column, after inserting a column, what is the recommended method to find the ID of the just-inserted row? Is there no SQL-standard way? The docs seem to imply (without explicitly stating) that a SEQUENCE is used behind the scenes hence 'currval()' could be used, but I didn't see in the docs any mention of what the underlying sequence's name is, or how to specify a name. Perhaps 'lastval()' would work, but not in all cases and in any event it has a sloppy feel to me. Thank you in advance for any advice that can be offered. -- David ^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: 'Identifier' columns 2018-08-14 02:19 'Identifier' columns David Favro <postgres@meta-dynamic.com> @ 2018-08-14 07:17 ` Laurenz Albe <laurenz.albe@cybertec.at> 0 siblings, 0 replies; 2+ messages in thread From: Laurenz Albe @ 2018-08-14 07:17 UTC (permalink / raw) To: David Favro <postgres@meta-dynamic.com>; pgsql-general@lists.postgresql.org David Favro wrote: > A couple of questions about auto-assigned identifier columns, > forgive my ignorance, I'm used to other methods to create IDs... > > 1. If creating a new application [i.e. no "legacy" reasons to do > anything] using PostgreSQL 10, when creating an "auto-assigned > integer ID" column, what are the advantages/disadvantages of using > the 'SERIAL' datatype [or equivalent explicitly created SEQUENCE w/ > nextval() used as default for column] versus the SQL-standard > 'integer GENERATED AS IDENTITY'? All other things being equal, it > would seem a no-brainer to follow the standard. Absolutely. Use GENERATED ALWAYS AS IDENTITY. > 2. When using the SQL-standard 'integer GENERATED AS IDENTITY' > column, after inserting a column, what is the recommended method to > find the ID of the just-inserted row? Is there no SQL-standard way? > The docs seem to imply (without explicitly stating) that a SEQUENCE > is used behind the scenes hence 'currval()' could be used, but I > didn't see in the docs any mention of what the underlying sequence's > name is, or how to specify a name. Perhaps 'lastval()' would work, > but not in all cases and in any event it has a sloppy feel to me. The best way is to use the (non-standard) RETURNING clause: INSERT ... RETURNING id; Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com ^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2018-08-14 07:17 UTC | newest] Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2018-08-14 02:19 'Identifier' columns David Favro <postgres@meta-dynamic.com> 2018-08-14 07:17 ` Laurenz Albe <laurenz.albe@cybertec.at>
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