Avoid Misunderstanding ON DELETE NO ACTION

Relational databases often provide several options for handling actions when a referenced row in a foreign key relationship is deleted.
Among these options, ON DELETE NO ACTION can be a bit confusing, both in terms of its naming and its behavior.

Let…


This content originally appeared on DEV Community and was authored by JackTT

Relational databases often provide several options for handling actions when a referenced row in a foreign key relationship is deleted.
Among these options, ON DELETE NO ACTION can be a bit confusing, both in terms of its naming and its behavior.

Let's read my post to ensure that you understand it correctly.

What Are ON DELETE Options?

The ON DELETE clause in SQL allows you to define what should happen to the rows in a child table when the related row in the parent table is deleted. ON DELETE options include:

  • CASCADE: Automatically deletes child rows when the parent row is deleted.
  • SET NULL: Sets the foreign key value in child rows to NULL when the parent row is deleted.
  • RESTRICT: Prevents deletion of the parent row if any child rows exist.
  • NO ACTION: Similar to RESTRICT; the action is deferred until the end of the transaction.

Comparing ON DELETE NO ACTION and ON DELETE RESTRICT

ON DELETE NO ACTION and ON DELETE RESTRICT seem similar since both options ultimately prevent the deletion of the parent row if it has dependent rows in the child table. However, the difference i in the timing:

  • ON DELETE NO ACTION: The restriction is enforced at the end of the transaction. If any other statements within the transaction resolve the issue (like deleting the child rows), the delete action can proceed without raising an error.
  • ON DELETE RESTRICT: The restriction is enforced immediately, preventing the delete action as soon as it’s attempted, without waiting for the end of the transaction.

If you're using Golang, you can reference "ON DELETE NO ACTION" similarly to "defer RESTRICT".


This content originally appeared on DEV Community and was authored by JackTT


Print Share Comment Cite Upload Translate Updates
APA

JackTT | Sciencx (2024-10-06T02:34:34+00:00) Avoid Misunderstanding ON DELETE NO ACTION. Retrieved from https://www.scien.cx/2024/10/06/avoid-misunderstanding-on-delete-no-action/

MLA
" » Avoid Misunderstanding ON DELETE NO ACTION." JackTT | Sciencx - Sunday October 6, 2024, https://www.scien.cx/2024/10/06/avoid-misunderstanding-on-delete-no-action/
HARVARD
JackTT | Sciencx Sunday October 6, 2024 » Avoid Misunderstanding ON DELETE NO ACTION., viewed ,<https://www.scien.cx/2024/10/06/avoid-misunderstanding-on-delete-no-action/>
VANCOUVER
JackTT | Sciencx - » Avoid Misunderstanding ON DELETE NO ACTION. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/06/avoid-misunderstanding-on-delete-no-action/
CHICAGO
" » Avoid Misunderstanding ON DELETE NO ACTION." JackTT | Sciencx - Accessed . https://www.scien.cx/2024/10/06/avoid-misunderstanding-on-delete-no-action/
IEEE
" » Avoid Misunderstanding ON DELETE NO ACTION." JackTT | Sciencx [Online]. Available: https://www.scien.cx/2024/10/06/avoid-misunderstanding-on-delete-no-action/. [Accessed: ]
rf:citation
» Avoid Misunderstanding ON DELETE NO ACTION | JackTT | Sciencx | https://www.scien.cx/2024/10/06/avoid-misunderstanding-on-delete-no-action/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.