Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGlE4-00GqcR-KA for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 20:02:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sGlE2-00Fccv-3X for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 20:02:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGlE1-00FccB-OQ for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 20:02:42 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sGlDz-000zF6-Sn for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 20:02:42 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2ebdfe26217so15396351fa.2 for ; Mon, 10 Jun 2024 13:02:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718049759; x=1718654559; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=jTGdGTwHTInxcLJu47D8Q7LpHRHYq/s7P0AhCNgXjQ8=; b=WsGu2P3eZD9z2BWejBtmEzrgBmG9KDOzZh+DV9x8O036FJkRreEHbnezbSrfHlUiS4 ijQF9ww23vfu/RS3335Y3Uj1lTEMzLMi5SJejMNFeeqLiVFzNaMne4tNOjsqDdULLBQ+ ydabdwnj6iVsbWirqY/GaVj98qOinna3SGNIqjF4g07ovo6Sduqz2vJHDNqilN3hzXn0 0VwxTj8Duf27ViPthxbURDA2IZ2tYwJT2MOFPPEGXGnX6y37sbGKLXmZ2bkffpBzd9gf Hm+yC4YN4i/XDMvdVVpvjW98Ywqe4udOuO7AI6mb7QjzwJ2siz/HbLz9s+58oDI5jCbR 58Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718049759; x=1718654559; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jTGdGTwHTInxcLJu47D8Q7LpHRHYq/s7P0AhCNgXjQ8=; b=pQLdD60UIUv9p4Gfzzn2+jjirO6hZoE2VeSh/mZx70unzNNkzAqoJ0MntpJDbYEsBh Se3SpHYNfwtbzSef+ljEdq5BBa/IEs+p/9lSHOpeBb/w6F9PdOaQfFfB/eQ62pfjypKO 1PxD3qFF8qgutP9GLM9bTqcsFmDloesMCI22u4VQRXeZxL5dedocYlGaQUgHkgAmpTkO N7WYa57oFeWe/S4+ze4xo5vHiP6CyFzTqIVT1vttwFfZo/Yot1R7t+8V1P2F0M2hXfYV /vsgff3wMDNn2nezOhYzYt5kpfITi420ZdozWj7H6ulQ6mjbjtCltsPlYTW/VEAJHgPS omJw== X-Gm-Message-State: AOJu0Yyc77wCGh30zbGiAYxlXpDdDbags3yzJ2c+RKIbsoJQqs8BPbzo iZiuT3dQOMA+qr6Uge1AKNmqL+T7hGf2N0xh/l1SyIF8//0ghJw297D4W65YiiBpAa/VuJNU6ux wWprgGtk7bbmsnpQ1SbBJMOp2vQ== X-Google-Smtp-Source: AGHT+IE7sVEdGNLT07XC+jx9SFjQwXfQhkyEHet1LKW/puZM5+VcVn/7PxWF0x0TtqcSNPmtIEfpjNvo+BQIOAZZzDw= X-Received: by 2002:a2e:8750:0:b0:2ea:7d8f:8d12 with SMTP id 38308e7fff4ca-2eadce91435mr58760811fa.48.1718049758517; Mon, 10 Jun 2024 13:02:38 -0700 (PDT) MIME-Version: 1.0 References: <7da8ec9-089-74ce-eb3-c88889b62c64@appl-ecosys.com> In-Reply-To: <7da8ec9-089-74ce-eb3-c88889b62c64@appl-ecosys.com> From: =?UTF-8?Q?Torsten_F=C3=B6rtsch?= Date: Mon, 10 Jun 2024 22:02:26 +0200 Message-ID: Subject: Re: Multiple tables row insertions from single psql input file To: Rich Shepard Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000009026e1061a8ea107" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000009026e1061a8ea107 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jun 10, 2024 at 8:50=E2=80=AFPM Rich Shepard wrote: > My question is whether I can create new rows for all three tables in the > same sql source file. Since the location and contact tables require > sequence > numbers from the company and location tables is there a way to specify, > e.g., current_val 'tablename PK' for the related tables? Or, do I still > need > to enter all new companies before their locations and contact? > > Something along these lines perhaps: =3D# create table a( id bigserial primary key, x text ); CREATE TABLE =3D# create table b( fk bigint references a(id), y text ); CREATE TABLE =3D# with ins_a as (insert into a (x) values ('a row') returning *) insert into b(fk, y) select ins_a.id, 'yy'||i.i from ins_a cross join generate_series(1,10) as i(i); INSERT 0 10 =3D# table a; table b; id | x ----+------- 1 | a row (1 row) Time: 0.215 ms fk | y ----+------ 1 | yy1 1 | yy2 1 | yy3 1 | yy4 1 | yy5 1 | yy6 1 | yy7 1 | yy8 1 | yy9 1 | yy10 (10 rows) --0000000000009026e1061a8ea107 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Mon, Jun 10, 2024 at 8:50=E2=80= =AFPM Rich Shepard <rshepard= @appl-ecosys.com> wrote:
<= blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-l= eft:1px solid rgb(204,204,204);padding-left:1ex">My question is whether I c= an create new rows for all three tables in the
same sql source file. Since the location and contact tables require sequenc= e
numbers from the company and location tables is there a way to specify,
e.g., current_val 'tablename PK' for the related tables? Or, do I s= till need
to enter all new companies before their locations and contact?

=C2=A0
Something along these lines perhaps:=C2=A0

=3D# create table a( id bigserial primary key, x text );
CREATE TABLE
=3D# create table b( fk bigint referen= ces a(id), y text ); =C2=A0
CREATE TABLE
=3D# with ins_a as (insert into a (x) = values ('a row') returning *)
=C2=A0 =C2=A0insert into b(fk, y) select i= ns_a.id, 'yy'||i.i from ins_a cross join generate_series(1,10) = as i(i);
INSERT 0 10
=3D# table a; table b;
id | =C2=A0=C2=A0x =C2=A0=C2=A0=C2=A0
----+-------
=C2=A01 | a row
(1 row)

Time: 0.215 ms
fk | =C2=A0y =C2=A0=C2=A0=C2=A0
----+------
=C2=A01 | yy1
=C2=A01 | yy2
=C2=A01 | yy3
=C2=A01 | yy4
=C2=A01 | yy5
=C2=A01 | yy6
=C2=A01 | yy7
=C2=A01 | yy8
=C2=A01 | yy9
=C2=A01 | yy10
(10 rows)

=C2=A0
--0000000000009026e1061a8ea107--