How can I add ON DELETE constraint on the table?
Constraints can be added to a new table or to an existing table. To add a unique or primary key, a referential constraint, or a check constraint, use the CREATE TABLE or the ALTER TABLE statement. To remove a constraint, use the ALTER TABLE statement.
The constraint can be created within the CREATE TABLE T-SQL command while creating the table or added using ALTER TABLE T-SQL command after creating the table. Adding the constraint after creating the table, the existing data will be checked for the constraint rule before creating that constraint.
Use ALTER TABLE+ADD CONSTRAINT. E.g. if you want to link tables members and profiles by member_id and cascade delete profiles each time the member is deleted, you can write something like this:
ALTER TABLE profiles ADD CONSTRAINT `fk_test` FOREIGN KEY (`member_id` ) REFERENCES `members` (`member_id` ) ON DELETE CASCADE
If you will need to update that constraint - you'll have to remove it at then create again, there's no direct way to alter it.
ALTER TABLE profiles DROP FOREIGN KEY `fk_test`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With