目标:根据系统视图联合查询包含phone字段的列名、数据量大于阈值、表名、schema、数据库名,并按数据量排序。
返回数据格式:schema-table-column-rows
mssql
SELECT t.TABLE_SCHEMA as 'TABLE_SCHEMA',a.name as 'TABLE_NAME', c.COLUMN_NAME, b.rows FROM sysobjects AS a
INNER JOIN sysindexes AS b ON a.id = b.id
INNER JOIN information_schema.columns c on a.name = c.TABLE_NAME
INNER JOIN information_schema.tables t on a.name = t.TABLE_NAME
WHERE (a.type = 'u') AND (b.indid IN (0, 1)) and (c.COLUMN_NAME like '%phone%') and (b.rows > 100)
ORDER BY b.rows DESC
mysql
SELECT t.table_schema AS 'schema', t.table_name AS 'table', c.COLUMN_NAME AS 'column', t.table_rows AS 'rows'
FROM information_schema.TABLES t
left join information_schema.COLUMNS c on c.table_name = t.table_name
WHERE c.COLUMN_NAME like '%phone%' and t.table_rows > 100
group by t.table_schema, t.table_name, c.COLUMN_NAME, t.table_rows order by t.table_rows desc
oracle
select s.username "schema",t.table_name "table",c.column_name "column_name",t.num_rows "num_rows" from sys.dba_users s
right join sys.dba_tables t on s.username=t.owner
right join all_tab_columns c on c.table_name=t.table_name
where t.num_rows > 100 and c.column_name like '%PHONE%'
group by s.username,t.table_name,c.column_name,t.num_rows order by 4 desc
postgres
SELECT c.table_schema,c.table_name,c.column_name,t.n_live_tup FROM pg_stat_user_tables as t
inner join information_schema.columns as c
on c.table_name = t.relname
where c.column_name like '%PHONE%' and t.n_live_tup >100
ORDER BY t.n_live_tup DESC