sql语句查询数据库表结构信息

开发中经常用到查询指定表及其字段的信息,以下是我整理的SQL语句查询方法,供自己平时使用也提供给大家参考!

1.适用MS SQL SERVER:

 SELECT
表名 = case when a.colorder= then d.name else '' end,
表说明 = case when a.colorder= then isnull(f.value,'') else '' end,
字段序号 = a.colorder,
字段名 = a.name,
标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')= then '√'else '' end,
主键 = case when exists(SELECT FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
类型 = b.name,
占用字节数 = a.length,
长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),),
允许空 = case when a.isnullable= then '√'else '' end,
默认值 = isnull(e.text,''),
字段说明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join
syscomments e
on
a.cdefault=e.id
left join
sys.extended_properties g
on
--a.id=g.id and a.colid=g.smallid
a.id=g.major_id and a.colid=g.Minor_id
left join
sys.extended_properties f
on
--d.id=f.id and f.smallid=
d.id=f.major_id and f.Minor_id=
where
d.name='表名' --如果只查询指定表,加上此条件
order by
a.id,a.colorder

2.适用ORACLE:

 SELECT
USER_TAB_COLS.TABLE_NAME as 表名,
user_tab_comments.comments as 表备注,
USER_TAB_COLS.COLUMN_ID as 列序号,
user_col_comments.comments as 列备注,
USER_TAB_COLS.COLUMN_NAME as 列名 ,
USER_TAB_COLS.DATA_TYPE as 数据类型,
USER_TAB_COLS.DATA_LENGTH as 长度,
USER_TAB_COLS.NULLABLE as 是否为空,
user_cons_columns.constraint_name as 约束名,
user_constraints.constraint_type as 主键
FROM USER_TAB_COLS inner join user_col_comments on
user_col_comments.TABLE_NAME=USER_TAB_COLS.TABLE_NAME
and user_col_comments.COLUMN_NAME=USER_TAB_COLS.COLUMN_NAME
INNER join user_cons_columns on user_cons_columns.table_name=USER_TAB_COLS.table_name
INNER join user_constraints on user_constraints.table_name=USER_TAB_COLS.table_name and user_constraints.constraint_name=user_cons_columns.constraint_name
inner join user_tab_comments on USER_TAB_COLS.TABLE_NAME=user_tab_comments.TABLE_NAME
WHERE USER_TAB_COLS.table_name='表名'
ORDER BY USER_TAB_COLS.TABLE_NAME

原文其它网址:http://www.zuowenjun.cn/post/2014/08/28/26.html

上一篇:利用doScroll在IE浏览器里模仿DOMContentLoaded


下一篇:MAC 终端上传代码到Github 或者 Gitlab