If I update a view, will my original tables get updated

103,585

Solution 1

see Using Views in 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.

Solution 2

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.

Solution 3

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/

Share:
103,585
Vaibhav Jain
Author by

Vaibhav Jain

IT professional

Updated on December 09, 2020

Comments

  • Vaibhav Jain
    Vaibhav Jain over 3 years

    Hypothetically I have two tables Employee and Locations. Additionaly I have a view viewEmpLocation which is made by joining Employee and Locations.

    If I update the view, will the data in the original table get updated?

  • Vaibhav Jain
    Vaibhav Jain about 14 years
    I understand your point. But a new question arises, if view is only a select statement, then How Indexes are maintained on a view. Are all the indexes get rebuilt as soon as someone queries a view.
  • Larry Lustig
    Larry Lustig about 14 years
    There are no indexes on views, only on the underlying tables. The indexes are used by the DBMS to assemble the rows from the different tables when you request data from the view. (This ignores a fairly new kind of view, the "materialized view" in which the database stores a "shadow" copy of the data, effectively pre-computing the view for you).
  • dburges
    dburges about 14 years
    There are so many caveats on inserting through a view, that I personally find it simpler to insert directly to the tables. After all,you still have to look up what fields go to what table in order to comply with rules and if you know that there is no reason not to directly update or insert the table.
  • KM.
    KM. about 14 years
    I have never actually done an UPDATE or INSERT on a view, but it might benefit you if you have an indexed view that you can use to quickly locate the rows that need the change. In that case I would construct the view for the sole purpose of the update.
  • Kyle J V
    Kyle J V about 14 years
    I appreciate feedback on my answer, but if it's negative, why does nobody comment? I'd like to be corrected if wrong.
  • Dan Bechard
    Dan Bechard over 8 years
    The question explicitly asks about the effects of updating a view. Proposing alternatives is great and all, but it doesn't answer the question.