"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > What is MySQL\'s Default ON DELETE Behavior?

What is MySQL\'s Default ON DELETE Behavior?

Posted on 2025-03-24
Browse:968

What is MySQL\'s Default ON DELETE Behavior?

MySQL's Default ON DELETE Behavior

When working with databases, understanding the behavior of foreign key constraints is crucial for maintaining data integrity. MySQL provides various options for specifying the action to be taken when a row in a parent table is deleted, one of which is the default behavior.

Default Behavior

MySQL's default ON DELETE behavior is to enforce referential integrity, preventing any change to the parent table that would break the relationship with child tables. This is achieved through the NO ACTION or RESTRICT options. Both these options essentially prohibit the deletion if dependent rows exist in child tables.

Other Options

MySQL offers additional options to handle ON DELETE events:

  • SET NULL: Deletes the parent row and sets the foreign key in the child table to NULL, if not declared NOT NULL.
  • CASCADE: Deletes the parent row and automatically deletes dependent rows in the child tables.
  • SET DEFAULT: This option is rejected by InnoDB and cannot be used.

Therefore, to answer the question:

  • Yes, the premise is correct. MySQL's default ON DELETE behavior is NO ACTION (or RESTRICT), preventing database changes that violate foreign key constraints.
  • NO ACTION and RESTRICT are synonymous, and they apply when no ON DELETE clause is specified.
  • SET NULL allows parent row deletion and sets foreign keys to NULL.
  • CASCADE deletes parent and dependent rows.
  • SET DEFAULT should not be used as it is rejected by InnoDB.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3