Single SQL to find if any record exists across several tables

From: Ertan Küçükoğlu <ertan(dot)kucukoglu(at)1nar(dot)com(dot)tr>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Single SQL to find if any record exists across several tables
Date: 2017-09-21 12:45:02
Message-ID: 002301d332d7$745b1500$5d113f00$@1nar.com.tr
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello,

Using PostgreSQL 9.6.5 64bit running on Windows 10 64bit.

There are no foreign key across tables in database. There is a table where
company codes are kept. Most other tables includes company code value in a
column. Column name is same for all tables which is "fk".

Before deleting any company code row, I would like to be sure that it is not
used in anywhere. Currently I am running a separate query for each table but
that takes a lot of time and possibility to miss a table or two.

Is it possible to build a single SQL statement and include all tables in it
so that I receive single "count(fk)" sum across all tables?

Several sample SQLs run at the moment are as follows:
Select count(fk) from cari
Select count(fk) from sipbaslik
Select count(fk) from stkdepo
Select count(fk) from stkkart
Select count(fk) from stkhar

Thanks & regards,
Ertan Küçükoğlu

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2017-09-21 15:06:14 Re: Single SQL to find if any record exists across several tables
Previous Message David G. Johnston 2017-09-20 16:20:27 Re: SETOF any table records AS return in pgSQL functions