# SQL Server Rule: SRD0004
| | |
|----|----|
| Assembly | SqlServer.Rules |
| Namespace | SqlServer.Rules.Design |
| Class | ForeignKeysNeedIndexRule |
## Rule Information
| | |
|----|----|
| Id | SRD0004 |
| Friendly Name | Index on Foreign Key |
| Category | Design |
| Ignorable | false |
| Applicable Types | Foreign Key Constraint |
## Description
Columns on both sides of a foreign key should be indexed.
## Summary
Consider indexing columns on both sides of a foreign key relationship.
### Examples
```sql
Create Table dbo.Parent
(
ParentId integer primary key,
SomeData char(1)
)
go
Create Table dbo.Child
(
ChildID integer primary key,
ParentId integer constraint fkParentChild references dbo.Parent(ParentID),
SomeData char(1)
)
go
alter table dbo.Child nocheck constraint fkParentChild
-- SML006, SRD0004
```
### Remarks
The rule checks for not indexed foreign keys in the current database. Create an index on any
foreign key as the foreign keys are used in joins almost always benefit from having an index.
It is better to create indexes on all foreign keys, despite the possible overhead of
maintaining unneeded indexes than not to have index when needed.
<sub><sup>Generated by a tool</sup></sub>