开发者

SQL Server - How to find if clustered index exists

开发者 https://www.devze.com 2023-04-08 00:44 出处:网络
We got 500+ tables and want to identify which tables doesn\'t have primary keys. Because creating index on large table will help to improve performance.

We got 500+ tables and want to identify which tables doesn't have primary keys. Because creating index on large table will help to improve performance.

Required Command - to identify table which are HEAPS (as they dont have clustered inde开发者_Python百科x)

Regards


SELECT OBJECT_NAME(object_id)
FROM sys.indexes 
WHERE index_id=0 
  AND OBJECTPROPERTY(object_id, 'IsUserTable') = 1

Finds all heaps. This issue is orthogonal to whether or not a PK exists though. A heap can have a non clustered PK and a clustered index isn't necessarily the PK. To find tables without a PK you can use.

SELECT *
FROM sys.tables t
WHERE NOT EXISTS
(
SELECT *
FROM sys.indexes i
WHERE is_primary_key=1 AND i.object_id = t.object_id
) 
0

精彩评论

暂无评论...
验证码 换一张
取 消