相互依赖的MySQL计算触发器

时间:2016-10-10 01:15:36

标签: mysql sql triggers

我目前正在开发一个大型MySQL数据库,其中包含4个主要表,如下所示:

表格损坏循环

---------------------------------------------------------
| Damage Loop No | Description | Minimum Remaining Life |
---------------------------------------------------------
| abc            | pie         | 5                      |
| xyz            | apple       | 15                     |
| ...            | ...         | ...                    |
---------------------------------------------------------

表设备

---------------------------------------------------------------------------
| Damage Loop No | Equipment No | Description    | Minimum Remaining Life |
---------------------------------------------------------------------------
| abc            | X100         | pie slice 1    | 5                      |
| abc            | X200         | pie slice 2    | 20                     |
| xyz            | B100         | apple slice 1  | 15                     | 
| xyz            | B200         | apple slice 2  | 30                     |   
| ...            | ...          | ...            | ...                    |    
---------------------------------------------------------------------------

表格监控位置

--------------------------------------------------------------------------------------------------------------------------------------
| Damage Loop No | Equipment No | CML No | Description | Corrosion Rate | Wall Thickness | Allowable Wall Thickness | Remaining Life |
--------------------------------------------------------------------------------------------------------------------------------------
| abc            | X100         | 1      | Location 1  | 0.2            | 3              | 2                        | 5
| abc            | X100         | 2      | Location 2  | 0.2            | 4              | 2                        | 10         
| xyz            | B100         | 1      | Location 1  | 0.2            | 5              | 2                        | 15
| xyz            | B100         | 2      | Location 2  | 0.2            | 6              | 2                        | 20    
| ...            | ...          | ...    | ...         | ...            | ...            | ...                      | ... 
--------------------------------------------------------------------------------------------------------------------------------------

表格检查

---------------------------------------------------------------------------
| Damage Loop No | Equipment No | CML No | Inspection No | Wall Thickness |
---------------------------------------------------------------------------
| abc            | X100         | 1      | 1             | 5              |
| abc            | X100         | 1      | 2             | 10             |
| abc            | X100         | 1      | 3             | 3              |
| ...            | ...          | ...    | ...           | ...            | 
---------------------------------------------------------------------------

现在这些表都有相互依赖的计算,我希望根据用户在数据库中更改的值来执行这些计算。例如,如果我们查看[Damage Loop]。[最小剩余寿命]。计算如下......

[Damage Loop].[Minimum Remaining Life] = Min([Equipment].[Minimum Remaining Life] For Associated Records)

[设备]。[最低剩余寿命]计算如下......

[Equipment].[Minimum Remaining Life] = Min([Monitoring Location].[Remaining Life] For Associated Records)

[监测地点]。[剩余寿命]计算如下......

[Monitoring Location].[Remaining Life] = ([Wall Thickness] - [Allowable Wall Thickness])/[Corrosion Rate]

Where...
[Wall Thickness] = Min([Inspection].[Wall Thickness] for associated records)
[Allowable Wall Thickness] = Manual input
[Corrosion Rate] = Manual input

因此,如果用户更改[监控位置]。[允许的墙厚度]字段,我希望触发器仅重新计算以下字段...

[Monitoring Location].[Remaining Life]
[Equipment].[Remaining Life]
[Damage Loop].[Remaining Life]

但如果用户更改[检查]。[壁厚]字段,我希望触发器执行以下计算...

[Monitoring Location].[Wall Thickness]
[Monitoring Location].[Remaining Life]
[Equipment].[Remaining Life]
[Damage Loop].[Remaining Life]

即。只重新计算用户更改字段依赖的字段。

我在我的系统中基本上有数百个这样的计算,并且想要一种非常有效且结构化的方式,只根据用户在数据库中的变化重新计算相关字段。我想避免为系统中的每个可能情况编写特定的触发器。关于如何实现这一目标的任何想法?

0 个答案:

没有答案