我可以使用一个SQL查询检查列PKey或UNIQUE约束 [英] Can I check whether a column PKey OR UNIQUE constraint using one sql query

查看:179
本文介绍了我可以使用一个SQL查询检查列PKey或UNIQUE约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在MSSQL中仅使用一个SQL查询来检查列PKey或UNIQUE约束吗?



如果是,请提供查询。

Can I check whether a column PKey OR UNIQUE constraint using only one sql query in MSSQL??

If so please give the query please.

推荐答案

如果我正确理解您的请求,您正在寻找一个查询,帮助您确定列何时在主键或唯一索引约束中。下面的查询将帮助您识别这些列。



If I'm understanding your request correctly, you are looking for a query that will help you to identify when a column is in a primary key or a unique index constraint. The query below will help you to identify those columns.

SELECT
	SCHEMA_NAME(tables.[schema_id]) AS [schema_name],
	tables.name,
	columns.name,
	indexes.name
FROM sys.tables
INNER JOIN sys.columns
	ON tables.[object_id] = columns.[object_id]
INNER JOIN sys.index_columns
	ON tables.[object_id] = index_columns.[object_id]
	AND columns.column_id = index_columns.column_id
INNER JOIN sys.indexes
	ON tables.[object_id] = indexes.[object_id]
	AND index_columns.index_id = indexes.index_id
WHERE
	indexes.is_unique = 1


这篇关于我可以使用一个SQL查询检查列PKey或UNIQUE约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