When to use ON UPDATE CASCADE

0 votes

I frequently use ON DELETE CASCADE, but I never use ON UPDATE CASCADE because I'm not sure when it will be useful.

Let's look at some code for the discussion's sake.

CREATE TABLE parent (
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id)
);

CREATE TABLE child (
    id INT NOT NULL AUTO_INCREMENT, parent_id INT,
    INDEX par_ind (parent_id),
    FOREIGN KEY (parent_id)
        REFERENCES parent(id)
        ON DELETE CASCADE
);

If a parent with an id is deleted for ON DELETE CASCADE, a record in the child with parent id = parent.id will also be erased automatically. There should be no issue with this.

  1. This implies that ON UPDATE CASCADE will act in the same way when the parent's ID is modified, right?
  2. If (1) is correct, there is no need to use ON UPDATE CASCADE if parent.id cannot be updated or will never be modified, such as when it is set to AUTO INCREMENT or is always set to TIMESTAMP. Is that accurate?
  3. What other circumstance should we utilize ON UPDATE CASCADE in if (2) is false?
  4. What happens if I update the child.parent id to something that doesn't already exist? Will it then be destroyed by default?

I am aware that some of the aforementioned questions can be tested programmatically for understanding, but I also want to know if any of this is dependant on the database vendor or not. Can someone please help me solve this?

Aug 25, 2022 in Database by Kithuzzz
• 38,000 points
1,453 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
It's true that ON UPDATE CASCADE wouldn't really be useful if your primary key was merely an identity value that was automatically increased.

However, suppose your primary key is a 10-digit UPC bar code and you need to alter it to a 13-digit UPC bar code because of expansion. If so, you could modify the primary key value using ON UPDATE CASCADE, and all tables that used the value as a foreign key reference would be updated correspondingly.

Regarding #4, you should get a foreign key error if you modify the child ID to a value that doesn't exist in the parent database (and you have referential integrity).

I hope this helps you.
answered Aug 26, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Database

0 votes
0 answers

How would I use excel to generate a large update sql statement?

I know there's a way to have ...READ MORE

Mar 30, 2022 in Database by Edureka
• 13,690 points
2,560 views
0 votes
0 answers

SQL Update from One Table to Another Based on a ID Match

Account and credit card numbers are stored ...READ MORE

Aug 22, 2022 in Database by Kithuzzz
• 38,000 points
1,448 views
0 votes
0 answers

Real life example, when to use OUTER / CROSS APPLY in SQL

With a colleague, I've been researching CROSS/OUTER ...READ MORE

Aug 25, 2022 in Database by Kithuzzz
• 38,000 points
991 views
0 votes
0 answers

I want to use CASE statement to update some records in sql server 2005

UPDATE dbo.TestStudents SET LASTNAME = ( ...READ MORE

Sep 2, 2022 in Database by Kithuzzz
• 38,000 points
1,322 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,250 points
1,410 views
0 votes
1 answer

How to install Oracle DB on Windows?

Installing Oracle DB is pretty easy wrt other ...READ MORE

answered Feb 28, 2019 in Database by Priyaj
• 58,020 points
1,630 views
0 votes
0 answers
0 votes
1 answer

INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server

Your table dbo.Sup Item Cat refers to ...READ MORE

answered Sep 19, 2022 in Database by narikkadan
• 63,600 points
9,125 views
0 votes
1 answer

Which query to use for better performance, join in SQL or using Dataset API?

DataFrames and SparkSQL performed almost about the ...READ MORE

answered Apr 19, 2018 in Apache Spark by kurt_cobain
• 9,350 points
2,104 views
0 votes
1 answer

How to connect Java program to the MySQL database?

You can connect your Java code with ...READ MORE

answered May 11, 2018 in Java by Parth
• 4,640 points
2,067 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP