微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息

MysqL必读探讨sql利用informatION_SCHEMA系统视图如何获取表的主外键信息》要点:
本文介绍了MysqL必读探讨sql利用informatION_SCHEMA系统视图如何获取表的主外键信息,希望对您有用。如果有疑问,可以联系我们。

接着上篇文章解析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'

运行结果如下:

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息


我们都知道我们在定义列的时候一般的使用都是varchar(50)之类的信息,这里我们需要整合DATA_TYPE和CHaraCTER_MAXIMUM_LENGTH信息,当CHaraCTER_MAXIMUM_LENGTH为-1时即使说没有指定具体最大长度,数据的指定长度信息是max,而numeric需要整合NUMERIC_PRECISION、NUMERIC_SCALE信息.修改后的sql如下:
代码如下:

SELECT  c.TABLE_SCHEMA,
        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
                    )
                    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
FROM    [informatION_SCHEMA].[COLUMNS] c
WHERE   TABLE_NAME = 'Address'
ORDER BY c.ORDINAL_POSITION

运行结果如图:

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息


现在我们需要标记这张表的那些列是主键,那些列是外键,要查询表的主、外键信息需要用到[informatION_SCHEMA].[TABLE_CONSTRAINTS]和[informatION_SCHEMA].[KEY_COLUMN_USAGE] 系统视图
运行结果如图:

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息


修改我们先前的sql语句:
代码如下:

SELECT  c.TABLE_SCHEMA,
        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

运行结果如图:

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息


现在我们已经知道那些列是主键那些是外键,接下来的就是外键列所关联的外检表信息,这里需要用到[informatION_SCHEMA].[referential_CONSTRAINTS]系统视图,运行该视图如下:

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息


所以修改我们的sql如下:
代码如下:

SELECT  c.TABLE_SCHEMA,
        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

运行结果如图:

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息


我们的查询结果中显示了太多的NULL,看着不怎么舒服,还有我们的表名应该显示一次就可以,修改sql如下:
代码如下:

SELECT  CASE WHEN c.ORDINAL_POSITION = 1
             THEN c.TABLE_SCHEMA + '.' + c.TABLE_NAME
             ELSE ''
        END AS TABLE_NAME,
        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


运行结果如图:

Mysql必读探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息


有不对的地方还请大家拍砖!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