sql-server-2008 – 如何在SQL Server中获取具有复合主键的表列表?

前端之家收集整理的这篇文章主要介绍了sql-server-2008 – 如何在SQL Server中获取具有复合主键的表列表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何创建一个查询,该查询为我提供了sql Server中具有复合主键的表的列表?也许使用sys.tables或information_schema.tables或其他?

解决方法

您可以在information_schema.table_constraints和information_schema.constraint_column_usage中通过检查表上的多行PRIMARY KEY约束来挖掘该信息,例如:
SELECT col.table_name 
FROM information_schema.table_constraints tc 
JOIN information_schema.constraint_column_usage col
  ON col.constraint_name = tc.constraint_name
 AND col.table_name = tc.table_name
 AND tc.constraint_type = 'PRIMARY KEY '
GROUP BY col.table_name
HAVING COUNT(*) > 1

An SQLfiddle to test with.

原文链接:https://www.f2er.com/mssql/76301.html

猜你在找的MsSQL相关文章