mysql - 游标在触发器内并更新表列

标签 mysql sql database

我有三个表

一个是包含 User_Id、User_name、Name 和 lastModifiedDate 列的 userDetails。 第二个是具有列 roleId、RoleName 和 第三个表是 User_Role,包含列 user_Id(外键引用 UserDetails)和 Role_id(外键引用 Role)。

现在的问题是,每当 Role 表中发生任何更新或删除操作时,我都需要通过从 User_Role 表中获取相应映射的 user_Id 来更新 UserDetails 表的 lastModifiedDate 列。

例如,我在 Role 表中将 roleId 设置为 1,2,3,4,并且将 roleId '3' 映射到 User_Role 表中的 user_Id 101,102,103,104。因此,如果在角色表中更新了 roleId '3',那么我需要从 User_Role 获取那些映射的 userId,并使用 sysdate 更新它们的 lastModifieddate 列。

我必须使用触发器,但我不知道,因为我是数据库的新手并且对此一无所知......请帮助大家 我必须在 mysql 中完成

最佳答案

你可以这样创建触发器:

 create or replace trigger PT_AD_ROLE after DELETE OR UPDATE on ROLE_TABLE for each row
      begin

       update user_table set lastModifiedTime=current_timestamp() from user_table u,role_table r,user_role  ur where r.role_id=ur.role_id and ur.user_id=u.user_id and r.role_id=:old.role_id;
    end

;

关于mysql - 游标在触发器内并更新表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29935036/

相关文章:

php - 在 PHP 中调用后多次插入

sql - 将三个查询合并为一个结果

sql - 具有多个结果的子查询,SQL Server

database - 关于转向多层 Delphi 架构的建议

php - 对多列进行 ASC 排序

mysql - 想在求和 1 列时从 3 个表中获取数据

mysql - 在 Mysql 中排序然后分组

mysql - 在 group_concat 中排序

MySQL:列出数据及其来自其他表的相关数据(如果可用)

android - CursorIndexOutOfBoundsException 请求索引 0,大小为 0