What is the SQL FOREIGN KEY UPDATE CASCADE?
The SQL FOREIGN KEY UPDATE CASCADE make sure that any update on reference table will automatically update corresponding value on refering table.
SQL FOREIGN KEY UPDATE CASCADE Example:
Lets continue with our same example of having two tables in your database. One is 'order' table and another is 'customer' table where customer id on order table is a foreign key that refers back to customer table primary key column customer_id.
Sometimes, it is required to update the id of customer. In that event, you want to make sure that all orders associated to that customer remains as it is. To make sure that customer id automatically gets updated on order table, we need to define the operation "ON UPDATE CASCADE".
Use following syntax of sql foregin key on update cascade constraint at the time of creating table:
CREATE TABLE customer ( customer_id INT NOT NULL, name VARCHAR(50) NOT NULL, email VARCHAR(50), PRIMARY KEY(order_number) ); CREATE TABLE order( order_number INT NOT NULL, order_total DOUBLE(10,2) default '0.0', customer_id INT NOT NULL, PRIMARY KEY(order_number), FOREGIN KEY (customer_id) REFERENCES customer(customer_id) ON UPDATE CASCADE