pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Efficient Insert statement for one record into multiple tables?
13+ messages / 5 participants
[nested] [flat]

* Efficient Insert statement for one record into multiple tables?
@ 2006-12-18 09:53 Peter Neu <peter.neu@gmx.net>
  2006-12-18 11:28 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
  0 siblings, 1 reply; 13+ messages in thread

From: Peter Neu @ 2006-12-18 09:53 UTC (permalink / raw)
  To: pgsql-jdbc

Hello,

 

I have this task that I need to insert one user record into to tables A and
B. 

 

User table A contains primary key and main data. Table B contains extra data
for

statistics. Table A has one field Id which is a sequence so it gets a unique
value 

for each new insert. I would like to insert this value into the other table
B as well as 

foreign key. 

 

So I'd like to do something like this 

 

insert into A (user_name, value2 ) values ( "foo", "foo2") 

 

AND 

 

insert into B ( id_from_a , statistic_data) values ( 23, "bla")

 

How is this done the best way? I would like to do this in one call. 

 

Sorry, for his dumb question but I'm not very experienced in Postgresql. :o(

 

 

Cheers,

 

Pete

 

 

 

 

 

 

 

 

^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 09:53 Efficient Insert statement for one record into multiple tables? Peter Neu <peter.neu@gmx.net>
@ 2006-12-18 11:28 ` Roland Walter <roland.walter.rwa@gmx.net>
  2006-12-18 12:05   ` Re: Efficient Insert statement for one record into multiple Peter Neu <peter.neu@gmx.net>
  0 siblings, 1 reply; 13+ messages in thread

