ON DELETE CASCADE 删除额外的行
ON DELETE CASCADE deletes extra rows
我有以下表格:
CREATE TABLE workspace (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
uid VARCHAR(255),
name NVARCHAR(255)
);
CREATE TABLE userGroup (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
workspaceId INT,
FOREIGN KEY (workspaceId) REFERENCES workspace(id) ON DELETE CASCADE,
name NVARCHAR(255)
);
CREATE TABLE userWorkspaceMapping (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
workspaceId INT,
FOREIGN KEY (workspaceId) REFERENCES workspace(id) ON DELETE CASCADE,
userId INT,
FOREIGN KEY (userId) REFERENCES user(id),
userGroupId INT,
FOREIGN KEY (userGroupId) REFERENCES userGroup(id) ON DELETE CASCADE
);
userWorkspaceMapping
的数据:
+----+-------------+--------+-------------+
| id | workspaceId | userId | userGroupId |
+----+-------------+--------+-------------+
| 1 | 1 | 1 | 1 | <------- I want to delete only this row
| 2 | 2 | 1 | 3 |
| 3 | 3 | 1 | 5 |
+-----------------------------------------+
当我删除 workspace
时,userWorkspaceMapping
中的所有三行都被删除,而不仅仅是第一行。
delete from workspace
where id = 1;
为什么 userWorkspaceMapping
中的所有内容都被删除了?
这是 fiddle:https://www.db-fiddle.com/f/tHMZy2rnyvoHH13E7dS99A/1
您的 userGroup
table 还引用了带有 ON DELETE CASCADE 约束的 workspace
。因此,当您在工作区中删除一行时,userGroup
中引用它的所有行都将被删除。
这意味着第 1 组到第 5 组已删除。
mysql> delete from workspace where id = 1;
Query OK, 1 row affected (0.03 sec)
mysql>
mysql> select * from userGroup;
+----+-------------+------+
| id | workspaceId | name |
+----+-------------+------+
| 6 | 2 | b1 |
| 7 | 2 | b2 |
| 8 | 2 | b3 |
| 9 | 2 | b4 |
| 10 | 2 | b5 |
| 11 | 3 | c1 |
| 12 | 3 | c2 |
| 13 | 3 | c3 |
| 14 | 3 | c4 |
| 15 | 3 | c5 |
+----+-------------+------+
因此,userWorkspaceMapping
中引用组 1 到 5 的所有行都将被删除。这占所有行。
级联可以并且确实在外键引用中级联了不止一个“步骤”。
CASCADE
实际上没有问题...我在这里插入了错误的测试 ID:
INSERT INTO userWorkspaceMapping (
userId,
workspaceId,
userGroupId
)
VALUES
(1, 1, 1),
(1, 2, 3), -- <---- should be between 6 and 9
(1, 3, 5) -- <---- should be between 10 and 15
;
它们都属于已删除的工作区...ID 应该是正确的。
我有以下表格:
CREATE TABLE workspace (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
uid VARCHAR(255),
name NVARCHAR(255)
);
CREATE TABLE userGroup (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
workspaceId INT,
FOREIGN KEY (workspaceId) REFERENCES workspace(id) ON DELETE CASCADE,
name NVARCHAR(255)
);
CREATE TABLE userWorkspaceMapping (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
workspaceId INT,
FOREIGN KEY (workspaceId) REFERENCES workspace(id) ON DELETE CASCADE,
userId INT,
FOREIGN KEY (userId) REFERENCES user(id),
userGroupId INT,
FOREIGN KEY (userGroupId) REFERENCES userGroup(id) ON DELETE CASCADE
);
userWorkspaceMapping
的数据:
+----+-------------+--------+-------------+
| id | workspaceId | userId | userGroupId |
+----+-------------+--------+-------------+
| 1 | 1 | 1 | 1 | <------- I want to delete only this row
| 2 | 2 | 1 | 3 |
| 3 | 3 | 1 | 5 |
+-----------------------------------------+
当我删除 workspace
时,userWorkspaceMapping
中的所有三行都被删除,而不仅仅是第一行。
delete from workspace
where id = 1;
为什么 userWorkspaceMapping
中的所有内容都被删除了?
这是 fiddle:https://www.db-fiddle.com/f/tHMZy2rnyvoHH13E7dS99A/1
您的 userGroup
table 还引用了带有 ON DELETE CASCADE 约束的 workspace
。因此,当您在工作区中删除一行时,userGroup
中引用它的所有行都将被删除。
这意味着第 1 组到第 5 组已删除。
mysql> delete from workspace where id = 1;
Query OK, 1 row affected (0.03 sec)
mysql>
mysql> select * from userGroup;
+----+-------------+------+
| id | workspaceId | name |
+----+-------------+------+
| 6 | 2 | b1 |
| 7 | 2 | b2 |
| 8 | 2 | b3 |
| 9 | 2 | b4 |
| 10 | 2 | b5 |
| 11 | 3 | c1 |
| 12 | 3 | c2 |
| 13 | 3 | c3 |
| 14 | 3 | c4 |
| 15 | 3 | c5 |
+----+-------------+------+
因此,userWorkspaceMapping
中引用组 1 到 5 的所有行都将被删除。这占所有行。
级联可以并且确实在外键引用中级联了不止一个“步骤”。
CASCADE
实际上没有问题...我在这里插入了错误的测试 ID:
INSERT INTO userWorkspaceMapping (
userId,
workspaceId,
userGroupId
)
VALUES
(1, 1, 1),
(1, 2, 3), -- <---- should be between 6 and 9
(1, 3, 5) -- <---- should be between 10 and 15
;
它们都属于已删除的工作区...ID 应该是正确的。