我无法运行以下声明:
delete from UT_Session where my_id='5146' and upgrade_time='2016-01-03 17:25:18'
因为我得到了:
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`Upgrade_tool`.`pre_tasks`, CONSTRAINT `pre_tasks_ibfk_1` FOREIGN KEY (`my_id`) REFERENCES `UT_Session` (`my_id`))
有表格:
create table UT_Session
(
my_id int not null,
upgrade_time DATETIME not null,
ownerName varchar(20),
source varchar(20) not null,
target varchar(20) not null,
isClosed boolean,
primary key (my_id,upgrade_time)
)ENGINE=INNODB;
CREATE INDEX upgrate_time_index
ON UT_Session (upgrade_time);
create table pre_tasks
(
my_id int,
foreign key (my_id) references UT_Session(my_id),
upgrade_time DATETIME not null,
foreign key (upgrade_time) references UT_Session(upgrade_time),
pre_task_type varchar (100),
reason varchar(500),
primary key (my_id,pre_task_type,upgrade_time)
)ENGINE=INNODB;
create table post_tasks
(
my_id int,
foreign key (my_id) references UT_Session(my_id),
upgrade_time DATETIME not null,
foreign key (upgrade_time) references UT_Session(upgrade_time),
post_task_type varchar (100),
reason varchar(500),
primary key (my_id,post_task_type,upgrade_time)
)ENGINE=INNODB;
现在,当我运行此查询以显示表的内容时,我得到了这个:
mysql> select * from UT_Session where my_id='5146';
+------+---------------------+-----------+----------+----------+----------+
| my_id | upgrade_time | ownerName | source | target | isClosed |
+------+---------------------+-----------+----------+----------+----------+
| 5146 | 2016-01-03 17:25:18 | Ronen | 5.1.2.12 | 5.2.2.26 | 0 |
| 5146 | 2016-01-03 17:35:10 | Ronen | 5.1.2.12 | 5.2.2.26 | 0 |
| 5146 | 2016-01-03 17:36:57 | Ronen | 5.1.2.12 | 5.2.2.26 | 1 |
+------+---------------------+-----------+----------+----------+----------+
mysql> select * from pre_tasks where my_id='5146';
+------+---------------------+--------------------------------------------------------+--------+
| my_id | upgrade_time | pre_task_type | reason |
+------+---------------------+--------------------------------------------------------+--------+
| 5146 | 2016-01-03 17:36:57 | Type 87954r0f | |
| 5146 | 2016-01-03 17:36:57 | Type 1a79F4rf | |
+------+---------------------+--------------------------------------------------------+--------+
mysql> select * from post_tasks where my_id='5146';
+------+---------------------+--------------------------------------------------------+--------+
| my_id | upgrade_time | post_task_type | reason |
+------+---------------------+--------------------------------------------------------+--------+
| 5146 | 2016-01-03 17:36:57 | Type v7d54r8f | |
+------+---------------------+--------------------------------------------------------+--------+
正如您所看到的那样,“my_id”和“upgrade_time”都是键,并且我要删除的行不存在于子表中,因为它是一个不同的时间。
这里有什么问题?
谢谢!
答案 0 :(得分:1)
是的,但您确实在子级或引用表中有my_id='5146'
的记录,因此它正确地抛出了该错误。如果您确实要删除该记录,请先从子表中删除它们,然后从父目录中删除它们。
您可以使用FOREIGN KEY
级联选项创建ON DELETE CASCADE
约束,如下所示,其中delete
也会级联到子表
foreign key (my_id) references UT_Session(my_id) on delete cascade
您实际上要做的是创建一个复合外键,如下所示;在这种情况下,记录的唯一性将取决于密钥。
FOREIGN KEY (my_id, upgrade_time)
REFERENCES UT_Session (my_id, upgrade_time) ON DELETE CASCADE