![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
$sql = "SELECT nextval('companies_company_id_seq'::text)"; And then I do my insert with that value. It works fine but I don't understand why it always gives me a +1 value of what I'm expecting. For example when I have an empty table it gives me 2 as the first nextval instead of a 1, or if I have two rows in there already it gives me a 4. Is this by design or am I missing something? It's not a problem I just want to know what's going on. |

#3
| |||
| |||
|
|
Long time MySQL user, six weeks into Postgres here. I'm converting a PHP script that uses MySQL to use Postgres instead. To substitue the missing MySQL functionaility with mysql_insert_id() and auto_increment I am using code like this: $sql = "SELECT nextval('companies_company_id_seq'::text)"; And then I do my insert with that value. It works fine but I don't understand why it always gives me a +1 value of what I'm expecting. For example when I have an empty table it gives me 2 as the first nextval instead of a 1, or if I have two rows in there already it gives me a 4. Is this by design or am I missing something? It's not a problem I just want to know what's going on. |
#4
| |||
| |||
|
| You need to do something like |
|
On Tuesday 14 Sep 2004 12:29 am, Greg Donald wrote: $sql = "SELECT nextval('companies_company_id_seq'::text)"; And then I do my insert with that value. It works fine but I don't understand why it always gives me a +1 value of what I'm expecting. For example when I have an empty table it gives me 2 as the first nextval instead of a 1, or if I have two rows in there already it gives me a 4. Is this by design or am I missing something? It's not a problem I just want to know what's going on. I new to postgresql too. ![]() Even in MySQL, the auto_increment value for a field in the table is kept separately. I remember using phpmyadmin to change this value. This counter will keep increasing no matter whether you table is shrinking or increasing. Just guessing, that sequences in postgesql are kept as separate counters along with the table, and will only keep on increasing. Postgresql will never check how many records are there in the table while returning a vlue for this sequence. Again, I am just guessing that this si teh behaviour. It doesnt make sense for the databse to check all teh records to find out the value of a simple counter. - Sandip |
#5
| |||
| |||
|
|
Long time MySQL user, six weeks into Postgres here. I'm converting a PHP script that uses MySQL to use Postgres instead. To substitue the missing MySQL functionaility with mysql_insert_id() and auto_increment I am using code like this: $sql = "SELECT nextval('companies_company_id_seq'::text)"; And then I do my insert with that value. [ cut ] |
#6
| |||
| |||
|
|
Hi, You don't have to select nextval separately. Just do an INSERT without the id column (presuming it is declared as SERIAL when created) like: INSERT INTO companies (company_name, company_whatever ) VALUES ( , ); Auto increment is indeed in a separate table that stores the next number in the sequence, not the last one used. And yes, this will increase 'forever' no matter how many rows there are or have been in your table. If you want to reset or otherwise meddle with the sequence use: You need to do something like select setval('seq-name', (select max(col) + 1 from table)); BR, Aarni On Monday 13 September 2004 22:08, you wrote: On Tuesday 14 Sep 2004 12:29 am, Greg Donald wrote: $sql = "SELECT nextval('companies_company_id_seq'::text)"; And then I do my insert with that value. It works fine but I don't understand why it always gives me a +1 value of what I'm expecting. For example when I have an empty table it gives me 2 as the first nextval instead of a 1, or if I have two rows in there already it gives me a 4. Is this by design or am I missing something? It's not a problem I just want to know what's going on. I new to postgresql too. ![]() Even in MySQL, the auto_increment value for a field in the table is kept separately. I remember using phpmyadmin to change this value. This counter will keep increasing no matter whether you table is shrinking or increasing. Just guessing, that sequences in postgesql are kept as separate counters along with the table, and will only keep on increasing. Postgresql will never check how many records are there in the table while returning a vlue for this sequence. Again, I am just guessing that this si teh behaviour. It doesnt make sense for the databse to check all teh records to find out the value of a simple counter. - Sandip |
#7
| |||
| |||
|
|
Long time MySQL user, six weeks into Postgres here. I'm converting a PHP script that uses MySQL to use Postgres instead. To substitue the missing MySQL functionaility with mysql_insert_id() and auto_increment I am using code like this: $sql = "SELECT nextval('companies_company_id_seq'::text)"; And then I do my insert with that value. It works fine but I don't understand why it always gives me a +1 value of what I'm expecting. For example when I have an empty table it gives me 2 as the first nextval instead of a 1, or if I have two rows in there already it gives me a 4. Is this by design or am I missing something? It's not a problem I just want to know what's going on. TIA.. As others have mentioned, you are basically calling the nextval |
#8
| |||
| |||
|
|
I think you should put <nextval(....)> directly into the INSERT instruction, so that it advances only once per added record. Have a look at Documentation PostgreSQL 7.4, Ch 9.11, table 9.34. HTH |
#9
| |||
| |||
|
|
On Mon, 13 Sep 2004 21:54:11 +0200, Ennio-Sr <nasr.laili (AT) tin (DOT) it> wrote: I think you should put <nextval(....)> directly into the INSERT instruction, so that it advances only once per added record. Have a look at Documentation PostgreSQL 7.4, Ch 9.11, table 9.34. HTH I would but then I don't know how to ensure I return that exact same id from the insert, like mysql_insert_id() does. That's the whole reason why I aquire the id first, so I know what it is after the insert. SELECT max(id) would be off under load I think. I just didn't understand why it appeared to be +2 and not +1 as a novice might expect. But now I get it. Thanks to all. After your insert statement, you can call CURRVAL to find out what the |
#10
| |||
| |||
|
|
On Mon, 13 Sep 2004 21:54:11 +0200, Ennio-Sr <nasr.laili (AT) tin (DOT) it> wrote: I think you should put <nextval(....)> directly into the INSERT instruction, so that it advances only once per added record. Have a look at Documentation PostgreSQL 7.4, Ch 9.11, table 9.34. HTH I would but then I don't know how to ensure I return that exact same id from the insert, like mysql_insert_id() does. That's the whole reason why I aquire the id first, so I know what it is after the insert. SELECT max(id) would be off under load I think. |
![]() |
| Thread Tools | |
| Display Modes | |
| |