Listing indexes and constraints
These two queries may help you. The first will list all of the tables and indexes on those tables in your database. If the table does not appear in the list is does not have any indexes defined on it. These queries assume SQL Server version 2005 or newer.
SELECT
IndexName = QUOTENAME(I.name),
TableName =
QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
N'.' + QUOTENAME(T.name),
IsPrimaryKey = I.is_primary_key
FROM sys.indexes AS I
INNER JOIN sys.tables AS T
ON I.[object_id] = T.[object_id]
WHERE
I.type_desc <> N'HEAP'
ORDER BY
TableName ASC,
IndexName ASC;
The second query will report for each table the identity column, if any on each table in your database.
SELECT
TableName =
QUOTENAME(SCHEMA_NAME(T.[schema_id])) +
N'.' + QUOTENAME(T.name),
IdentityColumn = COALESCE(QUOTENAME(C.name), N'No identity column')
FROM sys.tables AS T
LEFT OUTER JOIN sys.columns AS C
ON T.[object_id] = C.[object_id]
AND C.is_identity = 1
ORDER BY
TableName ASC;
To limit the queries to a specific table add a WHERE
clause similar to:
WHERE T.name = N'NAME-OF-TABLE'
No, something is incorrect.
The check on sys.indexes
should return a row even if your table has no indexes. The heap still has a record in sys.indexes
with a type_desc
of 'HEAP' and type
of 0.
I think you probably need to make sure you are in the right database context since OBJECT_ID()
and sys.objects
are database-specific.
Try this:
USE MyDatabase
SELECT *
FROM sys.indexes
WHERE object_id = OBJECT_ID('schema.MyTableName')