pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael J Davis <michael.j.davis@tvguide.com>
To: 'Herouth Maoz' <herouth@oumail.openu.ac.il>
To: pgsql-sql@postgreSQL.org
Subject: RE: [SQL] Getting primary key from insert statement
Date: Tue, 8 Jun 1999 11:23:14 -0500
Message-ID: <93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com> (raw)

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 AM
	To:	pgsql-sql@postgreSQL.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

	



view thread (14+ messages)  latest in thread

Message-ID: <93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com>
Permalink:  ../93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com/
Also on:    postgresql.org/message-id/93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: michael.j.davis@tvguide.com, herouth@oumail.openu.ac.il
  Subject: Re: [SQL] Getting primary key from insert statement
  In-Reply-To: <93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com>
  References: <93C04F1F5173D211A27900105AA8FCFC14558A@lambic.prevuenet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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