Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 759
Vaibhav Jain Avatar asked Apr 15 '10 19:04

Vaibhav Jain


People also ask

Will updating the view update table?

Yes, If you update your View then it will effect your original table.

What happens when you update a view in SQL?

The SQL UPDATE VIEW command can be used to modify the data of a view. All views are not updatable. So, UPDATE command is not applicable to all views. An updatable view is one which allows performing a UPDATE command on itself without affecting any other table.

Can we update a table through view in SQL?

To modify table data through a view. In Object Explorer, expand the database that contains the view and then expand Views. Right-click the view and select Edit Top 200 Rows. You may need to modify the SELECT statement in the SQL pane to return the rows to be modified.


3 Answers

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.

like image 116
KM. Avatar answered Oct 11 '22 20:10

KM.


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.

like image 45
Larry Lustig Avatar answered Oct 11 '22 20:10

Larry Lustig


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/

like image 39
Kyle J V Avatar answered Oct 11 '22 21:10

Kyle J V