如何将约束类型连接到列信息

时间:2015-05-10 23:17:37

标签: sql sql-server tsql join information-schema

在SQL Server中,对于给定的表,如何选择INFORMATION_SCHEMA.COLUMNS +列中包含该列constrain type的列(如果存在)。 一个例子:

+-------------+-------------+-----------+--------------------------+-----------------+
| COLUMN_NAME | IS_NULLABLE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CONSTRAINT_TYPE |
+-------------+-------------+-----------+--------------------------+-----------------+
| PersonID    | NO          | int       | NULL                     | PRIMARY KEY     |
| Name        | NO          | varchar   | 50                       |                 |
| Email       | NO          | varchar   | 50                       | UNIQUE          |
| Blog        | YES         | varchar   | 50                       |                 |
+-------------+-------------+-----------+--------------------------+-----------------+

我想我必须加入INFORMATION_SCHEMA.COLUMNSINFORMATION_SCHEMA.TABLE_CONSTRAINTSINFORMATION_SCHEMA.KEY_COLUMN_USAGE才能解决问题。

我试过

SELECT COL.COLUMN_NAME, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, C.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.COLUMNS COL
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C
ON COL.TABLE_NAME = C.TABLE_NAME
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K
ON COL.TABLE_NAME = K.TABLE_NAME AND COL.COLUMN_NAME = K.COLUMN_NAME
AND C.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG
AND C.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA
AND C.CONSTRAINT_NAME = K.CONSTRAINT_NAME
WHERE  COL.TABLE_NAME = 'Person';

但是它复制了表格中每个CONSTRAINT_TYPE的COLUMN_NAME,例如:

+-------------+-------------+-----------+--------------------------+-----------------+
| COLUMN_NAME | IS_NULLABLE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CONSTRAINT_TYPE |
+-------------+-------------+-----------+--------------------------+-----------------+
| PersonID    | NO          | int       | NULL                     | PRIMARY KEY     |
| PersonID    | NO          | int       | NULL                     | UNIQUE          |
| Name        | NO          | varchar   | 50                       | PRIMARY KEY     |
| Name        | NO          | varchar   | 50                       | UNIQUE          |
| Email       | NO          | varchar   | 50                       | PRIMARY KEY     |
| Email       | NO          | varchar   | 50                       | UNIQUE          |
| Blog        | YES         | varchar   | 50                       | PRIMARY KEY     |
| Blog        | YES         | varchar   | 50                       | UNIQUE          |
+-------------+-------------+-----------+--------------------------+-----------------+

1 个答案:

答案 0 :(得分:2)

您可以在CTE中单独生成两个查询并加入。

;WITH CTE AS(
    SELECT COL.COLUMN_NAME, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
    FROM INFORMATION_SCHEMA.COLUMNS AS COL
    WHERE COL.TABLE_NAME = 'Person'
)
,CTE2 AS(
    SELECT CC.COLUMN_NAME, C.CONSTRAINT_TYPE
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C
    INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CC ON C.CONSTRAINT_NAME = CC.CONSTRAINT_NAME
    where C.TABLE_NAME = 'Person'
)
SELECT CTE.*, CTE2.CONSTRAINT_TYPE
FROM CTE
LEFT JOIN CTE2 ON CTE2.COLUMN_NAME = CTE.COLUMN_NAME