Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 042652E320C for ; Fri, 7 Dec 2007 11:15:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 96198-05 for ; Fri, 7 Dec 2007 11:15:39 -0400 (AST) Received: from 137086.vserver.de (vs137086.vserver.de [62.75.137.86]) by postgresql.org (Postfix) with ESMTP id 124A32E31FC for ; Fri, 7 Dec 2007 11:15:38 -0400 (AST) Received: from a-kretschmer.de (p54B3B2D8.dip0.t-ipconnect.de [84.179.178.216]) (authenticated bits=0) by 137086.vserver.de (8.12.8/8.12.8) with ESMTP id lB7FFZ75013872 for ; Fri, 7 Dec 2007 16:15:36 +0100 Received: from kretschmer by a-kretschmer.de with local (Exim 4.61) (envelope-from ) id 1J0ewU-0005KB-M3 for pgsql-sql@postgresql.org; Fri, 07 Dec 2007 16:16:42 +0100 Date: Fri, 7 Dec 2007 16:16:42 +0100 From: Andreas Kretschmer To: pgsql-sql@postgresql.org Subject: Re: INSERT INTO relational tables Message-ID: <20071207151642.GA18766@KanotixBox> References: <20071207132226.281710@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20071207132226.281710@gmx.net> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: mutt-ng 1.5.9i (Linux) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/24 X-Sequence-Number: 29716 Stefan Scheidegger 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°