简体   繁体   English

在SQL中更新视图是一种好习惯吗?

[英]Is updating a view in SQL a good practice?

I am learning SQL. 我正在学习SQL。 It seems that PostgreSQL allows you to update a table through a 'view', if you have visibility of a few select columns of the table. 如果您对表格的一些选择列具有可见性,则PostgreSQL似乎允许您通过“视图”更新表格。 On the other hand, SQLite simply does not support this (which makes more sense to me). 另一方面,SQLite根本不支持此功能(这对我来说更有意义)。

I wonder whether it is a good practice to update tables through views even when it is allowed? 我想知道是否允许通过视图更新表是否是一个好习惯?

This question may be a matter of opinion, but I would say that updating data through a view is generally not a good practice, although there are exceptions. 这个问题可能只是一个见解,但我要说,尽管有例外,但通过视图更新数据通常不是一个好习惯。

One of the main reasons to define views is to isolate users from changes in underlying data structures. 定义视图的主要原因之一是将用户与基础数据结构的更改隔离开来。 Because not all views are updatable, that means that a change to the definition of a view (but not the result set) could invalidate code. 因为并非所有视图都是可更新的,所以这意味着对视图定义(而不是结果集)的更改可能会使代码无效。

In some databases, it is possible to get around this by using triggers on views. 在某些数据库中,可以通过在视图上使用触发器来解决此问题。

I should add that is "general" thinking. 我还要补充一点是“一般性”的想法。 Another reason to have views is for access control and security. 拥有视图的另一个原因是访问控制和安全性。 For instance, some users may not be able to see some columns in some tables; 例如,某些用户可能看不到某些表中的某些列; they have access to the view but not the underlying table. 他们有权访问视图,但不能访问基础表。 In this case, updates to views are a bit more reasonable. 在这种情况下,更新视图会更加合理。

All that said, I should point out that I'm not really a fan of having users update data directly at all. 综上所述,我应该指出,我并不是真正的让用户直接更新数据的人。 My preference is to do such updates through stored procedures, so there is much better control over the data model, auditing, and user-access. 我更喜欢通过存储过程进行此类更新,因此可以更好地控制数据模型,审核和用户访问。

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

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