Reputation: 9858
I want to prevent deleting from parent table when he has children in other tables that.
I make like this
ALTER TABLE constant_det_tb
ADD CONSTRAINT fk_idparent
FOREIGN KEY (idparent)
REFERENCES constant_tb(id) ON DELETE RESTRICT
When I delete from parent constant_tb table, it delete the rows even the table has reference to another tables and it has records reference to it.
Upvotes: 4
Views: 2140
Reputation: 403
Checking of foreign keys could be disabled for current session. It can be enabled with
SET FOREIGN_KEY_CHECKS = 1;
Upvotes: 0
Reputation: 14638
Make sure you have InnoDB as storage engine for all affected tables.
Check this (if not already) : http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
ON DELETE RESTRICT
reference option is all you need to achieve this.
Upvotes: 3