服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - 探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息

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

2019-12-30 15:47MYSQL教程网 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'


运行结果如下:
探讨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 ,
        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


运行结果如图:
探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息 
现在我们需要标记这张表的那些列是主键,那些列是外键,要查询表的主、外键信息需要用到[INFORMATION_SCHEMA].[TABLE_CONSTRAINTS]和[INFORMATION_SCHEMA].[KEY_COLUMN_USAGE] 系统视图
运行结果如图:
探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息
修改我们先前的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


运行结果如图:
探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息
现在我们已经知道那些列是主键那些是外键,接下来的就是外键列所关联的外检表信息,这里需要用到[INFORMATION_SCHEMA].[REFERENTIAL_CONSTRAINTS]系统视图,运行该视图如下:
探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息
所以修改我们的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


运行结果如图:
探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息
我们的查询结果中显示了太多的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



运行结果如图:
探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息
有不对的地方还请大家拍砖!

延伸 · 阅读

精彩推荐
  • Mysql浅谈mysql 树形结构表设计与优化

    浅谈mysql 树形结构表设计与优化

    在诸多的管理类,办公类等系统中,树形结构展示随处可见,本文主要介绍了mysql 树形结构表设计与优化,具有一定的参考价值,感兴趣的小伙伴们可以参...

    小码农叔叔5242021-11-16
  • MysqlMySQL锁的知识点总结

    MySQL锁的知识点总结

    在本篇文章里小编给大家整理了关于MySQL锁的知识点总结以及实例内容,需要的朋友们学习下。...

    别人放弃我坚持吖4362020-12-14
  • MysqlERROR: Error in Log_event::read_log_event()

    ERROR: Error in Log_event::read_log_event()

    ERROR: Error in Log_event::read_log_event(): read error, data_len: 438, event_type: 2 ...

    MYSQL教程网6412020-03-13
  • Mysql解决MySQl查询不区分大小写的方法讲解

    解决MySQl查询不区分大小写的方法讲解

    今天小编就为大家分享一篇关于解决MySQl查询不区分大小写的方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起...

    Veir_dev5592019-06-25
  • Mysql详解MySQL中的分组查询与连接查询语句

    详解MySQL中的分组查询与连接查询语句

    这篇文章主要介绍了MySQL中的分组查询与连接查询语句,同时还介绍了一些统计函数的用法,需要的朋友可以参考下 ...

    GALAXY_ZMY5442020-06-03
  • Mysqlmysql 不能插入中文问题

    mysql 不能插入中文问题

    当向mysql5.5插入中文时,会出现类似错误 ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column ...

    MYSQL教程网5722019-11-25
  • MysqlMySQL数据库varchar的限制规则说明

    MySQL数据库varchar的限制规则说明

    本文我们主要介绍了MySQL数据库中varchar的限制规则,并以一个实际的例子对限制规则进行了说明,希望能够对您有所帮助。 ...

    mysql技术网4192019-11-23
  • MysqlMySQL 数据备份与还原的示例代码

    MySQL 数据备份与还原的示例代码

    这篇文章主要介绍了MySQL 数据备份与还原的相关知识,本文通过示例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...

    逆心2972019-06-23