In MySQL, a cascade update can be implemented using foreign keys with the "ON UPDATE CASCADE" option. This means that if a primary key is updated in the parent table, the corresponding foreign keys in the child table will automatically be updated to reflect that change.
-- Creating a parent table
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(50)
);
-- Creating a child table with foreign key referencing the parent table
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(id) ON UPDATE CASCADE
);
-- Inserting data into the parent table
INSERT INTO departments (id, name) VALUES (1, 'Human Resources'), (2, 'IT');
-- Inserting data into the child table
INSERT INTO employees (id, name, department_id) VALUES (1, 'John Doe', 1), (2, 'Jane Smith', 2);
-- Updating the primary key in the parent table
UPDATE departments SET id = 3 WHERE id = 1;
-- The corresponding foreign key in the child table will automatically be updated
-- You can check the updated employees table
SELECT * FROM employees;
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?