Обсуждение: RE: [SQL] Getting primary key from insert statement

Поиск
Список
Период
Сортировка

RE: [SQL] Getting primary key from insert statement

От
Michael J Davis
Дата:
I have been using this technique for years in Oracle (select next value from
the primary key sequence and insert using this primary key).  I like this
approach.  I don't think a transaction is needed.  Access97 definitely likes
this approach better because it wants to reselect the newly inserted record.
Without the primary key in the insert statement, the re-select can be very
slow and potentially fail because the re-select is only using values
provided in the insert statement.  Retrieving the primary key after the
insert does not help Access97.

> -----Original Message-----
> From:    Herouth Maoz [SMTP:herouth@oumail.openu.ac.il]
> Sent:    Monday, June 07, 1999 1:29 AM
> To:    emils@mail.usis.bkc.lv; pgsql-sql@postgreSQL.org
> Subject:    Re: [SQL] Getting primary key from insert statement
> 
> At 12:51 +0300 on 07/06/1999, Emils Klotins wrote:
> 
> 
> >
> > Wouldn't it be simpler just to SELECT the next value from the sequence
> >BEFORE the insert and
> > use it in the INSERT statement directly?
> 
> Then you have to have a transaction around the two operations. And by the
> time you do the select, someone may lock the table where you insert. It's
> a
> potential for deadlock.
> 
> Herouth
> 
> --
> Herouth Maoz, Internet developer.
> Open University of Israel - Telem project
> http://telem.openu.ac.il/~herutma
> 
> 


RE: [SQL] Getting primary key from insert statement

От
Herouth Maoz
Дата:
At 23:10 +0300 on 07/06/1999, Michael J Davis wrote:


> I have been using this technique for years in Oracle (select next value from
> the primary key sequence and insert using this primary key).  I like this
> approach.  I don't think a transaction is needed.

I don't know what mechanism Oracle uses. Perhaps even a transaction is not
needed (although logically, the two operations should be one, so as not to
allow the failure of the insertion, if only to save on unused key values).

But doing things like that on the client side means that there is no
logical connection between the sequence and the table. Anybody is free to
enter any primary key, and a mistake in one of the front ends will cause
inconsistency in the database. The theory of databases asserts that you
should try to insert the logic and constraints of the organization's data
into the backend.

I know that this should mean that in fields declared "serial", values other
than the default should not be allowed. Perhaps this should be addressed in
new versions of Postgres somewhere.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma