Re: [PERL DBI] Insertiing data across multiple tables

From: SCassidy(at)overlandstorage(dot)com
To: "JD Wong" <asi4man(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, pgsql-general-owner(at)postgresql(dot)org
Subject: Re: [PERL DBI] Insertiing data across multiple tables
Date: 2008-07-24 21:17:18
Message-ID: OF068CB871.762745DE-ON88257490.00740E82-88257490.0074F4D9@overlandstorage.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

pgsql-general-owner(at)postgresql(dot)org wrote on 07/24/2008 01:36:27 PM:

> Does anybody know how to insert data over multiple tables
> transactionally? The relationship is 1:1 with the latter table
> having a foreign key constraint. In order to add data to Table2 I
> need to know the primary key value of the same record in Table1
> before it's committed, which in this case is a serial integer which
> of course is regulated by an implicit index. Does anybody know how
> to access a column's index in the Perl DBI?
> -thanks

Table2 needs to have the foreign key constraint defined as INITIALLY
DEFERRED.

First, turn AutoCommit off (starts a transaction).
Then, get the nextval of the sequence used for the foreign key.
Insert master table row, using the key value.
Insert second table row, again using the key value.
Commit
Set AutoCommit back on, if the execution will continue, and you will need
it again.

I sometimes set up primary key columns like: "id integer PRIMARY KEY
DEFAULT nextval('tblname_seq'),
that way I have a sequence name with a meaningful name, easy to reference.

Susan

----------------------------------------------------
Tiered Data Protection Made Simple
http://www.overlandstorage.com/
----------------------------------------------------

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Kirk Strauser 2008-07-24 23:38:12 PgDBF, Another XBase to PostgreSQL converter
Previous Message JD Wong 2008-07-24 20:36:27 [PERL DBI] Insertiing data across multiple tables