Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSn3e-0005XG-Ee for pgsql-general@arkaria.postgresql.org; Mon, 20 May 2019 18:30:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hSn3c-0003Tz-SI for pgsql-general@arkaria.postgresql.org; Mon, 20 May 2019 18:30:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSn3c-0003Tk-Ew for pgsql-general@lists.postgresql.org; Mon, 20 May 2019 18:30:44 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSn3V-0000fW-Vu for pgsql-general@postgresql.org; Mon, 20 May 2019 18:30:43 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 4D9D524B7F; Mon, 20 May 2019 14:30:37 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 20 May 2019 14:30:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=C 3eErNDVG/Pi7WClBP4ZM/9pOuocjsHNq0oB2Jbd9iU=; b=Rp83Y4IkRHuUxFUO5 hHh7qDLx903P4qnb3Uyn/2V+EzOTOV8NT+bnqd9W886F/syxRbICxqU7/0s9ReI6 pmKPlvVShOFfOyzimfPJf5Am/0OLYexVtM/VARFHx9TwkOS9nd0X8l9+7VzCtMB+ UN/YZSFw7mAPi61k0jpHbiNAYn9a/irFDaGb+r5/of1xbrmvolAx++8zPQIej28q /SS8BjDOBVZ0crP5jSznXXTKkAomYAVGHx/LxalTPnuprwlHCMe5pLUXSWPzF0Vf iEnzsnRdqgTQ8rHCI8aWFGfRZ0jaPIaxQG1YX+tWwf1vIGZR3BIupbRsC6EoUN5Z eywHA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=C3eErNDVG/Pi7WClBP4ZM/9pOuocjsHNq0oB2Jbd9 iU=; b=ImIp4oHy0IpseKdtuKSGSlVTnA3ex6cbFKb8GxvbIvuhhBieAho2ryRRp +Px7piO/GDL0FipBuaKe1Hjh3yoBqys4jULfJLt+Hp6sKKpoNBGZzg9NAl6888MT IdOCavBQ32pn1IPIHN/NUD3B5CEUKoOdXJf8HtskJFus7CD/6OjhamkqfHVXmg6D AnSPU11yBrN2hcr8XrEiRz6qZ/IWftkvm+kZiA31ZTGGFzPlSfiWHHq+h8LVYiOm uZ3tcf5uuFIA94jAee/gNENF86Q2lBPJcuR9axX9zUm6F0DOsEfvJa7wfok+3SKW r3Z7yor7yhovHBT/5sNzfsWAL7tyA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddruddtkedguddvjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecukfhppeejfedrvdefledruddujedrvdefkeenucfrrghrrghmpehmrghilhhf rhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhush htvghrufhiiigvpedt X-ME-Proxy: Received: from ranger.suse (c-73-239-117-238.hsd1.wa.comcast.net [73.239.117.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 475008005B; Mon, 20 May 2019 14:30:36 -0400 (EDT) Subject: Re: INSERT where not exists with foreign key To: Chuck Martin , pgsql-general References: From: Adrian Klaver Message-ID: <7fb55314-d630-1306-9b00-cd5513062f4d@aklaver.com> Date: Mon, 20 May 2019 11:30:34 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/20/19 11:11 AM, Chuck Martin wrote: > My Google foo isn't working on this question, probably because I don't > understand the question well enough. I'm using Postgres 11.3 on Centos > 7. I'm trying to insert a record in table A with a foreign key to table > B, but only where there is not already a foreign key in A to B. So > assume this simple structure: > > Table A > A.key    Integer > A.something     text > A.Bkey     Integer [foreign key to table B, column B.key > > Table B (or View C) > B.key    Integer > [more columns] > > Thinking that it might simplify matters, I created a view to table B > that only includes records with no foreign key in Table A. But still, > after reading the documentation and Googling, I can't work out what > seems like it should be simple. Among the many things that didn't work is: > > INSERT INTO A(something,A.Bkey) > > VALUES ('text', >               (SELECT C.key FROM C) > > But this didn't work because the subquery returned more than one value. > Of course I want it to return all values, but just one per insert. > > I can do this outside of Postgres, but would like to learn how to do > this with SQL. A more concrete example: create table parent_tbl(id integer primary key, fld_1 varchar) create table child_tbl(child_id serial primary key, fk_id integer references parent_tbl, child_fld_1 varchar); insert into parent_tbl values (1, 'test'); insert into parent_tbl values (2, 'dog'); insert into child_tbl(fk_id, child_fld_1) values (1, 'cat'); insert into child_tbl(fk_id, child_fld_1) values (2, 'fish'); insert into child_tbl(fk_id, child_fld_1) select id, 'rabbit' from parent_tbl where id not in(select child_id from child_tbl); select * from child_tbl; child_id | fk_id | child_fld_1 ----------+-------+------------- 1 | 1 | cat 2 | 2 | fish 3 | 3 | rabbit insert into parent_tbl values (4, 'parrot'); insert into parent_tbl values (5, 'lion'); insert into child_tbl(fk_id, child_fld_1) select id, 'rabbit' from parent_tbl where id not in(select child_id from child_tbl); select * from child_tbl; child_id | fk_id | child_fld_1 ----------+-------+------------- 1 | 1 | cat 2 | 2 | fish 3 | 3 | rabbit 4 | 4 | rabbit 5 | 5 | rabbit > > Chuck Martin > Avondale Software -- Adrian Klaver adrian.klaver@aklaver.com