每当视图中的值发生更改时触发
Trigger whenever a value in a view has changed
我有一个 SQL 视图,它解决了一些外键依赖关系。视图如下所示:
id | title | plantName | customerName | lastUpdated
其中 plantName 和 customerName 是外键的 2 个解析值。
共有3张表:
Table 1
id | name | surname | idTable2 | idTable3 | lastUpdated
Table 2
id | plantName
Table 3
id | firstName | lastName
我想要实现的是一个触发器,每当一行显示另一个值时,它就会更新视图中的 lastUpdated
列。
这里的问题是,当外键更改它的值时,视图中的 lastUpdated
也应该更新。
示例视图输出:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | raddison | mueller | 2020-02-26 10:04:03.000
假设我将更新 Table 2
并将 "raddison" 更新为 "mercure hotel"。之后视图将如下所示:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | mercure hotel | mueller | 2020-02-26 10:04:03.000
我现在想要第 2 行视图中的 lastUpdated
列表示外键已更改的日期时间。
期望的输出:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | mercure hotel | mueller | 2020-02-26 13:44:03.000
有什么方法可以实现吗?
提前致谢
在评论中,您确认您想要以下内容:
you want to update the "last_updated" column in table 1 whenever a
value changes in tables 1, 2 or 3, and include that "last_updated"
value in your view
向您展示如何创建触发器以在修改记录时更新列。
您需要在 tables 1、2 和 3 上创建触发器,并且更新语句应该更新 table 1 的 lastUpdated
列。
或者,您可以在所有 3 个 table 上创建一个 lastUpdated
,并且 select 您认为的最高值。
我有一个 SQL 视图,它解决了一些外键依赖关系。视图如下所示:
id | title | plantName | customerName | lastUpdated
其中 plantName 和 customerName 是外键的 2 个解析值。
共有3张表:
Table 1
id | name | surname | idTable2 | idTable3 | lastUpdated
Table 2
id | plantName
Table 3
id | firstName | lastName
我想要实现的是一个触发器,每当一行显示另一个值时,它就会更新视图中的 lastUpdated
列。
这里的问题是,当外键更改它的值时,视图中的 lastUpdated
也应该更新。
示例视图输出:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | raddison | mueller | 2020-02-26 10:04:03.000
假设我将更新 Table 2
并将 "raddison" 更新为 "mercure hotel"。之后视图将如下所示:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | mercure hotel | mueller | 2020-02-26 10:04:03.000
我现在想要第 2 行视图中的 lastUpdated
列表示外键已更改的日期时间。
期望的输出:
1 | hotel | mock | meier | 2020-02-26 10:03:03.817
2 | hotel | mercure hotel | mueller | 2020-02-26 13:44:03.000
有什么方法可以实现吗?
提前致谢
在评论中,您确认您想要以下内容:
you want to update the "last_updated" column in table 1 whenever a value changes in tables 1, 2 or 3, and include that "last_updated" value in your view
您需要在 tables 1、2 和 3 上创建触发器,并且更新语句应该更新 table 1 的 lastUpdated
列。
或者,您可以在所有 3 个 table 上创建一个 lastUpdated
,并且 select 您认为的最高值。