Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 629293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:43:48+00:00 2026-05-13T19:43:48+00:00

Seeing as a foreign key does not automatically create an index in SQL Server,

  • 0

Seeing as a foreign key does not automatically create an index in SQL Server, I want to create an explicit index on every FK field in my database. And I have over 100 tables in the schema…

So, does anyone have a ready packaged script that I could use to detect all FKs and create an index on each?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:43:48+00:00Added an answer on May 13, 2026 at 7:43 pm

    OK, I worked this out myself – here it is for everyone else’s benefit…

    select 'create index IX_'+c.name+'_'+p.name+' on '+c.name+'('+cf.name+');'
    from sysforeignkeys fk join sysobjects c on fk.fkeyid=c.id
    join sysobjects p on fk.rkeyid=p.id
    join syscolumns cf on c.id=cf.id and cf.colid = fk.fkey
    left join sysindexkeys k on k.id = cf.id and k.colid = cf.colid
    where k.id is null
    order by c.name
    

    It doesn’t work 100%, such as if you have two FKs on one table to the same primary table, but there are few enough instances of this (in my DB at least) that I could viably correct these by hand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

database management program: microsoft sql server management studio I was tasked to study a
SQL Server 2008 database design problem. I'm defining the architecture for a service where
I'm new to Oracle. I'm trying to create a table with a foreign key,
Seeing lots of SOAP/RDS stuff, but just want to dump some MySQL data and/or
I'm seeing my Ambient Data Framework code is entered on every http request, so
So, I've been seeing a lot of SQL examples on this site. I have
Seeing a checked expection in API is not rare, one of the most well
When seeing SQL code on the internet and in manuals there seems to vary
Seeing as new instances of value types are created every time they are passed
I've created a table with a unique index as well as a foreign index.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.