From: Roland Walter @ 2006-12-18 11:28 UTC (permalink / raw)
  To: Peter Neu <peter.neu@gmx.net>; +Cc: pgsql-jdbc

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peter Neu wrote:
> Hello,
> 
> I have this task that I need to insert one user record into to tables A
> and B.
> 
> User table A contains primary key and main data. Table B contains extra
> data for
> 
> statistics. Table A has one field Id which is a sequence so it gets a
> unique value
> 
> for each new insert. I would like to insert this value into the other
> table B as well as
> 
> foreign key.
> 
> So I’d like to do something like this
> 
> insert into A (user_name, value2 ) values ( “foo”, “foo2”)
> 
> AND
> 
> insert into B ( id_from_a , statistic_data) values ( 23, “bla”)
> 
> How is this done the best way? I would like to do this in one call.
> 
>  
> 
> Sorry, for his dumb question but I’m not very experienced in Postgresql. :o(
> 

If you know the name of the sequence for the primary key in table A, say
"primary_A", then use the following to insert into table B direct after
you inserted into table A in the same connection:

insert into B (id_from_a, statistic_data) values( currval("primary_A"),
bla")

This works only if your insertion into table A called in some form
nextval("primary_A"), may it be as default value or in the
insertion-statement itself.

Regards,
Roland.
- --
Dipl.-Phys. Roland Walter
mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFFhntJxoOEAkary1ERAljXAJ9DASPP58I74Cj6Nt59rDzaIGlHjQCdFZRr
lFQGZaGY24/Sj77M6Le9xbc=
=Yr/X
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 09:53 Efficient Insert statement for one record into multiple tables? Peter Neu <peter.neu@gmx.net>
  2006-12-18 11:28 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
@ 2006-12-18 12:05   ` Peter Neu <peter.neu@gmx.net>
  2006-12-18 12:29     ` Re: Efficient Insert statement for one record into multiple Dave Cramer <pg@fastcrypt.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Peter Neu @ 2006-12-18 12:05 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

in my create table statement I got this line:

id int4 DEFAULT nextval('public.user_auth_id2_seq'::text)

So according to your suggestions this would be

insert into B (id_from_a, statistic_data) values(
currval("public.user_auth_id2_seq"),bla")

,right?

If I got you right I cannot link this two insert statements together. 
So I need two times to open a prepared statement and execute it. 
Would I need to do this in a transaction or is it safe to do this
without it?

Cheers,
Pete










^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 09:53 Efficient Insert statement for one record into multiple tables? Peter Neu <peter.neu@gmx.net>
  2006-12-18 11:28 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
  2006-12-18 12:05   ` Re: Efficient Insert statement for one record into multiple Peter Neu <peter.neu@gmx.net>
@ 2006-12-18 12:29     ` Dave Cramer <pg@fastcrypt.com>
  0 siblings, 0 replies; 13+ messages in thread

From: Dave Cramer @ 2006-12-18 12:29 UTC (permalink / raw)
  To: Peter Neu <peter.neu@gmx.net>; +Cc: pgsql-jdbc


On 18-Dec-06, at 7:05 AM, Peter Neu wrote:

> Hi,
>
> in my create table statement I got this line:
>
> id int4 DEFAULT nextval('public.user_auth_id2_seq'::text)
>
> So according to your suggestions this would be
>
> insert into B (id_from_a, statistic_data) values(
> currval("public.user_auth_id2_seq"),bla")
>
> ,right?
>
> If I got you right I cannot link this two insert statements together.
> So I need two times to open a prepared statement and execute it.
> Would I need to do this in a transaction or is it safe to do this
> without it?
It's safe to do it without a transaction, what you have to make sure  
is that you do it with the same connection object.

Dave
>
> Cheers,
> Pete
>
>
>
>
>
>
>
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 1: 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
>




^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
@ 2006-12-18 14:30 Roland Walter <roland.walter.rwa@gmx.net>
  0 siblings, 0 replies; 13+ messages in thread

From: Roland Walter @ 2006-12-18 14:30 UTC (permalink / raw)
  To: Peter Neu <peter.neu@gmx.net>; +Cc: pgsql-jdbc

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peter Neu wrote:
> Hi,
> 
> in my create table statement I got this line:
> 
> id int4 DEFAULT nextval('public.user_auth_id2_seq'::text)
> 
> So according to your suggestions this would be
> 
> insert into B (id_from_a, statistic_data) values(
> currval("public.user_auth_id2_seq"),bla")
> 
> ,right?
> 

Yes. (But beware, I forgot the double-qoute in front of bla.)

> If I got you right I cannot link this two insert statements together. 
> So I need two times to open a prepared statement and execute it. 
> Would I need to do this in a transaction or is it safe to do this
> without it?

As Dave Cramer wrote already, you must use the same connection object.
If you put in a transaction depends of your application. If you do many
insertions in table A and B at the same time, you should put all
insertions in one transaction to avoid performance issues.

Regards,
Roland.

- --
Dipl.-Phys. Roland Walter
mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFFhqX0xoOEAkary1ERAoVPAJ4krT4+7k8YnzX3cRgmqTd9bMllawCfVJuo
KIHe0I0jK0lgwcuhJvy4ztc=
=T1t5
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
@ 2006-12-18 14:36 Albe Laurenz <all@adv.magwien.gv.at>
  2006-12-18 14:50 ` Re: Efficient Insert statement for one record into multiple Dave Cramer <pg@fastcrypt.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Albe Laurenz @ 2006-12-18 14:36 UTC (permalink / raw)
  To: Roland Walter *EXTERN* <roland.walter.rwa@gmx.net>; Peter Neu <peter.neu@gmx.net>; +Cc: pgsql-jdbc

>> I have this task that I need to insert one user record into to tables
A
>> and B.
>> 
>> So I'd like to do something like this
>> 
>> insert into A (user_name, value2 ) values ( "foo", "foo2")
>> 
>> AND
>> 
>> insert into B ( id_from_a , statistic_data) values ( 23, "bla")
>> 
>> How is this done the best way? I would like to do this in one call.
> 
> If you know the name of the sequence for the primary key in table A,
say
> "primary_A", then use the following to insert into table B direct
after
> you inserted into table A in the same connection:
> 
> insert into B (id_from_a, statistic_data) values(
currval("primary_A"),
> bla")
> 
> This works only if your insertion into table A called in some form
> nextval("primary_A"), may it be as default value or in the
> insertion-statement itself.

This is _bad_.

What if somebody did a nextval("primary_A") between the insert into A
and the insert into B?

If there is a sequence "primary_A" for table A, you should proceed
as follows:

- select nextval("primary_A")
- then insert into A with this primary key
- then insert into B with the same key

That should all be in a single transaction!

Yours,
Laurenz Albe



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 14:36 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
@ 2006-12-18 14:50 ` Dave Cramer <pg@fastcrypt.com>
  0 siblings, 0 replies; 13+ messages in thread

From: Dave Cramer @ 2006-12-18 14:50 UTC (permalink / raw)
  To: Albe Laurenz <all@adv.magwien.gv.at>; +Cc: Roland Walter *EXTERN* <roland.walter.rwa@gmx.net>; Peter Neu <peter.neu@gmx.net>; pgsql-jdbc


On 18-Dec-06, at 9:36 AM, Albe Laurenz wrote:

>>> I have this task that I need to insert one user record into to  
>>> tables
> A
>>> and B.
>>>
>>> So I'd like to do something like this
>>>
>>> insert into A (user_name, value2 ) values ( "foo", "foo2")
>>>
>>> AND
>>>
>>> insert into B ( id_from_a , statistic_data) values ( 23, "bla")
>>>
>>> How is this done the best way? I would like to do this in one call.
>>
>> If you know the name of the sequence for the primary key in table A,
> say
>> "primary_A", then use the following to insert into table B direct
> after
>> you inserted into table A in the same connection:
>>
>> insert into B (id_from_a, statistic_data) values(
> currval("primary_A"),
>> bla")
>>
>> This works only if your insertion into table A called in some form
>> nextval("primary_A"), may it be as default value or in the
>> insertion-statement itself.
>
> This is _bad_.
No, this works
>
> What if somebody did a nextval("primary_A") between the insert into A
> and the insert into B?
as long as you use currval then it works as advertised
>
> If there is a sequence "primary_A" for table A, you should proceed
> as follows:
This requires two trips to the db. Roland's solution doesn't, and it  
does work.
>
> - select nextval("primary_A")
> - then insert into A with this primary key
> - then insert into B with the same key
>
> That should all be in a single transaction!
This doesn't even have to be in a single transaction.

Dave
>
> Yours,
> Laurenz Albe
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
>                http://archives.postgresql.org
>




^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
@ 2006-12-18 15:06 Albe Laurenz <all@adv.magwien.gv.at>
  0 siblings, 0 replies; 13+ messages in thread

From: Albe Laurenz @ 2006-12-18 15:06 UTC (permalink / raw)
  To: Dave Cramer *EXTERN* <pg@fastcrypt.com>; +Cc: Roland Walter *EXTERN* <roland.walter.rwa@gmx.net>; Peter Neu <peter.neu@gmx.net>; pgsql-jdbc

>> This is _bad_.
> No, this works

Oops, indeed.

>> What if somebody did a nextval("primary_A") between the insert into A
>> and the insert into B?
> as long as you use currval then it works as advertised

Sorry, I didn't realize that currval returns the last value retrieved
_in this session_.

YOurs,
Laurenz Albe



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
@ 2006-12-18 15:36 Albe Laurenz <all@adv.magwien.gv.at>
  2006-12-18 16:22 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
  0 siblings, 1 reply; 13+ messages in thread

From: Albe Laurenz @ 2006-12-18 15:36 UTC (permalink / raw)
  To: Dave Cramer *EXTERN* <pg@fastcrypt.com>; Peter Neu <peter.neu@gmx.net>; +Cc: pgsql-jdbc

>> If I got you right I cannot link this two insert statements together.
>> So I need two times to open a prepared statement and execute it.
>> Would I need to do this in a transaction or is it safe to do this
>> without it?
>
> It's safe to do it without a transaction, what you have to make sure  
> is that you do it with the same connection object.

Safe as far as currval() is concerned, yes, but then the second
INSERT could fail while the first INSERT is committed, right?

Yours,
Laurenz Albe



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 15:36 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
@ 2006-12-18 16:22 ` Roland Walter <roland.walter.rwa@gmx.net>
  2006-12-19 10:18   ` Re: Efficient Insert statement for one record into multiple Peter Neu <peter.neu@gmx.net>
  0 siblings, 1 reply; 13+ messages in thread

From: Roland Walter @ 2006-12-18 16:22 UTC (permalink / raw)
  To: Albe Laurenz <all@adv.magwien.gv.at>; +Cc: Dave Cramer *EXTERN* <pg@fastcrypt.com>; Peter Neu <peter.neu@gmx.net>; pgsql-jdbc

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Albe Laurenz wrote:
>>> If I got you right I cannot link this two insert statements together.
>>> So I need two times to open a prepared statement and execute it.
>>> Would I need to do this in a transaction or is it safe to do this
>>> without it?
>> It's safe to do it without a transaction, what you have to make sure  
>> is that you do it with the same connection object.
> 
> Safe as far as currval() is concerned, yes, but then the second
> INSERT could fail while the first INSERT is committed, right?

Yes, but as I said, it depends of the application if you do the two
insertions in one transaction.

Regards,
Roland.

- --
Dipl.-Phys. Roland Walter
mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFFhsBMxoOEAkary1ERAlp2AJ97Sw/pKIPr7Lm4QPKBYg1wD4oQZwCdG/0r
xmvivi1ApSuI8viTpcKbIS0=
=A8ZT
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 15:36 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
  2006-12-18 16:22 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
@ 2006-12-19 10:18   ` Peter Neu <peter.neu@gmx.net>
  2006-12-19 11:03     ` Re: Efficient Insert statement for one record into multiple Markus Schaber <schabi@logix-tt.com>
  2006-12-19 11:09     ` Re: Efficient Insert statement for one record into multiple Dave Cramer <pg@fastcrypt.com>
  0 siblings, 2 replies; 13+ messages in thread

From: Peter Neu @ 2006-12-19 10:18 UTC (permalink / raw)
  To: pgsql-jdbc; +Cc: 'Roland Walter' <roland.walter.rwa@gmx.net>

Hello,

the insertion within the same connection objects works just fine. ;o)
One thing which still keeps me thinking is how should I handle duplicate
key exceptions when it happens in the first insert? Should I just close
the connection object in the catch clause when this happens in the first
statement so the second statement hits an AlreadyClosedConnectionException?

Cheers,
Pete 





^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 15:36 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
  2006-12-18 16:22 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
  2006-12-19 10:18   ` Re: Efficient Insert statement for one record into multiple Peter Neu <peter.neu@gmx.net>
@ 2006-12-19 11:03     ` Markus Schaber <schabi@logix-tt.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Markus Schaber @ 2006-12-19 11:03 UTC (permalink / raw)
  To: pgsql-jdbc

Hi, Peter,

"Peter Neu" <peter.neu@gmx.net> wrote:

> the insertion within the same connection objects works just fine. ;o)
> One thing which still keeps me thinking is how should I handle duplicate
> key exceptions when it happens in the first insert? Should I just close
> the connection object in the catch clause when this happens in the first
> statement so the second statement hits an AlreadyClosedConnectionException?

Why execute the second statement at all?

Maybe you should rework the control flow of your application.

Regards,
Markus

-- 
Markus Schaber | Logical Tracking&Tracing International AG
Dipl. Inf.     | Software Development GIS

Fight against software patents in Europe! www.ffii.org
www.nosoftwarepatents.org



^ permalink  raw  reply  [nested|flat] 13+ messages in thread

* Re: Efficient Insert statement for one record into multiple
  2006-12-18 15:36 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
  2006-12-18 16:22 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
  2006-12-19 10:18   ` Re: Efficient Insert statement for one record into multiple Peter Neu <peter.neu@gmx.net>
@ 2006-12-19 11:09     ` Dave Cramer <pg@fastcrypt.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Dave Cramer @ 2006-12-19 11:09 UTC (permalink / raw)
  To: Peter Neu <peter.neu@gmx.net>; +Cc: pgsql-jdbc; 'Roland Walter' <roland.walter.rwa@gmx.net>

If you create the key using nextval you will never get duplicate key  
exceptions.

However, you should never close connections in the catch clause.  
Always close connections in a finally clause.

Dave
On 19-Dec-06, at 5:18 AM, Peter Neu wrote:

> Hello,
>
> the insertion within the same connection objects works just fine. ;o)
> One thing which still keeps me thinking is how should I handle  
> duplicate
> key exceptions when it happens in the first insert? Should I just  
> close
> the connection object in the catch clause when this happens in the  
> first
> statement so the second statement hits an  
> AlreadyClosedConnectionException?
>
> Cheers,
> Pete
>
>
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 1: 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
>




^ permalink  raw  reply  [nested|flat] 13+ messages in thread


end of thread, other threads:[~2006-12-19 11:09 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-12-18 09:53 Efficient Insert statement for one record into multiple tables? Peter Neu <peter.neu@gmx.net>
2006-12-18 11:28 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
2006-12-18 12:05   ` Re: Efficient Insert statement for one record into multiple Peter Neu <peter.neu@gmx.net>
2006-12-18 12:29     ` Re: Efficient Insert statement for one record into multiple Dave Cramer <pg@fastcrypt.com>
2006-12-18 14:30 Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
2006-12-18 14:36 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
2006-12-18 14:50 ` Re: Efficient Insert statement for one record into multiple Dave Cramer <pg@fastcrypt.com>
2006-12-18 15:06 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
2006-12-18 15:36 Re: Efficient Insert statement for one record into multiple Albe Laurenz <all@adv.magwien.gv.at>
2006-12-18 16:22 ` Re: Efficient Insert statement for one record into multiple Roland Walter <roland.walter.rwa@gmx.net>
2006-12-19 10:18   ` Re: Efficient Insert statement for one record into multiple Peter Neu <peter.neu@gmx.net>
2006-12-19 11:03     ` Re: Efficient Insert statement for one record into multiple Markus Schaber <schabi@logix-tt.com>
2006-12-19 11:09     ` Re: Efficient Insert statement for one record into multiple Dave Cramer <pg@fastcrypt.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox