-1

In my database, all tables should have a column (let's say "abc") and I want to find out the tables which do not have this column. Do we have any such query to fulfill this requirement?

Database: Db2 v11.1 LUW

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • Possible duplicate of [Find related columns among hundreds of tables for future relational identification](https://stackoverflow.com/questions/42397279/find-related-columns-among-hundreds-of-tables-for-future-relational-identificati) – Clockwork-Muse May 14 '19 at 21:14

2 Answers2

1

You can build a query against SYSCAT.COLUMNS (and SYSCAT.TABLES) to find those tables not having such column:

select tabname from syscat.tables t1
where not exists
    (select colname from syscat.columns c
     where c.tabname=t1.tabname and colname='foo')
and tabname like 'SYSX%'

Above is just an example and not optimized.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
0

Non-system tables only. Column name must be in uppercase, unless you specified the column name as “abc” (in double quotes) upon the table creation intentionally.

select tabschema, tabname 
from syscat.tables t
where not exists
(
select 1 
from syscat.columns c
where c.tabschema=t.tabschema and c.tabname=t.tabname 
and c.colname='ABC'
)
and tabschema not like 'SYS%'
and type='T';
Mark Barinstein
  • 11,456
  • 2
  • 8
  • 16