首页 > 开发 > 综合 > 正文

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

2024-07-21 02:49:22
字体:
来源:转载
供稿:网友
sql语句查询数据库表结构信息

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

1.适用MS SQL SERVER:

 1 SELECT 2 表名 = case when a.colorder=1 then d.name else '' end, 3 表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end, 4 字段序号 = a.colorder, 5 字段名 = a.name, 6 标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end, 7 主键 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in ( 8 SELECT name FROM sysindexes WHERE indid in( 9 SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,10 类型 = b.name,11 占用字节数 = a.length,12 长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),13 小数位数 = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),14 允许空 = case when a.isnullable=1 then '√'else '' end,15 默认值 = isnull(e.text,''),16 字段说明 = isnull(g.[value],'')17 FROM18 syscolumns a19 left join20 systypes b21 on22 a.xusertype=b.xusertype23 inner join24 sysobjects d25 on26 a.id=d.id and d.xtype='U' and d.name<>'dtproperties'27 left join28 syscomments e29 on30 a.cdefault=e.id31 left join32 sys.extended_properties g33 on34 --a.id=g.id and a.colid=g.smallid35 a.id=g.major_id and a.colid=g.Minor_id36 left join37 sys.extended_properties f38 on39 --d.id=f.id and f.smallid=040 d.id=f.major_id and f.Minor_id=041 where42 d.name='表名' --如果只查询指定表,加上此条件43 order by44 a.id,a.colorder

2.适用Oracle:

 1 SELECT 2 USER_TAB_COLS.TABLE_NAME as 表名, 3 user_tab_comments.comments as 表备注, 4 USER_TAB_COLS.COLUMN_ID as 列序号, 5 user_col_comments.comments as 列备注, 6 USER_TAB_COLS.COLUMN_NAME as 列名 , 7 USER_TAB_COLS.DATA_TYPE as 数据类型, 8 USER_TAB_COLS.DATA_LENGTH as 长度, 9 USER_TAB_COLS.NULLABLE as 是否为空,10 user_cons_columns.constraint_name as 约束名,11 user_constraints.constraint_type as 主键12 FROM USER_TAB_COLS inner join user_col_comments on13 user_col_comments.TABLE_NAME=USER_TAB_COLS.TABLE_NAME14 and user_col_comments.COLUMN_NAME=USER_TAB_COLS.COLUMN_NAME15 INNER join user_cons_columns on user_cons_columns.table_name=USER_TAB_COLS.table_name16 INNER join user_constraints on user_constraints.table_name=USER_TAB_COLS.table_name and user_constraints.constraint_name=user_cons_columns.constraint_name17 inner join user_tab_comments on USER_TAB_COLS.TABLE_NAME=user_tab_comments.TABLE_NAME18 WHERE USER_TAB_COLS.table_name='表名'19 ORDER BY USER_TAB_COLS.TABLE_NAME

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表