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 1sGkvC-00GmmB-S1 for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 19:43:15 +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 1sGkvA-00FVUk-PQ for pgsql-general@arkaria.postgresql.org; Mon, 10 Jun 2024 19:43:13 +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 1sGkvA-00FVUc-C3 for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 19:43:13 +0000 Received: from mail-oa1-x2d.google.com ([2001:4860:4864:20::2d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sGkv9-000z6c-4U for pgsql-general@lists.postgresql.org; Mon, 10 Jun 2024 19:43:12 +0000 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-25487d915b8so150638fac.0 for ; Mon, 10 Jun 2024 12:43:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718048589; x=1718653389; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=KxOSsmUT1zq+PxkCmS9yqPgihHWHbEAQhkvThdH5U/E=; b=cWG6pLdVdZy9OylONFVzm1j1aY5RJuU1ttNe9Rpj8KV+aBNDk9HLhvL/rzEGHyzENu 35z3/Q1qyWbzNwB22KmBnzqFqP/eB7pxzbVhHPtGsbwUevSKQOK9zTPIvnc8zD26WVUF I4W0pgIsxPq0Dknw58+SuLqZqQu8Iv2W/xuF/IS/KGQoS0jpta2CUOb4g+1zmHq5IuQ3 U597CkN4whb0/gMSR58pH9iA8L3jwmyJT5Sx5xrG+Ct68XWL2lgyZvYfnsZ+S2ZZFrcF zjCYat+K6oaP0R/X0WwfDtUnVRsoFuNMb9oxXGSpBEMP/V8mgjJmpH1QZQOJbmWERiDj W2Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718048589; x=1718653389; h=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=KxOSsmUT1zq+PxkCmS9yqPgihHWHbEAQhkvThdH5U/E=; b=iRqSi/aVZI/N3R3h7/2UNzYElkzf1Y48+6trJoOjR9WOkLIz0RE3XnOcSCum6VR1z4 yATxyI3hJq8UyKvNi0Ad+C9+mOm2kiOIiEXEDU7Lk8eilahiRMleHF2OYbIRT+M5HoNM /UtN4QZCCgBQuisE7IqBWXcpo5/OHSHnuNP7YB5c1oJsb094PXUuAvACjYrX9ihE0G4f HQ+bcYYswAfxJedG0YQs525Nt8Lm+MhpxHA7Te8Ia+Gg0Zqke/uqCglHVFzv0OYeDe4T He0Qk2B6v+PlzhkeBFaewlSBSRtRkGRFLwuXRPKhyPEASq9WCqtOREtKdtoeahUjN/uf ov3A== X-Gm-Message-State: AOJu0YzD5nVf8icOqZFfnX8dQC01TfUGjVPkhcEz5vfap94IxMIqbvsa +zdf0CEQkRrl3A4xLuLECBpdFF/yykv6QXFjNv4PFUs7w+379VTZXoSUf8XEgHsNRgfWb6kNXVc GhH5T6YEpibtCCW/3SZX+mlqTPn/25g== X-Google-Smtp-Source: AGHT+IFQQtYdbWocrYiVIdJWCUWXgtM56NcxIRb/FMrohawbDof70WT/HMkHQjsedII5vJlkPYO/ZdcMyP970meTg8M= X-Received: by 2002:a05:6870:158b:b0:24f:d1bb:993e with SMTP id 586e51a60fabf-25463efd873mr12622119fac.0.1718048588053; Mon, 10 Jun 2024 12:43:08 -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: Ron Johnson Date: Mon, 10 Jun 2024 15:42:56 -0400 Message-ID: Subject: Re: Multiple tables row insertions from single psql input file To: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000cc449c061a8e5b5c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000cc449c061a8e5b5c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Jun 10, 2024 at 2:50=E2=80=AFPM Rich Shepard wrote: > My business tracking database has three main tables: company, location, > contact. The company and contact primary keys are sequences. > > I've been adding new rows using INSERT INTO files separately for each tab= le > after manually finding the last PK for the company and contact tables. Th= e > location table has the company PK as a FK; the contact table has both > company PK and location PK as foreign keys. > > Now I will use next_val 'PK' to assign the value for each new table row. > > 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? > With enough clever scripting you can create a .sql file that does almost anything. Most useful to you will be some number of "ALTER TABLE DISABLE TRIGGER ALL;" statements near the beginning of the file, and their "ALTER TABLE ... ENABLE TRIGGER ALL;" counterparts near the end of the file. --000000000000cc449c061a8e5b5c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Mon, Jun 10, 2024 at 2:50=E2=80=AFPM R= ich Shepard <rshepard@appl-e= cosys.com> wrote:
My business tracking database has three= main tables: company, location,
contact. The company and contact primary keys are sequences.

I've been adding new rows using INSERT INTO files separately for each t= able
after manually finding the last PK for the company and contact tables. The<= br> location table has the company PK as a FK; the contact table has both
company PK and location PK as foreign keys.

Now I will use next_val 'PK' to assign the value for each new table= row.

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 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
With enough clever scripting you can create a .sq= l file that does almost anything.

Most useful to y= ou will be some number of "ALTER TABLE <fo= o> DISABLE TRIGGER ALL;" statements near the beginning of th= e file, and their "ALTER TABLE ... ENABLE TRI= GGER ALL;" counterparts near the end of the file.
--000000000000cc449c061a8e5b5c--