首页 > 数据库 > MySQL > 正文

解析SQL 表结构信息查询 含主外键、自增长

2024-07-24 12:48:21
字体:
来源:转载
供稿:网友
最近项目需要做什么数据字典,需要表结构信息。在网上看了许多关于表结构信息的查询,感觉都不怎么样。相对好一点就是《基于SQL2005 SQL2008 表结构信息查询升级版的详解(含外键信息)》 ,但是这里有一点小问题,缺少一个过滤以致运行有一点小bug。在AdventureWorks2012数据库中的Address表查询结果如图:

在查询过滤中我们添加以下信息就ok了:
AND g.class_desc = 'OBJECT_OR_COLUMN'
修改后的SQL如下:
复制代码 代码如下:

SELECT  表名 = CASE WHEN a.colorder = 1 THEN d.name
                  ELSE ''
             END ,
        表说明 = CASE WHEN a.colorder = 1 THEN ISNULL(f.value, '')
                   ELSE ''
              END ,
        字段序号 = a.colorder ,
        字段名 = a.name ,
        标识 = CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '√'
                  ELSE ''
             END ,
        主键 = CASE WHEN EXISTS ( SELECT  1
                                FROM    dbo.sysindexes si
                                        INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id
                                                              AND si.indid = sik.indid
                                        INNER JOIN dbo.syscolumns sc ON sc.id = sik.id
                                                              AND sc.colid = sik.colid
                                        INNER JOIN dbo.sysobjects so ON so.name = so.name
                                                              AND so.xtype = 'PK'
                                WHERE   sc.id = a.id
                                        AND sc.colid = a.colid ) THEN '√'
                  ELSE ''
             END ,
        外键 = CASE WHEN tony.fkey IS NOT NULL
                       AND tony.fkey = a.colid THEN '√'
                  ELSE ''
             END ,
        外键表 = CASE WHEN tony.fkey IS NOT NULL
                        AND tony.fkey = a.colid THEN OBJECT_NAME(tony.fkeyid)
                   ELSE ''
              END ,
        外键字段 = CASE WHEN tony.fkey IS NOT NULL
                         AND tony.fkey = a.colid
                    THEN ( SELECT   name
                           FROM     syscolumns
                           WHERE    colid = tony.fkey
                                    AND id = tony.fkeyid
                         )
                    ELSE ''
               END ,
        类型 = b.name ,
        长度 = a.length ,
        精度 = COLUMNPROPERTY(a.id, a.name, 'PRECISION') ,
        小数位数 = ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) ,
        允许空 = CASE WHEN a.isnullable = 1 THEN '√'
                   ELSE ''
              END ,
        默认值 = ISNULL(e.text, '') ,
        字段说明 = ISNULL(g.[value], '') ,
        创建时间 = d.crdate ,
        更改时间 = CASE WHEN a.colorder = 1 THEN d.refdate
                    ELSE NULL
               END
FROM    dbo.syscolumns a
        LEFT JOIN dbo.systypes b ON a.xtype = b.xusertype
        INNER JOIN dbo.sysobjects d ON a.id = d.id
                                       AND d.xtype = 'U'
                                       AND d.status >= 0
        LEFT JOIN dbo.syscomments e ON a.cdefault = e.id
        LEFT JOIN sys.extended_properties g ON a.id = g.major_id
                                               AND a.colid = g.minor_id
                                               AND g.class_desc = 'OBJECT_OR_COLUMN'
        LEFT JOIN sys.extended_properties f ON d.id = f.major_id
                                               AND f.minor_id = 0
        LEFT JOIN sysobjects htl ON htl.parent_obj = d.id
                                    AND htl.xtype = 'F'
        LEFT JOIN sysforeignkeys tony ON htl.id = tony.constid
WHERE   d.name = 'Address'  --这里输入包含表名称的条件
ORDER BY d.id ,
        a.colorder
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表