VIEWS Understanding by chirag
DEFINITION
- A VIEW in SQL Server is like a virtual table that contains data from one or multiple tables. It does not hold any data and does not exist physically in the database. Similar to a SQL table, the view name should be unique in a database.
- It contains a set of predefined SQL queries to fetch data from the database. It can contain database tables from single or multiple databases as well.
- A VIEW does not require any storage in a database because it does not exist physically
How Does an SQL View Work?
A table (concrete table) stores its data in columns and rows in the database. A view (virtual table) is built on top of the concrete table(s) it fetches data from and does not store any data of its own in the database. A view only contains the SQL query that is used to fetch the data.
To summarize, the result set of a view is not materialized on the disk, and the query stored by the view is run every time we call the view. Let’s look at the diagram below to understand the difference between concrete and virtual tables.
Uses of a View
- Restricting data access – Views provide an additional level of table security by restricting access to a predetermined set of rows and columns of a table.
- Hiding data complexity – A view can hide the complexity that exists in multiple tables join.
- Simplify commands for the user – Views allow the user to select information from multiple tables without requiring the users to actually know how to perform a join.
- Store complex queries – Views can be used to store complex queries.
- Rename Columns – Views can also be used to rename the columns without affecting the base tables provided the number of columns in view must match the number of columns specified in select statement. Thus, renaming helps to hide the names of the columns of the base tables.
- Multiple view facility – Different views can be created on the same table for different users.
SQL CREATE OR REPLACE VIEW Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
We can use SQL VIEW to insert, update and delete data in a single SQL table. We need to note the following things regarding this.
- We can use DML operation on a single table only
- VIEW should not contain Group By, Having, Distinct clauses
- We cannot use a subquery in a VIEW in SQL Server
- We cannot use Set operators in a SQL VIEW
Use the following queries to perform DML operation using VIEW in SQL Server.
- Insert DML1Insert into DemoView values(4,'CC','KK','RR')
- Delete DML1Delete from DemoView where TableID=7
- Update DML1Update DemoView set value='Raj' where TableID=5
Getting Information about a view
Sp_Helptext View
DROP VIEW statement.Renaming the View in SQL Server
SQL VIEW and Check Option
We can use WITH CHECK option to check the conditions in VIEW are inline with the DML statements.
- It prevents to insert rows in the table where the condition in the Where clause is not satisfied
- If the condition does not satisfy, we get an error message in the insert or update statement
In the following query, we use the CHECK option, and we want only values starting with letter F in the [Codeone] column.
If we try to insert a value that does not match the condition, we get the following error message.
Q1-Can we drop a table that has dependent views on it?
Yes, we can drop a table even if any dependent views are associated with it, but the views that are associated with it will not be dropped. They will still execute in the database only with the status as inactive object and all those views become active and start functioning provided the table is recreated.
Q2-Can you create a view based on other views?
Yes, you can create a view based on other views. Usually we create views based on tables, but it also possible to create views based on views.
Q3-Can you update views?
Yes, views can be updated. However, updating a view that is based on multiple tables, may not update the underlying tables correctly. To correctly update a view that is based on multiple tables you can make use INSTEAD OF triggers in SQL Server.



Comments
Post a Comment