我找到了这篇关于级联删除的文章(http://www.mysqltutorial.org/mysql-on-delete-cascade/)它回答了我最初的许多问题,但现在我有了另一个问题。除了你们,我没有其他人可以确认,所以我非常感谢你们的反馈。
脚本:
主用户表(mastertable)
支持表1(子表1)
支持表2(子表2)
支持。。。你明白了(子表…)
如果从主表中删除了用户并设置了级联,则应该从支持表中删除用户记录。
现在,回到引用我发布的链接,我获取了他们的代码并添加了额外的表来测试多重级联删除,使用他们的代码和我添加的一些额外的表(额外的支持表)。
问题虽然我确认了下面的sql语句可以正常工作并实现我想要的结果,但下面的操作是否有效,或者有没有更好/更有效的方法来执行多个级联删除?
CREATE TABLE buildings (
building_no INT PRIMARY KEY AUTO_INCREMENT,
building_name VARCHAR(255) NOT NULL,
address VARCHAR(255) NOT NULL
);
CREATE TABLE rooms (
room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
);
CREATE TABLE rooms1 (
room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
);
CREATE TABLE rooms2 (
room_no INT PRIMARY KEY AUTO_INCREMENT,
room_name VARCHAR(255) NOT NULL,
building_no INT NOT NULL,
FOREIGN KEY (building_no)
REFERENCES buildings (building_no)
ON DELETE CASCADE
);
INSERT INTO buildings(building_name,address)
VALUES('ACME Headquaters','3950 North 1st Street CA 95134'),
('ACME Sales','5000 North 1st Street CA 95134');
SELECT * FROM buildings;
INSERT INTO rooms(room_name,building_no)
VALUES('Amazon',1),
('War Room',1),
('Office of CEO',1),
('Marketing',2),
('Showroom',2);
INSERT INTO rooms1(room_name,building_no)
VALUES('Bedroom',1),
('Kitchen',1),
('Game Room',1),
('Lounge',2),
('Gym',2);
INSERT INTO rooms2(room_name,building_no)
VALUES('Purple',1),
('Blue',1),
('Red',1),
('Yellow',2),
('Green',2);
SELECT * FROM rooms;
SELECT * FROM rooms1;
SELECT * FROM rooms2;
DELETE FROM buildings
WHERE
building_no = 2;
暂无答案!
目前还没有任何答案,快来回答吧!