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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:22:05+00:00 2026-05-30T16:22:05+00:00

I use MYSQL to create a table with combined primary key. For example, col_a

  • 0

I use MYSQL to create a table with combined primary key. For example, col_a and col_b are used together as the key. I will use col_a and col_b to get a row often, and also will use col_a or col_b separately to query some rows.

My questions is, how will I create the index for it? Will I need only one composite index, or three indexes with two indexes for the separated columns?

I think MYSQL will create the index for primary key. But will it create a composite index for (col_a, col_b), or create an index for each column?

  • 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-30T16:22:07+00:00Added an answer on May 30, 2026 at 4:22 pm

    Multi-Column Indexes

    For a multi-column index, the index works from left to right.

    Given your primary key:

    (col_a, col_b)
    

    Here are the cases where it can be used:

    • col_a: YES. The index can be utilized when searching on col_a, since starting from left to right, col_a is the first column.
    • col_a & col_b: YES. When searching on col_a, and col_b, the index can also be utilized because starting from left to right, it’s col_a, then col_b.
    • col_b:: NO. When searching only for col_b, the index can’t be used because the first column in the index is col_a, and we can’t skip over columns.

    Given a larger index like this:

    (col_a, col_b, col_c)
    
    • col_a: YES
    • col_a & col_b: YES
    • col_a & col_b & col_c: YES
    • col_a & col_c: NO. Can’t skip over col_b in the index.
    • col_b:: NO. Can’t skip col_a.
    • col_c:: NO. Can’t skip col_a.
    • col_b & col_c: NO. Can’t skip over col_a in the index.

    Covering Indexes

    To utilize an index as a “covering” index, the left-to-right pattern still applies.

    Given the index (col_a, col_b), you can select both col_a and col_b, but search only on col_a (or col_a and col_b), and MySQL can use the index both for the search and for retrieving the data because col_a and col_b are both in the index. This saves a trip to the actual row data, since the values are stored in the index.

    Given the index (col_a, col_b, col_c), you could not SELECT col_a and col_c if you were searching only on col_a and be able to use the covering index to return col_c, since you can’t skip over col_b in the index. Instead, MySQL would visit the actual table data. However, you could SELECT col_a and col_c and use it as a covering index if you were searching on both col_a and col_b.

    Primary Keys in “Covering” Indexes

    Every secondary index has the primary key included in the index.

    A single column index, as it relates to a covering index, can be thought of as a multi-column index in the following pattern. Given the primary key (col_a, col_b), a single column index on col_b, really looks like this (col_b)(col_a, col_b).

    Given the above single column index, you could still select col_a and col_b, and search on col_b, and the index could be used as a covering index. Since the primary key includes col_a, MySQL could still use the index to return both columns without having to hit the actual table data.

    What if I want to search on col_b?

    Given the primary key (col_a, col_b), if you want to search on just col_b, you should add a single column index on col_b. As described above, it can still be used as a covering index if you are returning col_b and col_a.

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

Sidebar

Related Questions

When I use MySQL Workbench to create a simple table with a single primary
I have one mysql table 'alfa' that will contain the primary key of another
Sql: create table autori( id_autor integer primary key auto_increment, nume varchar(50) not null, prenume
I am trying to use PHP and MySQL's Create Table Select between two different
I've got MySQL table CREATE TABLE cms_webstat ( ID int NOT NULL auto_increment PRIMARY
I use the following mysql query, DELIMITER $$ DROP PROCEDURE IF EXISTS `allied`.`aboutus_delete`$$ CREATE
I use the following mysql procedure, DROP PROCEDURE IF EXISTS `allied`.`GetRegistrationData`$$ CREATE DEFINER=`allied`@`%` PROCEDURE
Let`s have a example hotels table: CREATE TABLE `hotels` ( `HotelNo` varchar(4) character set
I've a MySQL table folders: CREATE TABLE IF NOT EXISTS `folders` ( `id` int(11)
I installed MySql on my own machine. I created database, create table, ... using

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.