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