Pages

Monday, 18 February 2013

Finding tables by name in MySQL and Oracle

My SQL
MySQL has the INFORMATION_SCHEMA. In this schema is the TABLES table. So to hunt down a table all tables containing the word forward you can do:

SELECT TABLE_SCHEMA, TABLE_NAME 
  FROM INFORMATION_SCHEMA.TABLES
 WHERE TABLE_NAME LIKE '%FORWARD%'


To hunt down a table with a given column use the COLUMNS table:

SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
  FROM INFORMATION_SCHEMA.COLUMNS
 WHERE COLUMN_NAME LIKE '%FORWARD%'


Oracle
Oracle has the ALL_TABLES table. So to hunt down a table you can do:

SELECT OWNER, TABLE_NAME 
  FROM ALL_tables
 WHERE TABLE_NAME LIKE '%
FORWARD%'

This will return the system tables as well as such you may wish to filter on owner.

To hunt down a table with a given column use the ALL_TAB_COLUMNS:

SELECT OWNER, TABLE_NAME, COLUMN_NAME
  FROM all_tab_columns
 WHERE COLUMN_NAME LIKE '%FORWARD%'


1 comment: