sql语句查询数据库表结构信息
开发中经常⽤到查询指定表及其字段的信息,以下是我整理的SQL语句查询⽅法,供⾃⼰平时使⽤也提供给⼤家参考!
1.适⽤MS SQL SERVER:
1 SELECT
2表名 = case lorder=1 then d.name else'' end,oracle数据库表结构怎么看
3表说明 = case lorder=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 lid))) 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默认值 = ,''),
16字段说明 = isnull(g.[value],'')
17 FROM
18 syscolumns a
19 left join
20 systypes b
21 on
22 a.xusertype=b.xusertype
23 inner join
24 sysobjects d
25 on
26 a.id=d.id pe='U' and d.name<>'dtproperties'
27 left join
28 syscomments e
29 on
30 a.cdefault=e.id
31 left join
ded_properties g
33 on
34 --a.id=g.id lid=g.smallid
35 a.id=g.major_id lid=g.Minor_id
36 left join
ded_properties f
38 on
39 --d.id=f.id and f.smallid=0
40 d.id=f.major_id and f.Minor_id=0
41where
42 d.name='表名' --如果只查询指定表,加上此条件
43 order by
44 a.lorder
2.适⽤ORACLE:
1 SELECT
2 USER_TAB_COLS.TABLE_NAME as表名,
3 user_tab_commentsments as表备注,
4 USER_TAB_COLS.COLUMN_ID as列序号,
5 user_col_commentsments 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_straint_name as约束名,
11 straint_type as主键
12 FROM USER_TAB_COLS inner join user_col_comments on
13 user_col_comments.TABLE_NAME=USER_TAB_COLS.TABLE_NAME
14 and user_col_comments.COLUMN_NAME=USER_TAB_COLS.COLUMN_NAME
15 INNER join user_cons_columns on user_cons_columns.table_name=USER_TAB_COLS.table_name
16 INNER join user_constraints on user_constraints.table_name=USER_TAB_COLS.table_name and straint_name=user_straint_name
17 inner join user_tab_comments on USER_TAB_COLS.TABLE_NAME=user_tab_comments.TABLE_NAME
18 WHERE USER_TAB_COLS.table_name='表名'
19 ORDER BY USER_TAB_COLS.TABLE_NAME
原⽂其它⽹址:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论