SQL Cookbook 系列 - 元数据查询

  1. 列出模式中的表
  2. 列出表的列
  3. 列出表的索引列
  4. 列出表约束
  5. 列出没有相应索引的外键
  6. 使用SQL来生成SQL
  7. 在oracle中描述数据字典视图

元数据在数据库中是用来描述其他数据库对象的数据,例如描述表,约束,索引等。
这个按照我的理解来看,元数据是数据库管理数据库对象的记录。

1.列出模式中的表
查看在给出模式中所有已创建的表的清单:
db2 : select tabname from syscat.tables where tabschema='SNEAGOL';
oracle : select table_name from all_tables where owner='SMEAGOL';
postgreSQL,mysql,sqlserver: select table_name from information_schema.tables where table_schema='SMEAGOL';
Note: schema的值是当前数据库的schema

2.列出表的列
列出表的各列、数据类型、表中的位置
db2: select colname,typename,colno from syscat.columns where tabname='emp' and tabschema='GOL';
oracle: select column_name,data_type,column_id from all_tab_columns where owner='GOL' and table_name='EMP';
postgreSQL,mysql,sqlserver: select column_name,data_type,ordinal_position from information_schema.columns where table_schema='website' and table_name='product';
Note:果然好用

3.列出表的索引列
列出给定表的索引,索引的列,索引列在索引中的位置
db2: selelct a.tabname,b.indname,b.colseq from syscat.indexes a ,syscat.indexcoluse b
where a.tabname='website' and a.tabname='product' and
a.indachema=b.indschema and a.indname=b.indname
oracle : select table_name,index_name,column_name,column_position from sys.all_ind_columns
where table_name='product' and table_owner='website';
postgreSQL: select a.tablename,a.indexname,b.column_name from pg_catalog.pg_indexes a,
information_schema.columns b where a.schema='website' and
a.tablename=b.table_name;
mysql: show index from product
sqlserver : select a.name table_name,b.name index_name,d.name column_name,c.index_column_id from sys.tables a,sys.indexes b, sys.index_columns c,sys.columns d where a.object_id=b.object_id and b.object_id=c.object_id and b.index_id=c.index_id and c.object_id=d.object_id and c.column_id=d.column_id and a.name='product';

4.列出表的约束
列出某模式中对牟彪定义的约束和这些约束基于的列
db2: select a.tabname,a.constname,b.colname,a.type from syscat.tabconst a,syscat.columns b
where a.tabname='product' and a.tabschema='website' and a.tabname=b.tabname and a.tabschema=b.tabschema;
oracle: select a.table_name,a.constraint_name,b.column_name,a.constraint_type from all_constraints a,all_const_columns b where a.table_name='product' and a.owner='website' and a.table_name=b.table_name and a.owner=b.owner and a.constraint_name=b.constraint_name;
postgreSQL,mysql,sqlserver : select a.table_name,a.constraint_name,b.column_name,a.constraint_type from information_schema.table_constraints a,information_schema.key_column_usage b where a.table_name='product' and a.table_schema='website' and a.table_name=b.table_name and a.table_schema=b.table_schema and a.constraint_name=b.constraint_name;
Note:这一部分的语句需要会用,只要会修改会使用即可

5.列出没有相应索引的外键
db2: select fkeys.tabname,fkeys.constname,fkeys.colname,ind_cols.indname from (
select a.tabschema,a.tabname,a.constname,b.colname from syscat.tabconst a,syscat.keycoluse b
where a.tabname='product' and a.tabschema='website' and a.type='F'
and a.tabname=b.tabname and a.tabschema=b.tabschema
) fkeys left join (
select a.tabschema,a.tabname,a.indname,b.colname from syscat.indexes a,syscat.indexcoluse b where a.indschema=b.indschema and a.indname=b.indname
) ind_cols on (
fkeys.tabschema=ind_cols.tabschema and
fkeys.tabname=ind_cols.tabname and
fkeys.colname=ind_cols.colname
) where ind_cols.indname is null;
oracle: select a.table_name,a.constraint_name,a.column_name,c.index_name from
all_cons_columns a,all_constraints b,all_ind_columns c
where a.table_name='product' and a.owner='website' and b.constraint_type='R'
and a.owner=b.owner and a.table_name=b.table_name and
a.constraint_name=b.constraint_name and a.owner=c.table_owner {+}
and a.table_name=c.table_name {+} and a.column_name=c.column_name {+}
and c.index_name is null;
postgreSQL: select fkeys.table_name,fkeys.constraint_name,fkeys.column_name,ind_cols.indexname from
(
select a.constraint_schema,a.table_name,a.constraint_name,a.column_name
from information_schema.key_column_usage a,
information_schema.referential_constraints b
where a.constraint_name=b.constraint_name and
a.constraint_schema=b.constraint_schema
and a.constraint_name='website' and a.table_name='product'
) fkeys left join (
select a.schemaname,a.tablename,a.indexname,b.column_name from
pg_catalog.pg_indexes a,information_schema.columns b
where a.tablename=b.table_name and a.schemaname=b.table_schema
) ind_cols on (
fkeys.constrain_schema=ind_clos.schemaname
and fkeys.table_name=ind_cols.tablename
and fkeys.column_name=ind_cols.column_name
) where ind_cols.indexname is null;
mysql: show index from product;
查询information_schema.key_column_usage.column_name中同一张表的结果进行比较,
不同的就是没有被索引的
sqlserver: select fkeys.table_name.fkeys.constraint_name,fkeys.column_name,ind_cols.index_name
from (
select a.object_id,d.colmun_id,a.name table_name,b.name constraint_name,d.name column_name
from sys.table a join sys.foreign_keys b on (
a.name='product' and a.object_id=b.object_id
) join sys.foreign_key_columns c on (
b.object_id=c.constraint_object_id
) join sys.colmuns d on (
c.constraint_column_id=d.column_id and a.object_id=d.object_id
)
) fkeys left join (
select a.name index_name,b.object_id,b.column_id from
sys.indexes a,sys.index_columns b
where a.index_id=b.index_id
) ind_cols on (
fkeys.object_id=ind_cols.object_id
and fkeys.column_id=ind_cols.column_id
) where ind_cols.index_name is null;
Note:因为我也不是专业的dba,没有用过,不过这些sql写的真好

6.用SQL来生成SQL
其用于可移植脚本方面非常有用,例如多种环境下的测试
mysql : select concat('select count(*) from ' , table_name , '; ') from information_schema.tables;
实际上就是根据元数据,生成一些关于查询结果数据的字符串,不过这些字符串是sql格式的字符串。
当生成的结果输出到文件中的时候,我们会获得一些简单的批量sql语句。
Note:其他数据库也是支持的,这个放心的用

7.在oracle中描述数据字典视图
oracle不仅维护了一系列的数据字典视图,还有用来说明这些数据字典的数据字典视图。
select table_name,comments from dictionary order by table_name;
Note:这个是Oracle数据库特有的

猜你喜欢

转载自blog.csdn.net/seacean2000/article/details/80897764