mysql级联删除怎么写 mysql 级联删除( 二 )


- FROM
-test_sub;
- //
+----+---------+--------+
| id | main_id | value|
+----+---------+--------+
|2 |2 | TWOTWO |
+----+---------+--------+
1 row in set (0.00 sec)
如果你非要使用触发器来处理,那么下面是一个例子:
-- 创建测试主表. ID 是主键.
CREATE TABLE t_test_main (
idINTNOT NULL,
valueVARCHAR(10),
PRIMARY KEY(id)
);
-- 创建测试子表.
CREATE TABLE t_test_sub (
idINTNOT NULL,
main_id INT ,
valueVARCHAR(10),
PRIMARY KEY(id)
);
-- 插入测试主表数据.
INSERT INTO t_test_main(id, value) VALUES (1, 'ONE');
INSERT INTO t_test_main(id, value) VALUES (2, 'TWO');
-- 插入测试子表数据.
INSERT INTO t_test_sub(id, main_id, value) VALUES (1, 1, 'ONEONE');
INSERT INTO t_test_sub(id, main_id, value) VALUES (2, 2, 'TWOTWO');
DELIMITER //
CREATE TRIGGER tr_t_test_main_Del
BEFORE DELETE ON t_test_main
FOR EACH ROW
BEGIN
DELETE FROM t_test_sub WHERE main_id = OLD.id;
END;
//
DELIMITER ;
mysql delete fromt_test_mainwhere id = 1;
Query OK, 1 row affected (0.01 sec)
mysql select * from t_test_sub;
+----+---------+--------+
| id | main_id | value|
+----+---------+--------+
|2 |2 | TWOTWO |
+----+---------+--------+
1 row in set (0.00 sec)
【mysql级联删除怎么写 mysql 级联删除】mysql级联删除怎么写的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于mysql 级联删除、mysql级联删除怎么写的信息别忘了在本站进行查找喔 。

推荐阅读