简体   繁体   English

更新视图与触发器

[英]Updating Views vs. Triggers

I am studying for a final exam, and came across this question: 我正在为期末考试而学习,并且遇到了这个问题:

Explain why updating views is not recommended. 说明为什么不建议更新视图 Explain how triggers can be used to support view updates. 说明如何使用触发器来支持视图更新。

I have looked on the web, and read a couple chapters from the book to no avail. 我浏览了网上,并从书中读了几章无济于事。

I have seen points made to where views can help make life easier, but none arguing against them. 我已经看到观点可以使生活变得更轻松,但没有人反对它们。

Is this a possible answer? 这是可能的答案吗?

One could use the INSTEAD OF clause in a TRIGGER statement in order to circumvent the updating of a table. 可以在TRIGGER语句中使用INSTEAD OF子句,以规避表的更新。 This would allow for the update of multiple tables that could be represented by one view. 这将允许更新可以由一个视图表示的多个表。

So, my questions are: 因此,我的问题是:

1.) Why are updating views not recommended? 1.)为什么不建议更新视图?

2.) How can triggers be used as a solution to the problem? 2.)如何将触发器用作解决问题的方法?

There are many restrictions on inherently updatable views . 对固有的可更新视图很多限制

This can be both frustrating and fragile, as future evolution of your view and/or schema might made the view no longer inherently updatable -- so breaking code that relies on this feature. 这可能既令人沮丧又脆弱,因为视图和/或架构的未来发展可能会使视图不再固有地可更新-因此破坏了依赖于此功能的代码。

At the expense of few lines of code, using an INSTEAD OF trigger will have the benefit of both reducing the above concern and to allow you to update a non-inherently updatable views. 以几行代码为代价,使用INSTEAD OF触发器将具有以下好处:减少了上述担忧,并允许您更新非固有的可更新视图。 You can use an INSTEAD OF trigger on an inherently updatable view to override the default behavior. 您可以在固有可更新的视图上使用INSTEAD OF触发器来覆盖默认行为。

When researching views, be sure to rely on relatively recent data as views have changed over the years so older opinions on the subject may not be valid. 在研究视图时,请确保依赖相对较新的数据,因为多年来这些视图已经发生了变化,因此关于该主题的较早的观点可能无效。 When views were first made updateable, the restrictions were many and control of the operation was little or nonexistent. 首次使视图可更新时,限制很多,对该操作的控制很少或根本不存在。

With the ability to write triggers on views in many systems, the restrictions have fallen away and control is precise. 在许多系统中,由于能够在视图上编写触发器,因此限制已消失,控制也很精确。 We can now determine exactly what happens to all the data during DML to views. 现在,我们可以准确确定在DML视图期间所有数据发生了什么。 It is now to the point where I disallow direct access to tables by applications. 现在到了我不允许应用程序直接访问表的地步。 All DML originating from apps have to go thru views (or stored procedures, but they are not as good as view triggers). 所有源自应用程序的DML都必须经过视图(或存储过程,但不如视图触发器那样好)。 The benefits are so vast I don't see why it hasn't become a universal standard. 好处是巨大的,我不明白为什么它还没有成为通用标准。

Indeed, many people's "views" on views (unfortunately, many people who are in charge of databases) seem to be stuck in the 1990's. 实际上,许多人对观点的“观点”(不幸的是,许多负责数据库的人)似乎停留在1990年代。 Some don't want any views at all in their database. 有些人根本不希望数据库中有任何视图。 Some allow views but don't allow using them for DML. 有些允许视图,但不允许将其用于DML。 Many insist on giving them special names ("VW_name", "name_View" or similar) which breaks an extremely useful wall of abstraction for your data. 许多人坚持给它们指定特殊名称(“ VW_name”,“ name_View”或类似名称),这打破了对数据非常有用的抽象墙。 Data abstraction is not a strong point of databases, so grab it where you can. 数据抽象不是数据库的强项,因此请尽可能获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM