database - MySQL InnoDB constraint does not work -
i stumble upon strange behavior innodb constraint, , cannot find cause of it.
have tables data.
below listed structures:
create table `contents` ( `id` int(10) unsigned not null auto_increment, `title` varchar(255) default null, primary key (`id`), key `title` (`title`) ) engine=innodb default charset=utf8; create table `fields` ( `id` int(10) unsigned not null auto_increment, `name` varchar(45) not null, `type` varchar(45) not null, primary key (`id`), unique key `nameunique` (`name`), key `name` (`name`), key `type` (`type`) ) engine=innodb default charset=utf8; create table `datatable` ( `id` bigint(20) unsigned not null auto_increment, `value` double not null, primary key (`id`), unique key `value_unique` (`value`) ) engine=innodb default charset=utf8; create table `content_data` ( `content_id` int(10) unsigned not null, `field_id` int(10) unsigned not null, `data_id` bigint(20) unsigned not null, primary key (`content_id`,`field_id`,`data_id`), key `fk_content_data_2_idx` (`field_id`), key `fk_content_data_3_idx` (`data_id`), constraint `fk_content_data_1` foreign key (`content_id`) references `contents` (`id`) on delete cascade on update cascade, constraint `fk_content_data_2` foreign key (`field_id`) references `fields` (`id`) on delete cascade on update cascade, constraint `fk_content_data_3` foreign key (`data_id`) references `datatable` (`id`) on delete cascade on update cascade ) engine=innodb default charset=utf8;
now, let's execute 2 queries:
first query:
mysql> select * `datatable` id=21318; empty set (0.00 sec)
we got empty set , ok since datatable
in fact has no row id=21318
second query:
mysql> select * `content_data` data_id=21318; +------------+----------+---------+ | content_id | field_id | data_id | +------------+----------+---------+ | 552 | 35 | 21318 | +------------+----------+---------+ 1 row in set (0.00 sec)
here, last query give result data_id=21318. wtf!
how explain situation?
why constraint not work?
idea, thanks.
if call made
set foreign_key_checks=0;
then fk checks turned off. knows, may state of system right now. have them turned on with
set foreign_key_checks=1;
note following. turning checks on not re-validate referential integrity. 1 needs alter table
that.
simply publishing schema little safeguarded.
meaning, turn off constraints, use system, delete data, load data infile (in short mess data), run off stackoverflow schema , "gosh how did happen".
and doesn't matter state system in now. matters in then.
Comments
Post a Comment