这篇文章主要为大家详细介绍了探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
接着上篇文章《解析SQL 表结构信息查询 含主外键、自增长》里面提到了INFORMATION_SCHEMA视图,其实到了SQL 2005微软都主推大家使用INFORMATION_SCHEMA系统视图,而不是在使用sys东东了,当然目前还是有许多信息只能通过sys视图来查询。这里我们还是以查询表结果信息为例来说明一些主要的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'
代码如下:
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
代码如下:
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
代码如下:
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
代码如下:
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
本文来自:http://www.q1010.com/177/9820-0.html
注:关于探讨SQL利用INFORMATION_SCHEMA系统视图如何获取表的主外键信息的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:MYSQL
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。