Monday, August 26, 2013

How to Insert, Update, Delete Rows in a View in SQL Server

SQL Server Create View Statement
A View can be defined as nothing more than a SQL statement that is stored in the database with an associated name. A view is actually a composition of a table in the form of a predefined SQL query.
A view can contain all rows of a table or select rows from a table. A view can be created from one or many tables which depends on the written SQL query to create a view.

Views, which are kind of virtual tables, allow users to do the following:
  • Structure data in a way that users or classes of users find natural or intuitive.
  • Restrict access to the data such that a user can see and (sometimes) modify exactly what they need and no more.
  • Summarize data from various tables which can be used to generate reports.
Creating a View:
Database views are created using the CREATE VIEW statement. Views can be created from a single table, multiple tables, or another view.
To create a view, a user must have the appropriate system privilege according to the specific implementation.
The basic CREATE VIEW syntax is as follows:
CREATE VIEW view_name AS
SELECT Column1, Column2.....
FROM Table_name
WHERE [condition];
You can include multiple tables in your SELECT statement in very similar way as you use them in normal SQL SELECT query.
Example:
Consider the CUSTOMER table having the following records:
Now, following is the example to create a view from CUSTOMER table. This view would be used to have customer name and age from CUSTOMER table:
CREATE VIEW CUSTOMER_VIEW AS
SELECT Name, Age
FROM CUSTOMER;
Now, you can query CUSTOMER_VIEW in similar way as you query an actual table. Following is the example:
SELECT * FROM CUSTOMER_VIEW;
Result:

View WITH CHECK OPTION:
The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose of the WITH CHECK OPTION is to ensure that all UPDATE and INSERTs satisfy the condition(s) in the view definition.
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
The following is an example of creating same view CUSTOMER_VIEW with the WITH CHECK OPTION:

CREATE VIEW CUSTOMER_VIEW AS
SELECT Name, Age
FROM CUSTOMER
WHERE Age IS NOT NULL
WITH CHECK OPTION;

The WITH CHECK OPTION in this case should deny the entry of any NULL values in the view's AGE column, because the view is defined by data that does not have a NULL value in the AGE column.
Updating a View:
A view can be updated under certain conditions:
  • The SELECT clause may not contain the keyword DISTINCT.
  • The SELECT clause may not contain summary functions.
  • The SELECT clause may not contain set functions.
  • The SELECT clause may not contain set operators.
  • The SELECT clause may not contain an ORDER BY clause.
  • The FROM clause may not contain multiple tables.
  • The WHERE clause may not contain sub queries.
  • The query may not contain GROUP BY or HAVING.
  • Calculated columns may not be updated.
  • All NOT NULL columns from the base table must be included in the view in order for the INSERT query to function.
So if a view satisfies all the above-mentioned rules then you can update a view. Following is an example to update the age of Ramesh:

UPDATE CUSTOMER_VIEW
SET AGE = 33
WHERE Name='Ramesh';
This would ultimately update the base table CUSTOMER and same would reflect in the view itself. Now, try to query base table, and SELECT statement would produce the following result:

Inserting Rows into a View:

Rows of data can be inserted into a view. The same rules that apply to the UPDATE command also apply to the INSERT command.
Here we can not insert rows in CUSTOMER_VIEW because we have not included all the NOT NULL columns in this view, otherwise you can insert rows in a view in similar way as you insert them in a table.
Deleting Rows in a View:
Rows of data can be deleted from a view. The same rules that apply to the UPDATE and INSERT commands apply to the DELETE command.
Following is an example to delete a record having AGE= 22.
DELETE FROM CUSTOMER_VIEW
WHERE Age = 22;

This would ultimately delete a row from the base table CUSTOMER and same would reflect in the view itself. Now, try to query base table, and SELECT statement would produce the following result:
Dropping a View:
You need a way to drop the view if it is no longer needed. The syntax is very simple as given below:
DROP VIEW view_name;
Following is an example to drop CUSTOMER_VIEW from CUSTOMER table:
DROP VIEW CUSTOMER_VIEW;
Thanks, TAMATAM

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.