RE: [SQL] Getting primary key from insert statement

From: Michael J Davis <michael(dot)j(dot)davis(at)tvguide(dot)com>
To: "'Herouth Maoz'" <herouth(at)oumail(dot)openu(dot)ac(dot)il>, pgsql-sql(at)postgreSQL(dot)org
Subject: RE: [SQL] Getting primary key from insert statement
Date: 1999-06-08 16:23:14
Message-ID: 93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Perhaps I need to qualify my approach a little more. Let me give an
example:

create table Status
(
StatusID int4 PRIMARY KEY DEFAULT
nextval('Status_seq'),
StatusName varchar(32) NOT NULL,
);

A normal insert in PostgreSQL would look like this:

Insert into Status (StatusName) values ('name of status');

An insert using Access97 would look like this:

Select nextval('Status_seq') into StatusID_value;
Insert into Status (StatusID, StatusName) values (StatusID_value,
'name of status');

Either way the StatusID or primary key is set the same way using the same
technique. One method lets PostgreSQL determine the primary key while the
other method proactively determines the primary key is before the insert.

I accomplish this by trapping the "Before Insert" event in the form used to
add new Status records (the user is NOT entering the StatusID) and doing the
following:

Form.StatusID = getSeqValue('Status_seq')

Where getSeqValue() is defined below:

Public function getSeqValue(sequence_name as string)
// basically this function does the following:
Select nextval('Status_seq') into StatusID;
End function

-----Original Message-----
From: Herouth Maoz [SMTP:herouth(at)oumail(dot)openu(dot)ac(dot)il]
Sent: Tuesday, June 08, 1999 2:27 AM
To: pgsql-sql(at)postgreSQL(dot)org
Subject: RE: [SQL] Getting primary key from insert statement

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Herouth Maoz 1999-06-08 16:40:17 RE: [SQL] Getting primary key from insert statement
Previous Message Jackson, DeJuan 1999-06-08 15:37:24 RE: [SQL] Mail about duplicate rows