![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi guys, I am in troubles with a SERIAL field. I have five tables. A parent table and four child tables. When I do the INSERT in the parent table, I have an ID (generated) by the sequence (SERIAL field), and I have to use this ID to reference all child tables. Well, once I do an INSERT in the parent table, how can I know (for sure) which number id was generated by the sequence? Simple example: ------------------------------------------------------ CREATE TABLE parent(id SERIAL, descrip CHAR(50)); ------------------------------------------------------ So, ------------------------------------------------------ INSERT INTO parent (descrip) VALUES ('project 1'); ------------------------------------------------------ How can I now (for sure) with value was generated by the sequence to fill the field ID? (There is lots of users using the software at the same time, so I am not able to use the last_value() function on the sequence.) Best Regards, Marcelo Pereira Brazil __________________________________________________ _____ Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discadoragora! http://br.acesso.yahoo.com/ ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster -----BEGIN PGP SIGNATURE----- |
#3
| |||
| |||
|
|
Well, once I do an INSERT in the parent table, how can I know (for sure) which number id was generated by the sequence? |
#4
| |||
| |||
|
|
Hi guys, I am in troubles with a SERIAL field. I have five tables. A parent table and four child tables. When I do the INSERT in the parent table, I have an ID (generated) by the sequence (SERIAL field), and I have to use this ID to reference all child tables. Well, once I do an INSERT in the parent table, how can I know (for sure) which number id was generated by the sequence? Simple example: ------------------------------------------------------ CREATE TABLE parent(id SERIAL, descrip CHAR(50)); ------------------------------------------------------ So, ------------------------------------------------------ INSERT INTO parent (descrip) VALUES ('project 1'); ------------------------------------------------------ How can I now (for sure) with value was generated by the sequence to fill the field ID? (There is lots of users using the software at the same time, so I am not able to use the last_value() function on the sequence.) Best Regards, Marcelo Pereira Brazil |
|
-- get_sequence(schema_name, table_name, column_name) CREATE OR REPLACE FUNCTION get_sequence (text, text, text) RETURNS text AS ' SELECT seq.relname::text FROM pg_class src, pg_class seq, pg_namespace, pg_attribute, pg_depend WHERE pg_depend.refobjsubid = pg_attribute.attnum AND pg_depend.refobjid = src.oid AND seq.oid = pg_depend.objid AND src.relnamespace = pg_namespace.oid AND pg_attribute.attrelid = src.oid AND pg_namespace.nspname = $1 AND src.relname = $2 AND pg_attribute.attname = $3; ' language sql; |
#5
| |||
| |||
|
|
Hi guys, I am in troubles with a SERIAL field. I have five tables. A parent table and four child tables. When I do the INSERT in the parent table, I have an ID (generated) by the sequence (SERIAL field), and I have to use this ID to reference all child tables. Well, once I do an INSERT in the parent table, how can I know (for sure) which number id was generated by the sequence? Simple example: ------------------------------------------------------ CREATE TABLE parent(id SERIAL, descrip CHAR(50)); ------------------------------------------------------ So, ------------------------------------------------------ INSERT INTO parent (descrip) VALUES ('project 1'); ------------------------------------------------------ How can I now (for sure) with value was generated by the sequence to fill the field ID? (There is lots of users using the software at the same time, so I am not able to use the last_value() function on the sequence.) Best Regards, Marcelo Pereira Brazil __________________________________________________ _____ Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! http://br.acesso.yahoo.com/ ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster |
#6
| |||
| |||
|
|
How can I now (for sure) with value was generated by the sequence to fill the field ID? (There is lots of users using the software at the same time, so I am not able to use the last_value() function on the sequence.) |
#7
| |||
| |||
|
|
MaRCeLO PeReiRA wrote: How can I now (for sure) with value was generated by the sequence to fill the field ID? (There is lots of users using the software at the same time, so I am not able to use the last_value() function on the sequence.) Yes you are nextval()/currval() are multi-user safe. They return the next/current value *in the current connection*. -- Richard Huxton Archonet Ltd ---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend |
#8
| |||
| |||
|
|
I think the best way would be not to use a SERIAL field, but an INTEGER field and a sequence: |
|
CREATE SEQUENCE parent_seq; CREATE TABLE parent(id INTEGER, descrip CHAR(50)); So when you want to insert on the parent table, you obtain the next value from the sequence and then you insert in the parent and child tables the value you obtained: newId:=SELECT nextval('parent_seq') INSERT INTO parent(id, descrip) VALUES (newId, 'XXXX'); INSERT INTO child_1(..., ..., parentId) VALUES (..., ..., newId); INSERT INTO child_2(..., ..., parentId) VALUES (..., ..., newId); INSERT INTO child_3(..., ..., parentId) VALUES (..., ..., newId); |
|
hope it helps. MaRCeLO PeReiRA wrote: Hi guys, I am in troubles with a SERIAL field. I have five tables. A parent table and four child tables. When I do the INSERT in the parent table, I have an ID (generated) by the sequence (SERIAL field), and I have to use this ID to reference all child tables. Well, once I do an INSERT in the parent table, how can I know (for sure) which number id was generated by the sequence? Simple example: ------------------------------------------------------ CREATE TABLE parent(id SERIAL, descrip CHAR(50)); ------------------------------------------------------ So, ------------------------------------------------------ INSERT INTO parent (descrip) VALUES ('project 1'); ------------------------------------------------------ How can I now (for sure) with value was generated by the sequence to fill the field ID? (There is lots of users using the software at the same time, so I am not able to use the last_value() function on the sequence.) Best Regards, Marcelo Pereira Brazil __________________________________________________ _____ Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! http://br.acesso.yahoo.com/ ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org) |
#9
| |||
| |||
|
|
Which means that sometimes they do not return the correct value - if you have a trigger that inserts another record you will not get the right value. |
|
MSSQL has @@IDENTITY and SCOPE_IDENTITY() to handle this case, I'm new to pgsql so I don't know if it has anything like that. Jerry "Richard Huxton" <dev (AT) archonet (DOT) com> wrote in message news:4193B30B.40905 (AT) archonet (DOT) com... MaRCeLO PeReiRA wrote: How can I now (for sure) with value was generated by the sequence to fill the field ID? (There is lots of users using the software at the same time, so I am not able to use the last_value() function on the sequence.) Yes you are nextval()/currval() are multi-user safe. They return the next/current value *in the current connection*. -- Richard Huxton Archonet Ltd ---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster |
#10
| |||
| |||
|
|
--- Jerry III <jerryiii (AT) hotmail (DOT) com> wrote: Which means that sometimes they do not return the correct value - if you have a trigger that inserts another record you will not get the right value. If you are new to PostgreSQL, as you say, then why are you so sure of this? Perhaps you may profit from looking a little more at how currval() works. |
![]() |
| Thread Tools | |
| Display Modes | |
| |