SQL 如果我更新视图,我的原始表会更新吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2648445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If I update a view, will my original tables get updated
提问by Vaibhav Jain
Hypothetically I have two tables Employee and Locations. Additionaly I have a view viewEmpLocation which is made by joining Employee and Locations.
假设我有两个表 Employee 和 Locations。另外,我有一个视图 viewEmpLocation,它是通过加入 Employee 和 Locations 制作的。
If I update the view, will the data in the original table get updated?
如果我更新视图,原始表中的数据会更新吗?
采纳答案by KM.
see Using Views in Microsoft SQL Server
请参阅在 Microsoft SQL Server 中使用视图
When modifying data through a view (that is, using INSERT or UPDATE statements) certain limitations exist depending upon the type of view. Views that access multiple tables can only modify one of the tables in the view. Views that use functions, specify DISTINCT, or utilize the GROUP BY clause may not be updated. Additionally, inserting data is prohibited for the following types of views:
* views having columns with derived (i.e., computed) data in the SELECT-list * views that do not contain all columns defined as NOT NULL from the tables from which they were defined
It is also possible to insert or update data through a view such that the data is no longer accessible via that view, unless the WITH CHECK OPTION has been specified.
通过视图修改数据(即使用 INSERT 或 UPDATE 语句)时,根据视图的类型存在某些限制。访问多个表的视图只能修改视图中的一个表。使用函数、指定 DISTINCT 或使用 GROUP BY 子句的视图可能不会更新。此外,禁止为以下类型的视图插入数据:
* views having columns with derived (i.e., computed) data in the SELECT-list * views that do not contain all columns defined as NOT NULL from the tables from which they were defined
也可以通过视图插入或更新数据,这样数据就不能再通过该视图访问,除非指定了 WITH CHECK OPTION。
回答by Larry Lustig
Yes.
是的。
The data "in" a view has no existence independent from the tables that make up the view. The view is, in essence, a stored SELECT statement that masquerades as a table. The data is stored in the original tables and only "assembled" into the view when you want to look at it. If the view is updateable (not all views are) the updates are applied to the table data.
视图“中”的数据不存在独立于构成视图的表。该视图本质上是一个伪装成表的存储 SELECT 语句。数据存储在原始表中,只有在您想查看时才“组装”到视图中。如果视图是可更新的(并非所有视图都是可更新的),则更新将应用于表数据。
回答by Kyle J V
You could use a trigger on the view to do an insert/update/delete to the actual tables.
您可以在视图上使用触发器对实际表执行插入/更新/删除操作。
http://www.devarticles.com/c/a/SQL-Server/Using-Triggers-In-MS-SQL-Server/1/
http://www.devarticles.com/c/a/SQL-Server/Using-Triggers-In-MS-SQL-Server/1/