We help IT Professionals succeed at work.
Get Started

In SQLServer2012, are explicit Indexes needed in addition to Foreign Key Constraints?

RCUllrich
RCUllrich asked
on
491 Views
Last Modified: 2012-07-17
I am in the process of moving several Access backend databases to SQLServer 2012. I have scripts that will create the tables and define the primary key and any foreign keys. Do I also need to add an index on the Foreign Key or is that automatically built when I add the CONSTRAINT for the FOREIGN KEY?
Here is the syntax of the Create Table command:

CREATE TABLE [dbo].[AssetGroup] (
    [asset_grp_num]      INT                      IDENTITY (1, 1) NOT NULL,
    [asset_grp_name]     VARCHAR (255) NOT NULL,
    [is_inactive]              BIT                       DEFAULT ((0)) NOT NULL,
    [emp_id]                   INT                       NOT NULL,
    [mod_dt]                  DATETIME            DEFAULT (GetDate()) NOT NULL,
    CONSTRAINT [PK_AssetGroup] PRIMARY KEY CLUSTERED ([asset_grp_num] ASC),
    CONSTRAINT [FK_AssetGroup_Employee] FOREIGN KEY ([emp_id]) REFERENCES [dbo].[Employee] ([emp_id])
);

Here is the syntax of the Index that is related to Foreign Key defined above:

CREATE NONCLUSTERED INDEX [IX_AssetGroupEmployee]
  ON [dbo].[AssetGroup]([emp_id] ASC);

I am assuming that the index on the foreign key would only be needed if this index was needed to optimize queries. Or is it a good practice to always add an index for any Foreign Key?
Thanks,
Bob
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE