This is a discussion on Re: Last ID Problem within the pgsql Novice forums, part of the PostgreSQL category; --> > mitch and all, i've developed a simple little script > in order to test the "last id" methodology ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| > mitch and all, i've developed a simple little script > in order to test the "last id" methodology mitch > suggested. > > it looks like this... php and adodb include > excluded > for brevity... > > ----- > $db = &ADONewConnection('postgres7'); > $db -> > Connect($db_string,$db_owner,$db_pw,$db_name); > $insert_id = $db->getone("select > nextval('public.customer_cust_id_seq')"); > > print 'The ID is ' . $insert_id; > ----- > > my sequence name is 'public.customer_cust_id_seq' > (found this in pgadmin3). > > the last id number in my table is 65. when i use > nextval(), i get a result of 66 for $insert_id - > which > is the value that i would want to then perform and > insert. > > however, when i use currval(), as recommended, i get > no result. i probably get an error, but i haven't > checked for that yet. > > is it OK to use nextval() to get the next id value > in > the sequence before doing an insert? how come > currval() doesn't work. > > thanks to all for any guidance here. mitch and all, i noticed that if i keep refreshing my page that the $insert_id keeps growing... 66 then 67 then 68 then 69. i guess this makes sense, after all, the "next value" is always and icnrement higher. this makes me a little nervous, though. i only want one value... the next id i should use to perform an insert. i'm hoping currval() does the trick, however, i'm getting no result. do i need to instruct the sequence to go to it last value before calling currval()? tia... __________________________________ Do you Yahoo!? Read only the mail you want - Yahoo! Mail SpamGuard. http://promotions.yahoo.com/new_mail ---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend |
| |||
| > mitch and all, i noticed that if i keep refreshing > my > page that the $insert_id keeps growing... 66 then > 67 > then 68 then 69. > > i guess this makes sense, after all, the "next > value" > is always and icnrement higher. this makes me a > little nervous, though. > > i only want one value... the next id i should use > to > perform an insert. > > i'm hoping currval() does the trick, however, i'm > getting no result. > > do i need to instruct the sequence to go to it last > value before calling currval()? > > tia... another point of interest. now that i've been refreshing my nextval() statement, my highest cust_id value is 65, but nextval() keeps incrementing from where it was before. reading through the manual, this is designed in behavior. it looks like i could reset the value using setval(). i also noticed that currval() returns the value of the last nextval(). this infers that i must call nextval() prior to being able to get currval(), yet nextval() was not included in the original suggestion. am i missing something here? tia... __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ---------------------------(end of broadcast)--------------------------- TIP 3: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to majordomo@postgresql.org so that your message can get through to the mailing list cleanly |
| |||
| > another point of interest. now that i've been > refreshing my nextval() statement, my highest > cust_id > value is 65, but nextval() keeps incrementing from > where it was before. reading through the manual, > this > is designed in behavior. > > it looks like i could reset the value using > setval(). > > i also noticed that currval() returns the value of > the > last nextval(). this infers that i must call > nextval() prior to being able to get currval(), yet > nextval() was not included in the original > suggestion. > > am i missing something here? > > tia... > actually, michael fuhr addressed this issue in this group on 1/31/05. not sure why it didn't register. so, i should use nextval() if i use mitch's methodology of calling the id first then performing the insert. giving that nextval increments on a refresh, is there anything i need to worry about? or is the worst case scenario a gap in unique ids? this shouldn't matter as far as i can tell - as long as the numbers are unique. tia... __________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250 ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org |
| ||||
| On Wed, Feb 09, 2005 at 08:53:18AM -0800, operationsengineer1@yahoo.com wrote: > so, i should use nextval() if i use mitch's > methodology of calling the id first then performing > the insert. Right. You can call nextval() first and then explicitly insert the value you obtained, or you can do the insert first and let the serial column's default expression call nextval() automatically, and you can then find out the value it used with a subsequent call to currval(). > giving that nextval increments on a refresh, is there > anything i need to worry about? nextval() increments the sequence each time it's called. If you're using the sequence values as keys, then that's what you need. Presumably you'd only call nextval() when you're going to insert a new record, so what's the concern? -- Michael Fuhr http://www.fuhr.org/~mfuhr/ ---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq |