я всегда думал что после включения FOREIGN_KEY_CHECKS база проверяет консистентность
разве нет?
разве нет?
Код:
CREATE TABLE IF NOT EXISTS `aaa` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `aab` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`aaa_id` int(11) NOT NULL,
`name` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `aaa_id` (`aaa_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `aab`
ADD CONSTRAINT `aab_ibfk_1` FOREIGN KEY (`aaa_id`) REFERENCES `aaa` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Код:
INSERT INTO `test`.`aab` (`id` ,`aaa_id` ,`name`)
VALUES (null, '1', 'aaa');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`aab`, CONSTRAINT `aab_ibfk_1` FOREIGN KEY (`aaa_id`) REFERENCES `aaa` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Код:
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO `test`.`aab` (`id` ,`aaa_id` ,`name`)
VALUES (null, '1', 'aaa');
SET FOREIGN_KEY_CHECKS=1;
select * from aab;
+----+--------+------+
| id | aaa_id | name |
+----+--------+------+
| 2 | 1 | 0 |
+----+--------+------+
1 row in set (0.00 sec)
mysql> select * from aaa;
Empty set (0.00 sec)