Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the foreign key referential action? (behavior)

I have set up a table that contains a column with a foreign key, set to ON DELETE CASCADE (delete child when parent is deleted)

What would the SQL command be to change this to ON DELETE RESTRICT? (can't delete parent if it has children)

like image 406
Moak Avatar asked Jul 29 '10 03:07

Moak


People also ask

How do I change the reference of a foreign key in SQL?

To modify a foreign key. In Object Explorer, expand the table with the foreign key and then expand Keys. Right-click the foreign key to be modified and select Modify.

How do you modify a foreign key constraint?

Here is how you would do that: ALTER TABLE my_table ADD FOREIGN KEY (key) REFERENCES other_table(id) ON DELETE SET NULL; And that's it!! That's how you change a foreign key constraint in MySQL!

Can foreign key value be changed?

You can change the value in Table2, so long as the value you are changing it to is already contained in Table1. If you want to change it to a value which is not already contained in Table1, you can add the value to Table1 first, and then change the value in Table2 to that value.

How does foreign key maintain referential integrity?

Referential integrity is the state of a database in which all values of all foreign keys are valid. A foreign key is a column or a set of columns in a table whose values are required to match at least one primary key or unique key value of a row in its parent table.


1 Answers

Old question but adding answer so that one can get help

Its two step process:

Suppose, a table1 has a foreign key with column name fk_table2_id, with constraint name fk_name and table2 is referred table with key t2 (something like below in my diagram).

   table1 [ fk_table2_id ] --> table2 [t2] 

First step, DROP old CONSTRAINT: (reference)

ALTER TABLE `table1`  DROP FOREIGN KEY `fk_name`;   

notice constraint is deleted, column is not deleted

Second step, ADD new CONSTRAINT:

ALTER TABLE `table1`   ADD CONSTRAINT `fk_name`      FOREIGN KEY (`fk_table2_id`) REFERENCES `table2` (`t2`) ON DELETE CASCADE;   

adding constraint, column is already there

Example:

I have a UserDetails table refers to Users table:

mysql> SHOW CREATE TABLE UserDetails; : :  `User_id` int(11) DEFAULT NULL,   PRIMARY KEY (`Detail_id`),   KEY `FK_User_id` (`User_id`),   CONSTRAINT `FK_User_id` FOREIGN KEY (`User_id`) REFERENCES `Users` (`User_id`) : : 

First step:

mysql> ALTER TABLE `UserDetails` DROP FOREIGN KEY `FK_User_id`; Query OK, 1 row affected (0.07 sec)   

Second step:

mysql> ALTER TABLE `UserDetails` ADD CONSTRAINT `FK_User_id`      -> FOREIGN KEY (`User_id`) REFERENCES `Users` (`User_id`) ON DELETE CASCADE; Query OK, 1 row affected (0.02 sec)   

result:

mysql> SHOW CREATE TABLE UserDetails; : : `User_id` int(11) DEFAULT NULL,   PRIMARY KEY (`Detail_id`),   KEY `FK_User_id` (`User_id`),   CONSTRAINT `FK_User_id` FOREIGN KEY (`User_id`) REFERENCES                                         `Users` (`User_id`) ON DELETE CASCADE : 
like image 103
Grijesh Chauhan Avatar answered Sep 30 '22 03:09

Grijesh Chauhan