Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we write update and delete queries in views?

In SQL Server 2005, I have some views created by using a SELECT statement. Can we write UPDATE and DELETE statements in views?

like image 281
Surya sasidhar Avatar asked Jun 27 '10 13:06

Surya sasidhar


People also ask

Can we write update statement in view?

You can insert, update, and delete rows in a view, subject to the following limitations: If the view contains joins between multiple tables, you can only insert and update one table in the view, and you can't delete rows. You can't directly modify data in views based on union queries.

Can we use update statement in view in SQL?

Can you insert, update and delete in a View (SQL)? Yes, you can insert, update and delete a record in a view but there are some restrictions. Use the following procedure to create a sample to understand how to perform such tasks. Step 1: Create a schema of a table named "Employee" in your Database.

Can we delete from views?

Well you can delete from a view if that is what you are asking, but you can't have a view that deletes information. The view is a portion of data from the underlying tables. Provided that you have permissions, you can do the same data manipulation in views that you can do to a table directly.

What happens if we update data in a view?

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.


2 Answers

from this MSDN article: Modifying Data Through a View,

  • Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.

  • The columns that are being modified in the view must reference the underlying data in the table columns directly. They cannot be derived in any other way, such as through:

    • An aggregate function (AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR and VARP).
    • A computation; the column cannot be computed from an expression using other columns. Columns formed using set operators (UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT) amount to a computation and are also not updatable.
  • The columns that are being modified cannot be affected by GROUP BY, HAVING, or DISTINCT clauses.

  • TOP cannot be used anywhere in the select_statement of the view when WITH CHECK OPTION is also specified.

and see the article for remaining ...

like image 196
Srinivas Reddy Thatiparthy Avatar answered Sep 18 '22 01:09

Srinivas Reddy Thatiparthy


Well you can delete from a view if that is what you are asking, but you can't have a view that deletes information. The view is a portion of data from the underlying tables. Provided that you have permissions, you can do the same data manipulation in views that you can do to a table directly.

So you can do something like:

DELETE FROM my_View WHERE id = 3;

When to use views
What is a view

like image 42
kemiller2002 Avatar answered Sep 18 '22 01:09

kemiller2002