RE: [SQL] Getting primary key from insert statement - Mailing list pgsql-sql

From Michael J Davis
Subject RE: [SQL] Getting primary key from insert statement
Date
Msg-id 93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com
Whole thread Raw
Responses RE: [SQL] Getting primary key from insert statement
List 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@oumail.openu.ac.il]Sent:    Tuesday, June 08, 1999 2:27
AMTo:   pgsql-sql@postgreSQL.orgSubject:    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 notneeded (although logically, the two operations should be one, so as
not toallow 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 nological connection between the sequence and the
table.Anybody is
 
free toenter any primary key, and a mistake in one of the front ends will
causeinconsistency in the database. The theory of databases asserts that
youshould try to insert the logic and constraints of the organization's
datainto the backend.
I know that this should mean that in fields declared "serial",
values otherthan the default should not be allowed. Perhaps this should be
addressed innew versions of Postgres somewhere.
Herouth
--Herouth Maoz, Internet developer.Open University of Israel - Telem projecthttp://telem.openu.ac.il/~herutma



pgsql-sql by date:

Previous
From: "Jackson, DeJuan"
Date:
Subject: RE: [SQL] Mail about duplicate rows
Next
From: Herouth Maoz
Date:
Subject: RE: [SQL] Getting primary key from insert statement