sql - 为什么我们需要触发器来从 View 中删除?

标签 sql sql-server plsql triggers

有时我发现一些代码可以触发允许用户从 View 中删除? 但是为什么我们需要从 View 中删除,因为我们可以像下面这样简单的代码一样转到 Table > 并执行删除:

delete from table where x=y ;

我的问题是为什么我们要使用 TRIGGERS 来删除 View ? 换句话说,使用删除相对于 View 有什么优势!

最佳答案

The reason we use view are mainly to hide the complexity of data sources , Hide away the actual tables (For security reasons), saving us writing the same code over and over again.

Now if you havent let your users access the tables directly and they only work through View in this case they will be executing Deletes against views.

Triggers are only used when your View has more than One underlying table. You cannot do update, insert or delete operations on view if it effects multiple underlying tables. Therefore we make use of Instead of Delete/Update/Insert Triggers to do these operations against views.

When you have multiple Underlying tables if you Update,Delete or Insert effects only One underlying table it does allow you to execute the statement but it is not guaranteed that it will be done correctly.

therefore if you have a situation where your view is based on multiple underlying tables you should always do update/delete/inserts

1) Directly to underlying tables.
2) Use Instead of triggers.

还有 Read here 了解有关为什么不应对具有多个基础表的 View 执行更新/删除/插入操作的更多详细信息。

关于sql - 为什么我们需要触发器来从 View 中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21591020/

相关文章:

sql - 检查所有表行是否相等

mysql - 我应该为 vb.net 应用程序使用什么 sql 查询语句?

mysql - SQL 按通配符排序

c# - Entity Framework 在一次插入多行时忽略重复的主键值

oracle - 调用 Oracle PL/SQL 过程时使用什么编码

mysql - 在 SQL 中组合具有不同属性和大小的表

sql - 如何在 SQL 中不显示计数列

Sql从逗号分隔的字符串中删除重复项

oracle - 在 where 子句中使用函数调用进行 pl/sql 查询优化

oracle pl/sql 错误 : can't put_line more than 2000 characters