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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:08:31+00:00 2026-06-18T10:08:31+00:00

I was having a doubt regarding foreign key constraints. So I made two simple

  • 0

I was having a doubt regarding foreign key constraints. So I made two simple table like these for testing purposes.

mysql> EXPLAIN parent;
+-------+---------+------+-----+---------+----------------+         
| Field | Type    | Null | Key | Default | Extra          |      
+-------+---------+------+-----+---------+----------------+      
| id    | int(11) | NO   | PRI | NULL    | auto_increment |      
+-------+---------+------+-----+---------+----------------+      
1 row in set (0.01 sec)                                          

mysql> EXPLAIN child;                                            
+-----------+---------+------+-----+---------+----------------+  
| Field     | Type    | Null | Key | Default | Extra          |  
+-----------+---------+------+-----+---------+----------------+  
| id        | int(11) | NO   | PRI | NULL    | auto_increment |  
| parent_id | int(11) | NO   | MUL | NULL    |                |  
+-----------+---------+------+-----+---------+----------------+  

Now I did not specify the foreign key constraint at the time of creation of the table. I added it later as follows.

ALTER TABLE child                                                      
ADD CONSTRAINT parent_fk FOREIGN KEY(parent_id) REFERENCES parent(id); 

I checked the engine used for the tables and its InnoDB. My Question are…

  1. Why its not restricting me from deleting a record in parent on which there are several dependent child records? By default it should restrict me right?

  2. If I create an index on the foreign key field before I add the foreign key constraint, its working as expected. Do I need to create index like that each time?

  3. Is it a bad practice to add any kind of constraints after the creation of a table?
  • 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-18T10:08:32+00:00Added an answer on June 18, 2026 at 10:08 am

    MySQL requires indexes on fields when using ALTER TABLE to add new foreign key constraints. The constraint is checked against the index in the parent table, so without an index no check is made.

    14.6.4.4. FOREIGN KEY Constraints

    When you add a foreign key constraint to a table using ALTER TABLE, remember to create the required indexes first.

    And, also:

    Deviation from SQL standards: If there are several rows in the parent table that have the same referenced key value, InnoDB acts in foreign key checks as if the other parent rows with the same key value do not exist. For example, if you have defined a RESTRICT type constraint, and there is a child row with several parent rows, InnoDB does not permit the deletion of any of those parent rows.

    InnoDB performs cascading operations through a depth-first algorithm, based on records in the indexes corresponding to the foreign key constraints.

    To answer your specific questions:

    1) It’s not restricting you because you have not created the required indexes. As stated in their documentation, the algo uses the indexes corresponding to the constraint.

    2) See #1. Yes, unless the FK is in place before the rows are added to the parent table.

    3) No, it isn’t bad practice. You just need to be explicit with your intent. For example, do you want to check for referential integrity after ALTERing the table? Here is a potentially helpful question: Force InnoDB to recheck foreign keys on a table/tables?

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

Sidebar

Related Questions

I am novice in hibernate. i am having a doubt regarding transactions and flushing..
I'm having one doubt about the VIM ENCRYPTION key . I having a text
having this two classes I always have a doubt of which is the best
I am having following doubt regarding int flavors (unsigned int, long int, long long
I am having a doubt regarding a project structure. I am having a project
I am having a doubt regarding the Facebook policies. It is being stated that:
i m having a doubt regarding which one is better option to use ?
No doubt I'm doing something stupid, but I've been having problems running a simple
Hi does Apples iOS is having backward compatability? I'm having a doubt like in
I am having one doubt regarding the use of static variable in Asp.net pages.

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.