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

  • Home
  • SEARCH
  • 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 3226490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:27:46+00:00 2026-05-17T16:27:46+00:00

I have a database created with a GUI tool and I’ve noticed what appears

  • 0

I have a database created with a GUI tool and I’ve noticed what appears to be an inconsistent use of KEY (aka INDEX) definitions:

CREATE TABLE `foo_bar` (
  `foo_id` int(10) unsigned NOT NULL,
  `bar_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`foo_id`, `bar_id`),
  KEY `foo_bar_fk2` (`bar_id`), -- <== ???
  CONSTRAINT `foo_bar_fk1` FOREIGN KEY (`foo_id`) REFERENCES `foo` (`foo_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `foo_bar_fk2` FOREIGN KEY (`bar_id`) REFERENCES `bar` (`bar_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci COMMENT='Links between Foo and Bar';

I have the following questions about indexes:

  1. Is it necessary to explicitly define indexes for primary and foreign keys?
  2. If it’s not, do you actually get two indexes (and less performance)?
  3. Is it different in InnoDB and MyISAM (foreign keys apart)?
  • 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-17T16:27:47+00:00Added an answer on May 17, 2026 at 4:27 pm

    I’ve been doing some experiments on what you told me and I’d thought I’d share it as answer.

    First I create some test tables:

    CREATE TABLE foo (
        foo_id int(10) unsigned NOT NULL,
        PRIMARY KEY (foo_id)
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
    CREATE TABLE bar (
        bar_id int(10) unsigned NOT NULL,
        PRIMARY KEY (bar_id)
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
    
    
    CREATE TABLE foo_bar (
        foo_id int(10) unsigned NOT NULL,
        bar_id int(10) unsigned NOT NULL
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
    

    So far, no indexes exists:

    mysql> SHOW INDEXES FROM foo_bar;
    Empty set (0.00 sec)
    

    Adding a primary key generates an index:

    mysql> ALTER TABLE foo_bar
        -> ADD PRIMARY KEY (`foo_id`, `bar_id`);
    Query OK, 0 rows affected (0.70 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> SHOW INDEXES FROM foo_bar;
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | foo_bar |          0 | PRIMARY  |            1 | foo_id      | A         |           0 |     NULL | NULL   |      | BTREE      |         |
    | foo_bar |          0 | PRIMARY  |            2 | bar_id      | A         |           0 |     NULL | NULL   |      | BTREE      |         |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    2 rows in set (0.02 sec)
    

    If I add a foreign key on foo_id it reuses the primary key index since that column is the first one in the index:

    mysql> ALTER TABLE foo_bar
        -> ADD CONSTRAINT `foo_bar_fk1` FOREIGN KEY (`foo_id`) REFERENCES `foo` (`foo_id`) ON DELETE CASCADE ON UPDATE CASCADE;
    Query OK, 0 rows affected (0.27 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> SHOW INDEXES FROM foo_bar;
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | foo_bar |          0 | PRIMARY  |            1 | foo_id      | A         |           0 |     NULL | NULL   |      | BTREE      |         |
    | foo_bar |          0 | PRIMARY  |            2 | bar_id      | A         |           0 |     NULL | NULL   |      | BTREE      |         |
    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    2 rows in set (0.00 sec)
    

    If I add a foreign key on bar_id, it creates an index because no existing index can be reused:

    mysql> ALTER TABLE foo_bar
        -> ADD CONSTRAINT `foo_bar_fk2` FOREIGN KEY (`bar_id`) REFERENCES `bar` (`bar_id`) ON DELETE CASCADE ON UPDATE CASCADE;
    Query OK, 0 rows affected (0.25 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> SHOW INDEXES FROM foo_bar;
    +---------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table   | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +---------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | foo_bar |          0 | PRIMARY     |            1 | foo_id      | A         |           0 |     NULL | NULL   |      | BTREE      |         |
    | foo_bar |          0 | PRIMARY     |            2 | bar_id      | A         |           0 |     NULL | NULL   |      | BTREE      |         |
    | foo_bar |          1 | foo_bar_fk2 |            1 | bar_id      | A         |           0 |     NULL | NULL   |      | BTREE      |         |
    +---------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    3 rows in set (0.02 sec)
    

    One of our foreign keys is using the primary key index. That means that we cannot remove such index!

    mysql> ALTER TABLE foo_bar
        -> DROP PRIMARY KEY;
    ERROR 1025 (HY000): Error on rename of '.\test\#sql-568_c7d' to '.\test\foo_bar' (errno: 150)
    

    Unless we create an index for the foreign key or we drop the key itself:

    mysql> ALTER TABLE foo_bar
        -> DROP FOREIGN KEY `foo_bar_fk1`;
    Query OK, 0 rows affected (0.19 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> ALTER TABLE foo_bar
        -> DROP PRIMARY KEY;
    Query OK, 0 rows affected (0.23 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    

    The conclusion is that MySQL creates indexes automatically when they’re required for a functionality (but only if they are strictly necessary).

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

Sidebar

Related Questions

I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have designed database tables (normalised, on an MS SQL server) and created a
I have an Oracle database backup file (.dmp) that was created with expdp .
I have a regex created from a list in a database to match names
I have a batch file that was created for an Oracle database that I'm
I am trying to import an access database to mysql. I have created a
So I have the following user defined type in my oracle database: CREATE OR
I have a database in ISO-8859-2 format, but I need to create XML in
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
I have an Access database in which I drop the table and then create

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.