29

I have a question of interest:

I have 2 tables in with InnoDb.
table tbl_a has a primary key, named a_id;
table tbl_b has a primary b_id and a foreign key on tbl_a.a_id with "ON DELETE NO ACTION".

+-------------+---------------+---------------+
|  Table Name |  Primary Key  |  Foreign Key  |
+-------------+---------------+---------------+
|    tbl_a    |     a_id      |               |
|    tbl_b    |     b_id      |     a_id      |
+-------------+---------------+---------------+

why should I still use InnoDb and foreign keys, if i don't really use the magic of foreign keys in the end in anyway?
Is there still a point of using
and foreign keys
instead of
and no foreign keys.
If I just do "NO ACTION" on deletes or updates?

I hope you got my point of interest :)

3
  • Doesn't it ensure that the foreign keys correspond to actual, valid rows? Aug 23, 2012 at 17:09
  • If you delete foreign row your data is not complete, and you are going to have big mess in DB.
    – Peter
    Aug 23, 2012 at 17:09
  • summary looks like, ON DELETE NO ACTION will stop invalid deletes/updates, so it is good to have Apr 28, 2021 at 3:37

2 Answers 2

93

I think you're misunderstanding what ON DELETE NO ACTION means. It does not mean to suppress the foreign-key constraint.

When you delete a record that is referred to by a foreign key, InnoDB has the ability to take an automatic action to rectify the situation:

  • it can CASCADE, meaning, delete the referring record. (This would make sense for something like user_address.user_id. If you hard-delete a user, you probably want to hard-delete all of the user's addresses as well.)
  • it can SET NULL, meaning, clear out the referring key. (This might make sense for something like file.last_modified_by. If you hard-delete a user, you might want the file's last-modified-by to become simply "unknown".)

If you specify NO ACTION, you're telling InnoDB that you don't want it to take either of these actions. So InnoDB can't fix the situation for you; all it can do is reject the DELETE and return an error.

As a result, ON DELETE NO ACTION is actually the same as ON DELETE RESTRICT (the default).

(Note: in some DBMSes, and in standard SQL, ON DELETE NO ACTION is a bit different from ON DELETE RESTRICT: in those, ON DELETE NO ACTION means "accept the DELETE within the current transaction, but reject the whole transaction if I try to commit it before rectifying the problem". But InnoDB doesn't support deferred checks, so it treats ON DELETE NO ACTION exactly the same as ON DELETE RESTRICT, and always rejects the DELETE immediately.)

See §§14.2.2.5 "FOREIGN KEY Constraints" and 13.1.17.2 "Using FOREIGN KEY Constraints" in the MySQL 5.6 Reference Manual.

10
  • ohhhh ok, thank you very much... well then I truly misunderstood "NO ACTION"
    – Preexo
    Aug 23, 2012 at 17:18
  • 1
    but why does phpmyadmin offer both: "NO ACTION" and "RESTRICT"? do they misunderstand it too?
    – Preexo
    Aug 23, 2012 at 17:23
  • @user1011116: Well, MySQL supports both notations, so I guess phpmyadmin feels that it should offer them both as well? And I could imagine a storage engine handling them differently -- some DBMSes do handle them slightly differently (as mentioned in the documentation I linked to) -- so perhaps phpmyadmin feels that it's more future-proof to let users choose.
    – ruakh
    Aug 23, 2012 at 17:35
  • 2
    I agree with the asker, here. "NO ACTION" does not communicate the same thing as "DEFAULT ACTION". To me, "NO ACTION" seems to indicate "don't do anything," which would also be nonsensical.
    – ryvantage
    Apr 17, 2014 at 18:09
  • @ryvantage: I'm not sure what the purpose of your comment is -- I'm not MySQL, so it doesn't make sense to argue with me about what it should mean -- but let me try to address it anyway . . . It's not that "NO ACTION" means "DEFAULT ACTION"; rather, it's that there's no default action. When you delete a row in a way that would violate the constraint, you can specify an action for MySQL to take to rectify the situation; but if you don't do so, then it won't take any action, so the statement continues to violate the constraint. So, it's rejected.
    – ruakh
    Apr 17, 2014 at 21:26
9

The foreign key constraint even without ON DELETE / UPDATE CASCADE ensures that if you insert a value into the child table, that it has a correctly matching value in the parent table (or is NULL if the FK column is nullable). Attempting to insert an invalid value into the child table's FK column would result in error when the constraint fails, so your data integrity is protected.

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

Defining the foreign key constraint also implicitly defines an index on the FK column in the child table, which although you could have manually defined the index, will improve joining performance.

ON DELETE NO ACTION (which is the same as omitting the ON DELETE clause) will actively prevent deletion of a parent row if it is referenced by any child table, not passively allow it to be deleted without affecting child rows.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.