[Re: PRIMARY KEY] - Mailing list pgsql-sql

From Phillip Smith
Subject [Re: PRIMARY KEY]
Date
Msg-id 1173270012.4925.66.camel@phillip-desktop
Whole thread Raw
Responses Re: [Re: PRIMARY KEY]  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-sql
If you actually need to know the value of N_GEN in your ASP application, you will need to query the database first and
selectthe NEXTVAL from the sequence that the "serial" data type will create, then use that returned value in your
insert- ie, DON'T exclude it from the insert, otherwise it will default to NEXTVAL again and return a different
value.<br/><br /> The only catch with this is that you most likely won't end up with contiguous values in this column -
ie,if a user cancels after you seect nextval, but before you insert - the value of the sequence has already increased,
andwill be increased again before returning a value when you next select nextval<br /><br /> Cheers,<br /> ~p<br /><br
/>On Wed, 2007-03-07 at 12:57 +0100, M.P.Dankoor wrote:<br /><blockquote type="CITE"> Hello,<br /><br /> Is it possible
toredesign your table as follows:<br /><br /> create table Mod48_00_2007 (<br /> ID            text,<br /> N_GEN      
 serial     not null,<br /> FORMSTORE     text,<br /> COD_NOTATIO   text,<br /> PA_COGNOME    text,<br /> constraint
pk_Mod48_00_2007primary key (N_GEN)<br /> );<br /><br /> Your insert simply becomes:<br /> INSERT INTO MOD48_00_2007
(ID,FORMSTORE, COD_NOTAIO, PA_COGNOME) VALUES ('192168217200737122012', '', '00128', 'DE MARTINIS')<br /><br /> Do note
thatyou do not refer to the N_GEN column, it will use the next value, please refer to <a
href="http://www.postgresql.org/docs/8.1/interactive/datatype.html#DATATYPE-SERIAL">http://www.postgresql.org/docs/8.1/interactive/datatype.html#DATATYPE-SERIAL</a><br
/>for more information.<br /><br /> Mario<br /><br /> Shavonne Marietta Wijesinghe wrote: <br /><blockquote
type="CITE"><fontsize="2">Hello</font><br />   <br /> I have created a table <br /> CREATE TABLE MOD48_00_2007 ( ID
text,N_GEN int PRIMARY KEY, FORMSTORE text, COD_NOTAIO text, PA_COGNOME text); <br />   <br /> And i insert the rows
viaa form in ASP. When the form loads i have a functin that goes and gets the value of the field N_GEN adds 1 to it and
showsit to the user. <br /> The problem is when i have 2 users working at the same time. <br />   <br /> For example
thelast value in my field N_GEN is 2 <br /> When both the users A and B loads the form (ASP page) it sees N_GEN = 3 :)
<br/>   <br /> So they fill in the form and user A clicks on the button OK and the record has been inserted with N_GEN
=3. But when the user B clicks on the button the record is not inserted because it has the same key "3" <br />   <br />
INSERTINTO MOD48_00_2007 (ID, N_GEN, FORMSTORE, COD_NOTAIO, PA_COGNOME) VALUES ('192168217200737122012', '3', '',
'00128','DE MARTINIS') <br />   <br /> Is there any way i can do this automatically? i mean maybe i have to use
someotherproperty instead of "Primary Key" ?? <br />   <br /> Thanks <br />   <br /><font size="2">Shavonne
Wijesinghe</font><br/><br /></blockquote><br /></blockquote> 

pgsql-sql by date:

Previous
From: Richard Broersma Jr
Date:
Subject: Re: inheritance
Next
From: "Radhika Sambamurti"
Date:
Subject: View Vs. Table