From: Bruno T. <bt...@as...> - 2003-10-31 17:38:37
|
Hi Sidnei > I was needing a way to figure out the next id that will be generated > by an insert using Postgres, and stumbled over queryInsertID. From > what I can see, that one gives the last inserted id. Now the question > is: There is a easy way to get the *next id*? It seems i need to do a > CREATE SEQUENCE if the table was just created/doesnt exist yet, but Im > not the Postgres guru here, so I'm not sure. Postgres has a special type called 'serial'. If you create a column with this type set, lets say customer_id, postgres will create a sequence called customer_id_seq and will set a DEFAULT constraint with nextval('customer_id_seq') to that column. When you do an INSERT and you don't pass in value for that field, DEFAULT will be used, thus incrementing the sequence. If you don't want to go like this, you can create that field as an integer and manually create a sequence. Then, you can "SELECT nextval('sequence')" to get the next id and use it to create a new record. []'s Bruno Trevisan bt...@as... |=3D| Async Open Source |=3D| D. Alexandrina, 253= 4 http://www.async.com.br/ |=3D| +55 16 261-2331 |=3D| 13566-290 |=3D| +55 16 9781-8717 |=3D| S=E3o Carlos, SP, B= rasil |