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)
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.
- This implies that ON UPDATE CASCADE will act in the same way when the parent's ID is modified, right?
- 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?
- What other circumstance should we utilize ON UPDATE CASCADE in if (2) is false?
- 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?