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

接着上篇文章《

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'

运行结果如下:

93bcf8c90bda09b1397dde5dcd91b1d2.png

我们都知道我们在定义列的时候一般的使用都是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

运行结果如图:

f4bb709f941a4e54eae6600379a801b5.png

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

运行结果如图:

13a06b4a76ed3a9e7fc3006682f1b72e.png

修改我们先前的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

运行结果如图:

dfaceeee8d54b56d55d0e00d1e24b640.png

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

646855254c0e0fec7faf41ae7570405e.png

所以修改我们的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

运行结果如图:

c309b369104c81326ec794b5fc758c58.png

我们的查询结果中显示了太多的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

运行结果如图:

bd478e951b0cf3e9c6bc963c95db99e4.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值