2

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?
Praveen Puglia
  • 5,577
  • 5
  • 34
  • 68
  • 1
    Default is `RESTRICT`, yes. Can you check with `SHOW CREATE TABLE child;` whether the FK has been actually defined (properly)? – ypercubeᵀᴹ Feb 05 '13 at 17:40
  • yes it is.. **KEY `parent_fk` (`parent_id`), CONSTRAINT `parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)** – Praveen Puglia Feb 05 '13 at 17:46

1 Answers1

4

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?

Community
  • 1
  • 1
Matthew
  • 10,244
  • 5
  • 49
  • 104