2

我无法运行以下语句:

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”都是键,而我要删除的行不存在于子表中,因为它是不同的时间。

这里有什么问题?

谢谢!

4

1 回答 1

1

是的,但是您确实在子表或引用表中有记录,my_id='5146'因此它正确地抛出了该错误。如果您真的想删除该记录,请先从子表中删除它们,然后再从父表中删除它们。

您可以使用下面的级联选项创建FOREIGN KEY约束,其中也将级联到子表ON DELETE CASCADEdelete

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
于 2016-01-05T07:57:41.460 回答