March 3, 2010 at 4:51 am
Folks..
I need a sql query which can give me the list of Foreign Key column names along with table name which does not have indexes in a database.
TIA
NBSql
March 3, 2010 at 5:57 pm
SQL 2005
Select s.name +'.'+ o.name as TableName,o2.name as FKName,c.name as FKColumn
From sys.foreign_key_columns fk
Inner Join sys.objects o
On o.object_id = fk.parent_object_id
Inner Join sys.schemas s
On s.schema_id = o.schema_id
Inner Join sys.objects o2
On o2.object_id = fk.constraint_object_id
Inner Join sys.columns c
On fk.parent_object_id = c.object_id
And fk.parent_column_id = c.column_id
Left Outer Join sys.index_columns ic
On ic.object_id = fk.parent_object_id
And ic.column_id = fk.parent_column_id
And ic.index_column_id = fk.constraint_column_id
Where ic.object_id is null
Order by o.name
SQL 2000 (but will work on 2005 still).
select o.name as TableName,c.name as FKColumn from sys.sysreferences r
Inner join sys.syscolumns c
on r.fkeyid = c.id
and r.fkey1 = c.colid
Inner Join sys.sysobjects o
on c.id = o.id
Left Outer Join sys.sysindexkeys k
on c.id = k.id
And c.colid = k.colid
Where k.id is null
Order by o.name
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 3, 2010 at 9:41 pm
March 3, 2010 at 10:04 pm
You're welcome
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
March 3, 2010 at 10:27 pm
One more question which i missed in Original Post..
Does it require to have an index on FK columns. can any one please post possitives and negatives of having an index on FK columns..
TIA...
March 3, 2010 at 11:44 pm
NewBeeSQL (3/3/2010)
One more question which i missed in Original Post..Does it require to have an index on FK columns. can any one please post possitives and negatives of having an index on FK columns..
TIA...
Here's an article on the subject
http://www.sqlservercentral.com/articles/T-SQL/68337/
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply