pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Kretschmer <akretschmer@spamfence.net>
To: pgsql-sql@postgresql.org
Subject: Re: INSERT INTO relational tables
Date: Fri, 7 Dec 2007 16:16:42 +0100
Message-ID: <20071207151642.GA18766@KanotixBox> (raw)
In-Reply-To: <20071207132226.281710@gmx.net>
References: <20071207132226.281710@gmx.net>

Stefan Scheidegger <Stefan.Scheidegger@gmx.net> schrieb:
> An example to explain my Problem: Lets say I have a table containing
> information about the customer (name, address, ...) and about his
> order (pieces, product-name, price). Because one customer can order
> several products I split the table into two relational tables to
> prevent redundancy:
> 
> tbl_customer (cust_id, cust_name, cust_address) and tbl_order
> (ord_pieces, ord_productname, ord_price, ord_customer REFERENCES
> tbl_customer(cust_id))
> 
> Now I want to insert several new customers with several orders each at
> once. If I had all information in one table, this would be easy with
> something like:
> 
> INSERT INTO tbl_customerorders (name, address, pieces, porductname,
> price) VALUES ('MR. X', '1st street', 3, 't-shirts', 30), ('MR. X',
> '1st street', 5, 'books', 50),  ('MRS. Y', '2nd street', 1, 't-shirt',
> 10),...
> 
> But how can I do this in one query if I split the table? I can add one
> new customer, get his ID with curval() and then add his orders. But
> this won???t work if I want to add several customers at once.

There are any ways:
- use a special import table like your last example, insert your data in
  this table and use later regular SQL to fill the actual tables.
- create a own function with parameters like your last SQL. This
  function can do the job (check if customer exists, if no, create them,
  insert the actual order)
- You can create a special VIEW with RULEs on INSERT and this
  RULEs can do the job.
- Maybe there are other solutions...


> data as if it was stored in only one table. But is there in
> posgres/sql an abstraction-layer that allows me to insert as if the
> information was stored in one table? (Something like a VIEW that
> provides INSERT, UPDATE, ??? and automatically inserts the referenced
> ID.)

You can create such an abstraction layer.


Andreas (from germany...)
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."    (unknow)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



view thread (7+ messages)  latest in thread

Message-ID: <20071207151642.GA18766@KanotixBox>
Permalink:  ../20071207151642.GA18766@KanotixBox/
Also on:    postgresql.org/message-id/20071207151642.GA18766@KanotixBox

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: akretschmer@spamfence.net
  Subject: Re: INSERT INTO relational tables
  In-Reply-To: <20071207151642.GA18766@KanotixBox>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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