Trigger before delete MySql(删除MySql前触发)
问题描述
我有一个名为 user 的表.这个表有一个部门表的外键.一个用户可以与一个部门相关联.在删除部门之前,我想将任何用户(具有该部门 ID)设置为默认值 (1) 以避免引用完整性错误.
I have a table named user. This table has a foreign key to a department table. One user can be associated with one department. Before deleting a department, I would like to set any user (having that department ID) to a default value (1) to avoid a referential integrity error.
你知道一个很好的例子吗?大多数示例表明触发器应用于一张表.这里触发器应该在部门上触发,但更改用户表中的值.
Do you know a good example. Most examples shows that the trigger is applied to one table. Here the trigger should be triggered on department but change values in user table.
谢谢.
推荐答案
我还没有测试过,但是 基于文档,这看起来是正确的:
I haven't tested it, but based on the documentation, this looks about right:
CREATE TRIGGER update_user_before_delete BEFORE DELETE ON department
FOR EACH ROW BEGIN
UPDATE user SET department = 1 WHERE department = OLD.department;
END;
这篇关于删除MySql前触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:删除MySql前触发
基础教程推荐
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01