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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:03:59+00:00 2026-06-06T22:03:59+00:00

What is the difference in the following two CREATE TABLE statements? (The first one

  • 0

What is the difference in the following two CREATE TABLE statements? (The first one uses KEY and the second one does not.)

CREATE TABLE `title` (
    `title` VARCHAR(255) NOT NULL,
    `order_number` VARCHAR(35) NOT NULL,
    KEY `order_number` (`order_number`),
    CONSTRAINT `order_number_fk` FOREIGN KEY (`order_number`)
        REFERENCES `order` (`order_number`) ON DELETE CASCADE
)

CREATE TABLE `title` (
    `title` VARCHAR(255) NOT NULL,
    `order_number` VARCHAR(35) NOT NULL,
    CONSTRAINT `order_number_fk` FOREIGN KEY (`order_number`)
        REFERENCES `order` (`order_number`) ON DELETE CASCADE
)

Both of them create valid tables. How are they different and which would I want to use?

  • 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-06T22:04:00+00:00Added an answer on June 6, 2026 at 10:04 pm

    They are (almost*) the same.

    When you create a foreign key constraint, an index is created on the relevant column(s) of the referencing table automatically if no suitable index already exists.

    From the manual page on FOREIGN KEY Constraints:

    InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.

    Emphasis mine.


    (*) I say almost the same because there a few subtle differences.

    The name of the index

    In the first version you have given the index an explicit name, but in the second version the name of the index is the same as the name of the constraint (if it is specified).

    Compare the output of SHOW INDEX in both cases:

    Version 1:

    Table  Non_unique  Key_name          Seq_in_index   Column_name   ...
    title  1           order_number      1              order_number  ...
    

    Version 2:

    Table  Non_unique  Key_name          Seq_in_index   Column_name   ...
    title  1           order_number_fk   1              order_number  ...
    

    As you can see, the only difference here is the name of the index.

    Silent dropping

    Another subtle difference is that in the second case, as the documentation mentions, the automatically created index could be silently dropped when new indexes are added:

    This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint.

    This means is that if you later create a multicolumn index on, for example, (order_number, title):

    CREATE INDEX ix_order_number_title ON title (order_number, title);
    

    Then run SHOW INDEX again:

    Version 1:

    Table  Non_unique  Key_name               Seq_in_index   Column_name   ...
    title  1           order_number           1              order_number  ...
    title  1           ix_order_number_title  1              order_number  ...
    title  1           ix_order_number_title  2              title         ...
    

    Version 2:

    Table  Non_unique  Key_name               Seq_in_index   Column_name   ...
    title  1           ix_order_number_title  1              order_number  ...
    title  1           ix_order_number_title  2              title         ...
    

    Now you can see that the first version has two indexes but the second version has only one. With the second version, the index that was automatically created by the foreign key constraint was automatically dropped again when the multi-column index was added. Normally this isn’t a serious issue because the new index makes the original index mostly redundant.

    Which would I want to use?

    Normally you don’t need to worry about explicitly creating the index on the referencing table of a foreign key constraint.

    But you might want to create an index explicitly if:

    • you prefer to give it a name that is different from the name of the constraint, or
    • you don’t want the index to silently disappear when other indexes are added.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following table definition: CREATE TABLE Customers( id INT NOT NULL PRIMARY KEY, name
Is there any difference between the following two examples and should one be preferred
What's the difference between the following two? The first 1 works but the 2nd
Is there a difference between the following two statements: mysql> EXPLAIN SELECT IF(arms IS
Consider the following SQL: CREATE TABLE USER1 ( pkUSER1_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
I have the following table structure: CREATE TABLE a ( a_id int(10) unsigned NOT
In the following table definition, what is the difference between these two column definitions,
Is there any difference between following two ways of creating an object. Student s1
What is difference between the following two function definitions? A 2D array is being
Is there a difference between the following two : ArrayList list = getData(); public

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.