最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

解析SQL表結(jié)構(gòu)信息查詢含主外鍵、自增長

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-09 21:08:01
文檔

解析SQL表結(jié)構(gòu)信息查詢含主外鍵、自增長

解析SQL表結(jié)構(gòu)信息查詢含主外鍵、自增長:最近項目需要做什么數(shù)據(jù)字典,需要表結(jié)構(gòu)信息。在網(wǎng)上看了許多關(guān)于表結(jié)構(gòu)信息的查詢,感覺都不怎么樣。相對好一點(diǎn)就是《基于SQL2005 SQL2008 表結(jié)構(gòu)信息查詢升級版的詳解(含外鍵信息)》 ,但是這里有一點(diǎn)小問題,缺少一個過濾以致運(yùn)行有一點(diǎn)小bug。在Ad
推薦度:
導(dǎo)讀解析SQL表結(jié)構(gòu)信息查詢含主外鍵、自增長:最近項目需要做什么數(shù)據(jù)字典,需要表結(jié)構(gòu)信息。在網(wǎng)上看了許多關(guān)于表結(jié)構(gòu)信息的查詢,感覺都不怎么樣。相對好一點(diǎn)就是《基于SQL2005 SQL2008 表結(jié)構(gòu)信息查詢升級版的詳解(含外鍵信息)》 ,但是這里有一點(diǎn)小問題,缺少一個過濾以致運(yùn)行有一點(diǎn)小bug。在Ad

最近項目需要做什么數(shù)據(jù)字典,需要表結(jié)構(gòu)信息。在網(wǎng)上看了許多關(guān)于表結(jié)構(gòu)信息的查詢,感覺都不怎么樣。相對好一點(diǎn)就是《基于SQL2005 SQL2008 表結(jié)構(gòu)信息查詢升級版的詳解(含外鍵信息)》 ,但是這里有一點(diǎn)小問題,缺少一個過濾以致運(yùn)行有一點(diǎn)小bug。在AdventureWorks2012數(shù)據(jù)庫中的Address表查詢結(jié)果如圖:

在查詢過濾中我們添加以下信息就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 ,
        標(biāo)識 = 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') ,
        小數(shù)位數(shù) = ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) ,
        允許空 = CASE WHEN a.isnullable = 1 THEN '√'
                   ELSE ''
              END ,
        默認(rèn)值 = ISNULL(e.text, '') ,
        字段說明 = ISNULL(g.[value], '') ,
        創(chuàng)建時間 = 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

運(yùn)行結(jié)果如圖:

我不怎么喜歡它的“類型”信息,一般的varchar都會有長度信息,還有這個查詢對于SQL 2012的新數(shù)據(jù)類型不支持,該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

運(yùn)行效果如圖:

有不對的地方還請大家拍磚!謝謝!

您可能感興趣的文章:

  • MySQL rownumber SQL生成自增長序號使用介紹
  • sql分類匯總及Select的自增長腳本
  • SQL Server設(shè)置主鍵自增長列(使用sql語句實(shí)現(xiàn))
  • sql自增長設(shè)置與刪除的深入分析
  • mysql修改自增長主鍵int類型為char類型示例
  • SQL Server 打開或關(guān)閉自增長
  • 聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    解析SQL表結(jié)構(gòu)信息查詢含主外鍵、自增長

    解析SQL表結(jié)構(gòu)信息查詢含主外鍵、自增長:最近項目需要做什么數(shù)據(jù)字典,需要表結(jié)構(gòu)信息。在網(wǎng)上看了許多關(guān)于表結(jié)構(gòu)信息的查詢,感覺都不怎么樣。相對好一點(diǎn)就是《基于SQL2005 SQL2008 表結(jié)構(gòu)信息查詢升級版的詳解(含外鍵信息)》 ,但是這里有一點(diǎn)小問題,缺少一個過濾以致運(yùn)行有一點(diǎn)小bug。在Ad
    推薦度:
    標(biāo)簽: 查詢 sql 查查詢
    • 熱門焦點(diǎn)

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top