Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 1D6C02E02AB for ; Mon, 10 Dec 2007 07:08:57 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 84840-03 for ; Mon, 10 Dec 2007 07:08:46 -0400 (AST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.179]) by postgresql.org (Postfix) with ESMTP id 567E22E0243 for ; Mon, 10 Dec 2007 07:08:52 -0400 (AST) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrelayeu5) with ESMTP (Nemesis) id 0ML25U-1J1gVF211E-0004II; Mon, 10 Dec 2007 12:08:50 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1J1gVE-0002Zz-00 for pgsql-sql@postgresql.org; Mon, 10 Dec 2007 12:08:48 +0100 Date: Mon, 10 Dec 2007 12:08:48 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: INSERT INTO relational tables Message-ID: <20071210110848.GE917@a-kretschmer.de> References: <20071207172528.820B92C96D4@maia-2.hub.org> <20071209075032.2A1112E3294@postgresql.org> <20071210104204.324340@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20071210104204.324340@gmx.net> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1+KgOvzo8//IHqzlQVKIp5HOBGcCG03Le7xQdI y7x8ZHh8xpr2VrUzjiCH+jTC2m7OuVPoFyIkX6WoqxhrtggGDR V/84yUBp01WvMgdHrYM24/nkx8YJxzX X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/36 X-Sequence-Number: 29728 am Mon, dem 10.12.2007, um 11:42:04 +0100 mailte Stefan Scheidegger folgendes: > Thanks for your hints so far. > > I'm looking for both syntax simplicity and referential integrity. I'm > using c++ with libpqxx, but I didn't find a good object-relational > mapper. And I'm too lazy to implement a whole persistency layer as I > don't have a huge DB with many relations. > > I tried Andreas' suggestion with a RULE for INSERT on a VIEW. I > created the VIEW composing my table with customers and orders: > > CREATE VIEW vw_customer_insert AS SELECT * FROM tbl_customer, > tbl_order WHERE cust_id = ord_customer; > > Then I added a rule for the inserting: CREATE RULE rl_customer_insert > AS ON INSERT TO vw_customer_insert DO INSTEAD ( INSERT INTO > tbl_customer VALUES (DEFAULT, new.cust_name, new.cust_address); INSERT > INTO tbl_order VALUES (NEW.ord_pieces, NEW.ord_productname, > NEW.ord_price, currval('"tbl_cusomer_cust_id_seq"'));); > > But this results in the same problem: It works fine if I insert just > one new customer with one new order. But if I want to insert several > new entries: Insert the new customer only into the table if this customer doesn't exist there. If the customer exist in the table, obtain the id for this customer. simplified: - detect, if the customer exists yes: obtain the id as id no: insert and use currval() as id - insert the order with the id I would write a function for this. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net