程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 探討SQL利用INFORMATION_SCHEMA系統視圖如何獲取表的主外鍵信息

探討SQL利用INFORMATION_SCHEMA系統視圖如何獲取表的主外鍵信息

編輯:MySQL綜合教程

接著上篇文章《解析SQL 表結構信息查詢 含主外鍵、自增長》裡面提到了INFORMATION_SCHEMA視圖,其實到了SQL 2005微軟都主推大家使用INFORMATION_SCHEMA系統視圖,而不是在使用sys東東了,當然目前還是有許多信息只能通過sys視圖來查詢。這裡我們還是以查詢表結果信息為例來說明一些主要的INFORMATION_SCHEMA視圖的使用。
首先我們需要查詢列的信息,這需要用到[INFORMATION_SCHEMA].[COLUMNS]系統視圖來查詢數據列的信息,SQL 如下:
復制代碼 代碼如下:
SELECT  c.TABLE_SCHEMA ,
        c.TABLE_NAME ,
        c.COLUMN_NAME ,
        c.DATA_TYPE ,
        c.CHARACTER_MAXIMUM_LENGTH ,
        c.COLUMN_DEFAULT ,
        c.IS_NULLABLE ,
        c.NUMERIC_PRECISION ,
        c.NUMERIC_SCALE
FROM    [INFORMATION_SCHEMA].[COLUMNS] c
WHERE   TABLE_NAME = 'Address'

運行結果如下:

