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 1hSmrR-0004rx-I4 for pgsql-general@arkaria.postgresql.org; Mon, 20 May 2019 18:18:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hSmrQ-0006d3-AT for pgsql-general@arkaria.postgresql.org; Mon, 20 May 2019 18:18:08 +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 1hSmrP-0006Pw-Oq for pgsql-general@lists.postgresql.org; Mon, 20 May 2019 18:18:07 +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 1hSmrN-0000QT-83 for pgsql-general@postgresql.org; Mon, 20 May 2019 18:18:06 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 766BD24D5C; Mon, 20 May 2019 14:18:04 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 20 May 2019 14:18:04 -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=x VtCYiX/RgLUR8cAOtv2yK1EJcqhPFaLNwZb2iqsVsU=; b=hFEqjVL2mO+M2yQxK 3Vj/yOuRZzdkyojHVdKdxvIdu+Yq7jzHxK+DAa1rJTz8bIHKfB8NJKVdbyPLvBLf oSVn8WCTt0tjIbz3kz5f/IgVyxCfw2JsI3TpjG/XeLk1FMBES+KqGybkGHt1Cgi5 t1h5uKvB5W3toTxpJ1nGmOu6uD01gCIs8QfXai60fNdTu2Bv9WE05UoEPO0S4KRV u5t4icITwTcLwD27FU6NpbkhnNvg11c5iv8gPlrhxk7q1gYSbA9CaCcikvrB51sm fXjJ7C4krt2e0ckwbGEN09beIeQrnNpk47Bb2zeuZl1F3s83yqGbB6hbYmHDYHOS DiWXA== 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=xVtCYiX/RgLUR8cAOtv2yK1EJcqhPFaLNwZb2iqsV sU=; b=ScgZ7njA8orAcJqWm0iWsy2CGS5Y0oYpxctSmjb+fYXQzKiqc/iHgY84+ jzPnBYTJQ5dqYQBjTR1unkcV1B/gBWnrEDRn4T1IWMVX6g33MGEQVfJXtGaOursg +cl9I8iDgF2JdlZU5CfEn+Tt208OMYtzAFSTKUH+aMW5v1gj8HnNOQwDE/a6oPra 2TieDglbwlVSv48GWsNymSIZDmcxrdWOEMbbPah3bTW9hSrGeKemA51tpMCOlfU2 XK2bB8fw5EuDsOrzzyf2G0PT5fADxJYlkzSgukFnJw1+QIqIaLpUjprpy/AjbzBy lFuYL6+F7TBTpXXqVb4txxDTfujsA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddruddtkedguddvhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjeefrddv feelrdduudejrddvfeeknecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkh hlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd 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 4B52F8005C; Mon, 20 May 2019 14:18:03 -0400 (EDT) Subject: Re: INSERT where not exists with foreign key To: Chuck Martin , pgsql-general References: From: Adrian Klaver Message-ID: Date: Mon, 20 May 2019 11:18:02 -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. Some examples that you can modify: https://www.postgresql.org/docs/11/sql-insert.html INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07'; WITH upd AS ( UPDATE employees SET sales_count = sales_count + 1 WHERE id = (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation') RETURNING * ) INSERT INTO employees_log SELECT *, current_timestamp FROM upd; > > Chuck Martin > Avondale Software -- Adrian Klaver adrian.klaver@aklaver.com