Thread: [Re: PRIMARY KEY]

[Re: PRIMARY KEY]

From
Phillip Smith
Date:
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> 

Re: [Re: PRIMARY KEY]

From
Bruno Wolff III
Date:
On Wed, Mar 07, 2007 at 23:20:12 +1100, Phillip Smith <fukawi2@iinet.net.au> wrote:
> If you actually need to know the value of N_GEN in your ASP application,
> you will need to query the database first and select the 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.

In 8.2, you can also use the RETURNING clause to get those values.


Re: [Re: PRIMARY KEY]

From
Phillip Smith
Date:
Of course - my bad... That's the main reason I upgraded to 8.2! <br /><br /> On Fri, 2007-03-09 at 09:03 -0600, Bruno
WolffIII wrote: <blockquote type="CITE"><pre>
 
<font color="#000000">On Wed, Mar 07, 2007 at 23:20:12 +1100,</font>
<font color="#000000">  Phillip Smith <<a href="mailto:fukawi2@iinet.net.au">fukawi2@iinet.net.au</a>>
wrote:</font>
<font color="#000000">> If you actually need to know the value of N_GEN in your ASP application,</font>
<font color="#000000">> you will need to query the database first and select the NEXTVAL from</font>
<font color="#000000">> the sequence that the "serial" data type will create, then use that</font>
<font color="#000000">> returned value in your insert - ie, DON'T exclude it from the insert,</font>
<font color="#000000">> otherwise it will default to NEXTVAL again and return a different value.</font>

<font color="#000000">In 8.2, you can also use the RETURNING clause to get those values.</font>
</pre></blockquote><br /><p><b>*******************Confidentiality and Privilege Notice*******************</b><p> The
materialcontained in this message is privileged and confidential to the addressee. If you are not the addressee
indicatedin this message or responsible for delivery of the message to such person, you may not copy or deliver this
messageto anyone, and you should destroy it and kindly notify the sender by reply email. <p> Information in this
messagethat does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by
Weatherbeeta.Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or
consequentialloss arising from transmission of this message or any attachments <br />