程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 解析SQL 表構造信息查詢 含主外鍵、自增加

解析SQL 表構造信息查詢 含主外鍵、自增加

編輯:MySQL綜合教程

解析SQL 表構造信息查詢 含主外鍵、自增加。本站提示廣大學習愛好者:(解析SQL 表構造信息查詢 含主外鍵、自增加)文章只能為提供參考,不一定能成為您想要的結果。以下是解析SQL 表構造信息查詢 含主外鍵、自增加正文


比來項目須要做甚麼數據字典,須要表構造信息。在網上看了很多關於表構造信息的查詢,感到都不怎樣樣。絕對好一點就是《基於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

運轉成果如圖:

我不怎樣愛好它的“類型”信息,普通的varchar都邑有長度信息,還有這個查詢關於SQL 2012的新數據類型不支撐,該SQL外面的嵌套查詢比擬多,因而我就本身從新寫了一個SQL。
這裡提示年夜家盡可能用INFORMATION_SCHEMA.XXX視圖而不去用sys.XXX視圖。
新的SQL以下:

SELECT
 --OBJECT_ID(a.TABLE_SCHEMA + '.' + a.TABLE_NAME) AS [object_id] ,
        CASE WHEN a.ORDINAL_POSITION = 1
             THEN a.TABLE_SCHEMA + '.' + a.TABLE_NAME
             ELSE ''
        END AS TABLE_NAME ,
        CASE WHEN ( a.ORDINAL_POSITION = 1
                    AND p1.value IS NOT NULL
                  ) THEN p1.value
             ELSE ''
        END AS TABLE_Description ,
        a.COLUMN_NAME ,
        CASE WHEN ( ( CHARINDEX('char', a.DATA_TYPE) > 0
                      OR CHARINDEX('binary', a.DATA_TYPE) > 0
                    )
                    AND a.CHARACTER_MAXIMUM_LENGTH <> -1
                  )
             THEN a.DATA_TYPE + '('
                  + CAST(a.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(4)) + ')'
             WHEN ( ( CHARINDEX('CHAR', a.DATA_TYPE) > 0
                      OR CHARINDEX('binary', a.DATA_TYPE) > 0
                    )
                    AND a.CHARACTER_MAXIMUM_LENGTH = -1
                  ) THEN a.DATA_TYPE + '(max)'
             WHEN ( CHARINDEX('numeric', a.DATA_TYPE) > 0 )
             THEN a.DATA_TYPE + '(' + CAST(a.NUMERIC_PRECISION AS VARCHAR(4))
                  + ',' + CAST(a.NUMERIC_SCALE AS VARCHAR(4)) + ')'
             ELSE a.DATA_TYPE
        END AS COLUMN_TYPE ,
        CASE WHEN c.IS_IDENTITY = 1 THEN 'YES'
             ELSE 'NO'
        END AS IS_IDENTITY ,
        a.IS_NULLABLE ,
        CASE WHEN a.COLUMN_DEFAULT IS NULL THEN ''
             ELSE a.COLUMN_DEFAULT
        END AS Default_Value ,
        CASE WHEN p.value IS NULL THEN ''
             ELSE p.value
        END AS [COLUMN_Description] ,
        CASE WHEN o.name IS  NULL THEN ''
             ELSE '√'
        END AS Is_PrimaryKey ,
        CASE WHEN f.parent_column_id IS NULL THEN ''
             ELSE '√'
        END AS Is_Foreignkeys ,
        CASE WHEN referenced_object_id IS NULL THEN ''
             ELSE OBJECT_NAME(referenced_object_id)
        END AS Foreign_Table ,
        CASE WHEN referenced_object_id IS NULL THEN ''
             ELSE ( SELECT  name
                    FROM    sys.columns
                    WHERE   object_id = f.referenced_object_id
                            AND column_id = f.referenced_column_id
                  )
        END AS Foreign_key
FROM    INFORMATION_SCHEMA.COLUMNS a
        INNER JOIN sys.columns c ON OBJECT_ID(a.TABLE_SCHEMA + '.'
                                              + a.TABLE_NAME) = c.OBJECT_ID
                                    AND a.COLUMN_NAME = c.NAME
        LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE b ON a.TABLE_SCHEMA = b.TABLE_SCHEMA
                                                           AND a.TABLE_NAME = b.TABLE_NAME
                                                           AND a.COLUMN_NAME = b.COLUMN_NAME
        LEFT JOIN sys.sysobjects o ON o.name = b.CONSTRAINT_NAME
                                      AND o.xtype = 'PK'
        LEFT JOIN sys.extended_properties p ON OBJECT_ID(a.TABLE_SCHEMA + '.'
                                                         + a.TABLE_NAME) = p.major_id
                                               AND a.Ordinal_position = p.minor_id
                                               AND p.class_desc = 'OBJECT_OR_COLUMN'
        LEFT JOIN sys.extended_properties p1 ON OBJECT_ID(a.TABLE_SCHEMA + '.'
                                                          + a.TABLE_NAME) = p1.major_id
                                                AND p1.minor_id = 0
        LEFT JOIN SYS.foreign_key_columns f ON OBJECT_ID(a.TABLE_SCHEMA + '.'
                                                         + a.TABLE_NAME) = f.parent_object_id
                                               AND a.ORDINAL_POSITION = f.parent_column_id
WHERE   a.TABLE_NAME = 'Address'
-- a.TABLE_NAME IN (SELECT name FROM sys.tables)
ORDER BY a.TABLE_SCHEMA,a.TABLE_NAME, a.ORDINAL_POSITION

運轉後果如圖:

有纰謬的處所還請年夜家拍磚!感謝!
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved