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 8410777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:16:03+00:00 2026-06-10T00:16:03+00:00

I have a table with 2 foreign keys. I’m somewhat new to MySQL, can

  • 0

I have a table with 2 foreign keys. I’m somewhat new to MySQL, can someone tell me which is the right way in applying an INDEX to tables?

# Sample 1
CREATE  TABLE IF NOT EXISTS `my_table` (
  `topic_id` INT UNSIGNED NOT NULL ,
  `course_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`topic_id`, `course_id`) ,
  INDEX `topic_id_idx` (`topic_id` ASC) ,
  INDEX `course_id_idx` (`course_id` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

# Sample 2
CREATE  TABLE IF NOT EXISTS `my_table` (
  `topic_id` INT UNSIGNED NOT NULL ,
  `course_id` INT UNSIGNED NOT NULL ,
  PRIMARY KEY (`topic_id`, `course_id`) ,
  INDEX `topic_id_idx` (`topic_id`, `course_id`) ,
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

I guess what I’m really asking is what’s the difference between defining both as separate indexes and the other as combined?

  • 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-06-10T00:16:04+00:00Added an answer on June 10, 2026 at 12:16 am

    The reason why you might want one of these over the other has to do with how you plan on querying the data. Getting this determination right can be a bit of trick.

    Think of the combined key in terms of, for example, looking up a student’s folder in a filing cabinet, first by the student’s last name, and then by their first name.

    Now, in the case of the two single indexes in your example, you could imagine, in the student example, having two different sets of organized folders, one with every first name in order, and another with ever last name in order. In this case, you’ll always have to work through the greatest amount of similar records, but that doesn’t matter so much if you only have one name or the other anyway. In such a case, this arrangement gives you the greatest flexibility while still only maintaining indexes over two columns.

    In contrast, if given both first and last name, it’s a lot easier for us as humans to look up a student first by last name, then by first name (within a smaller set of potentials). However, when the last name is not known, it makes it very difficult to find the student by first name alone because the students with the same first-name are potential interleaved with every veration of last name (table scan). This is all true for the algorithms the computer uses to look up the information too.

    So, as a rule of thumb, add the extra key to a single index if you are going to be filtering the data by both values at once. If at times you will have one and not the other, make sure which ever value that is, it’s the leftmost key in the index. If the value could be either, you’ll probably want both indexes (one of these could actually have both key for the best of both words, but even that comes at a cost in terms of writes). Getting this stuff right can be pretty important, as this often amounts to an all or nothing game. If all the data the dbms requires to preform the indexed lookup isn’t present, it will probably resort to a table scan. Mysql’s explain feature is one tool which can be helpful in checking your configuration and identifying optimizations.

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

Sidebar

Related Questions

I have a table called user_relationship. which has two foreign keys refering back to
Hi have a table named category which has two foreign keys to the table
I have a table which consists of 2 foreign keys. And those are only
I have a table which is referenced by foreign keys on many other tables.
I use MySQL 5.1. Have a table with 2 foreign keys, referencing 2 different
I have a relational table with two foreign-keys pointing to same table. The first
I have a table, tblNoComp, that has two columns, both foreign keys pointing to
I have innoDB table using numerous foreign keys, but we just want to look
I have a table with two foreign keys in one table. Table PROJECTS -
I have a table with 2 foreign keys, lets call them fk1 and fk2.

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.