我們都知道我們在定義列的時候一般的使用都是varchar(50)之類的信息,這裡我們需要整合DATA_TYPE和CHARACTER_MAXIMUM_LENGTH信息,當CHARACTER_MAXIMUM_LENGTH為-1時即使說沒有指定具體最大長度,數據的指定長度信息是max,而numeric需要整合NUMERIC_PRECISION、NUMERIC_SCALE信息。修改後的SQL如下:
復制代碼 代碼如下:
SELECT  c.TABLE_SCHEMA ,
        c.TABLE_NAME ,
        c.COLUMN_NAME ,
        CASE WHEN ( ( CHARINDEX('char', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH <> -1
                  )
             THEN c.DATA_TYPE + '('
                  + CAST(c.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(4)) + ')'
             WHEN ( ( CHARINDEX('CHAR', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH = -1
                  ) THEN c.DATA_TYPE + '(max)'
             WHEN ( CHARINDEX('numeric', c.DATA_TYPE) > 0 )
             THEN c.DATA_TYPE + '(' + CAST(c.NUMERIC_PRECISION AS VARCHAR(4))
                  + ',' + CAST(c.NUMERIC_SCALE AS VARCHAR(4)) + ')'
             ELSE c.DATA_TYPE
        END AS DATA_TYPE ,
        c.COLUMN_DEFAULT ,
        c.IS_NULLABLE ,
        c.COLUMN_DEFAULT
FROM    [INFORMATION_SCHEMA].[COLUMNS] c
WHERE   TABLE_NAME = 'Address'
ORDER BY c.ORDINAL_POSITION

運行結果如圖:

現在我們需要標記這張表的那些列是主鍵,那些列是外鍵,要查詢表的主、外鍵信息需要用到[INFORMATION_SCHEMA].[TABLE_CONSTRAINTS]和[INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] 系統視圖
運行結果如圖:

修改我們先前的SQL語句:
復制代碼 代碼如下:
SELECT  c.TABLE_SCHEMA ,
        c.TABLE_NAME ,
        c.COLUMN_NAME ,
        CASE WHEN ( ( CHARINDEX('char', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH <> -1
                  )
             THEN c.DATA_TYPE + '('
                  + CAST(c.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(4)) + ')'
             WHEN ( ( CHARINDEX('CHAR', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH = -1
                  ) THEN c.DATA_TYPE + '(max)'
             WHEN ( CHARINDEX('numeric', c.DATA_TYPE) > 0 )
             THEN c.DATA_TYPE + '(' + CAST(c.NUMERIC_PRECISION AS VARCHAR(4))
                  + ',' + CAST(c.NUMERIC_SCALE AS VARCHAR(4)) + ')'
             ELSE c.DATA_TYPE
        END AS DATA_TYPE ,
        c.COLUMN_DEFAULT ,
        c.IS_NULLABLE ,
        c.COLUMN_DEFAULT ,
        CASE WHEN tc.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 'YES'
             ELSE 'NO'
        END AS IS_PRIMARY_KEY ,
        CASE WHEN tc.CONSTRAINT_TYPE = 'FOREIGN KEY' THEN 'YES'
             ELSE 'NO'
        END AS IS_FOREIGN_KEY
FROM    [INFORMATION_SCHEMA].[COLUMNS] c
        LEFT JOIN [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
                                                              AND kcu.TABLE_NAME = c.TABLE_NAME
                                                              AND kcu.COLUMN_NAME = c.COLUMN_NAME
        LEFT JOIN [INFORMATION_SCHEMA].[TABLE_CONSTRAINTS] tc ON tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
                                                              AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE   c.TABLE_NAME = 'Address'
ORDER BY c.ORDINAL_POSITION

運行結果如圖:

現在我們已經知道那些列是主鍵那些是外鍵,接下來的就是外鍵列所關聯的外檢表信息,這裡需要用到[INFORMATION_SCHEMA].[REFERENTIAL_CONSTRAINTS]系統視圖,運行該視圖如下:

所以修改我們的SQL如下:
復制代碼 代碼如下:
SELECT  c.TABLE_SCHEMA ,
        c.TABLE_NAME ,
        c.COLUMN_NAME ,
        CASE WHEN ( ( CHARINDEX('char', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH <> -1
                  )
             THEN c.DATA_TYPE + '('
                  + CAST(c.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(4)) + ')'
             WHEN ( ( CHARINDEX('CHAR', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH = -1
                  ) THEN c.DATA_TYPE + '(max)'
             WHEN ( CHARINDEX('numeric', c.DATA_TYPE) > 0 )
             THEN c.DATA_TYPE + '(' + CAST(c.NUMERIC_PRECISION AS VARCHAR(4))
                  + ',' + CAST(c.NUMERIC_SCALE AS VARCHAR(4)) + ')'
             ELSE c.DATA_TYPE
        END AS DATA_TYPE ,
        c.COLUMN_DEFAULT ,
        c.IS_NULLABLE ,
        c.COLUMN_DEFAULT ,
        CASE WHEN tc.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 'YES'
             ELSE 'NO'
        END AS IS_PRIMARY_KEY ,
        CASE WHEN tc.CONSTRAINT_TYPE = 'FOREIGN KEY' THEN 'YES'
             ELSE 'NO'
        END AS IS_FOREIGN_KEY,
        fkcu.COLUMN_NAME AS FOREIGN_KEY,
        fkcu.TABLE_NAME AS FOREIGN_TABLE
FROM    [INFORMATION_SCHEMA].[COLUMNS] c
        LEFT JOIN [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
                                                              AND kcu.TABLE_NAME = c.TABLE_NAME
                                                              AND kcu.COLUMN_NAME = c.COLUMN_NAME
        LEFT JOIN [INFORMATION_SCHEMA].[TABLE_CONSTRAINTS] tc ON tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
                                                              AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
        LEFT JOIN [INFORMATION_SCHEMA].[REFERENTIAL_CONSTRAINTS] fc ON kcu.CONSTRAINT_SCHEMA = fc.CONSTRAINT_SCHEMA
                                                              AND kcu.CONSTRAINT_NAME = fc.CONSTRAINT_NAME
        LEFT JOIN [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] fkcu ON fkcu.CONSTRAINT_SCHEMA = fc.UNIQUE_CONSTRAINT_SCHEMA
                                                              AND fkcu.CONSTRAINT_NAME = fc.UNIQUE_CONSTRAINT_NAME
WHERE   c.TABLE_NAME = 'Address'
ORDER BY c.ORDINAL_POSITION

運行結果如圖:

我們的查詢結果中顯示了太多的NULL,看著不怎麼舒服,還有我們的表名應該顯示一次就可以,修改SQL如下:
復制代碼 代碼如下:
SELECT  CASE WHEN c.ORDINAL_POSITION = 1
             THEN c.TABLE_SCHEMA + '.' + c.TABLE_NAME
             ELSE ''
        END AS TABLE_NAME ,
        c.COLUMN_NAME ,
        CASE WHEN ( ( CHARINDEX('char', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH <> -1
                  )
             THEN c.DATA_TYPE + '('
                  + CAST(c.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(4)) + ')'
             WHEN ( ( CHARINDEX('CHAR', c.DATA_TYPE) > 0
                      OR CHARINDEX('binary', c.DATA_TYPE) > 0
                    )
                    AND c.CHARACTER_MAXIMUM_LENGTH = -1
                  ) THEN c.DATA_TYPE + '(max)'
             WHEN ( CHARINDEX('numeric', c.DATA_TYPE) > 0 )
             THEN c.DATA_TYPE + '(' + CAST(c.NUMERIC_PRECISION AS VARCHAR(4))
                  + ',' + CAST(c.NUMERIC_SCALE AS VARCHAR(4)) + ')'
             ELSE c.DATA_TYPE
        END AS DATA_TYPE ,
        ISNULL(c.COLUMN_DEFAULT, '') AS COLUMN_DEFAULT ,
        CASE WHEN c.IS_NULLABLE = 'YES' THEN '√'
             ELSE ''
        END IS_NULLABLE ,
        CASE WHEN tc.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN '√'
             ELSE ''
        END AS IS_PRIMARY_KEY ,
        CASE WHEN tc.CONSTRAINT_TYPE = 'FOREIGN KEY' THEN '√'
             ELSE ''
        END AS IS_FOREIGN_KEY ,
        ISNULL(fkcu.COLUMN_NAME, '') AS FOREIGN_KEY ,
        ISNULL(fkcu.TABLE_NAME, '') AS FOREIGN_TABLE
FROM    [INFORMATION_SCHEMA].[COLUMNS] c
        LEFT JOIN [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] kcu ON kcu.TABLE_SCHEMA = c.TABLE_SCHEMA
                                                              AND kcu.TABLE_NAME = c.TABLE_NAME
                                                              AND kcu.COLUMN_NAME = c.COLUMN_NAME
        LEFT JOIN [INFORMATION_SCHEMA].[TABLE_CONSTRAINTS] tc ON tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
                                                              AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
        LEFT JOIN [INFORMATION_SCHEMA].[REFERENTIAL_CONSTRAINTS] fc ON kcu.CONSTRAINT_SCHEMA = fc.CONSTRAINT_SCHEMA
                                                              AND kcu.CONSTRAINT_NAME = fc.CONSTRAINT_NAME
        LEFT JOIN [INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] fkcu ON fkcu.CONSTRAINT_SCHEMA = fc.UNIQUE_CONSTRAINT_SCHEMA
                                                              AND fkcu.CONSTRAINT_NAME = fc.UNIQUE_CONSTRAINT_NAME
WHERE   c.TABLE_NAME = 'Address'
ORDER BY c.ORDINAL_POSITION


運行結果如圖:

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved