Thread: Question on Multi-Table Inserts

Question on Multi-Table Inserts

From
Don Parris
Date:
Hi all,

This is more of a strategic question.  If I want to enter a particular financial transaction in my DB, I need to use the following process:

Insert a new or select an existing payee
Insert a new transaction record (using the payee_id from the payee table)
Insert at least one, but usually more line items (using the transrec_id from  the transrec table)
Insert a new or select an existing category for the transaction line item (using the cat_id) from the category table.

Is it as simple as running a series of insert statements in succession, passing on the PK of the first table to the next table in the process?  I.e.,

BEGIN
INSERT into table payee(values...)
INSERT into table transrec(values...) # pass in payee_id variable
SELECT *from category (to get cat_id)
INSERT into table transdetails(values...) #pass in transrec_id and cat_id variables
COMMIT

Or is it more complicated than that?

Thanks!
Don
--
D.C. Parris, FMP, Linux+, ESL Certificate
Minister, Security/FM Coordinator, Free Software Advocate
GPG Key ID: F5E179BE

Re: Question on Multi-Table Inserts

From
Joe Abbate
Date:
Hello Don,

On 16/04/13 05:59, Don Parris wrote:
> Is it as simple as running a series of insert statements in succession,
> passing on the PK of the first table to the next table in the process?
> I.e.,
>
> BEGIN
> INSERT into table payee(values...)
> INSERT into table transrec(values...) # pass in payee_id variable
> SELECT *from category (to get cat_id)
> INSERT into table transdetails(values...) #pass in transrec_id and
> cat_id variables
> COMMIT
>
> Or is it more complicated than that?

From a business standpoint, and without any further implementation
details, I'd say the first INSERT and the SELECT aren't part of the
logical unit of work, so the database transaction would normally
parallel the financial transaction, i.e., just insert into transrec and
trandetails.

As you said, the payee insert depends on checking for existence, so it
would be conditional.  Similarly, the cat_id has to exist and be found
first.  If this was a typical interactive application, the SELECTs for
payee and category would be done first (before the BEGIN), and the
latter could be done just once with the results cached in the front-end
(shown to the user as a drop-down selection).

Regards,

Joe


Re: Question on Multi-Table Inserts

From
Federico Di Gregorio
Date:
On 16/04/2013 11:59, Don Parris wrote:
[snip]
> Is it as simple as running a series of insert statements in succession,
> passing on the PK of the first table to the next table in the process?
> I.e.,
>
> BEGIN
> INSERT into table payee(values...)
> INSERT into table transrec(values...) # pass in payee_id variable
> SELECT *from category (to get cat_id)
> INSERT into table transdetails(values...) #pass in transrec_id and
> cat_id variables
> COMMIT
>
> Or is it more complicated than that?

If your only data store is the database then using a transaction is the
right thing to do. Being all-or-nothing you get exactly what you want.

federico



Re: Question on Multi-Table Inserts

From
Don Parris
Date:
Thanks all,

Sorry - I got busy the last few days.


On Tue, Apr 16, 2013 at 8:54 AM, Federico Di Gregorio <fog@dndg.it> wrote:
On 16/04/2013 11:59, Don Parris wrote:
[snip]
> Is it as simple as running a series of insert statements in succession,
> passing on the PK of the first table to the next table in the process?
> I.e.,
>
> BEGIN
> INSERT into table payee(values...)
> INSERT into table transrec(values...) # pass in payee_id variable
> SELECT *from category (to get cat_id)
> INSERT into table transdetails(values...) #pass in transrec_id and
> cat_id variables
> COMMIT
>
> Or is it more complicated than that?

If your only data store is the database then using a transaction is the
right thing to do. Being all-or-nothing you get exactly what you want.

federico




--
D.C. Parris, FMP, Linux+, ESL Certificate
Minister, Security/FM Coordinator, Free Software Advocate
GPG Key ID: F5E179BE