View in SQL
Views (Virtual Tables) in SQL
Concept of a view in SQL
Single table derived from other tables
Considered to be a virtual table
VIEW Definition
View is Virtual relation that does not
necessarily actually exist in the database
but is produced upon request, at time of
request.
Views (Virtual Tables) in SQL
A view is a virtual table based on the result-set
of an SQL statement.
A view contains rows and columns, just like a
real table.
The fields in a view are fields from one or
more tables in the database.
User can add SQL functions, WHERE, and JOIN
statements to a view and present the data .
CREATE VIEW command
Give Table (View) name,
List of attribute names for the View (table),
and a Query to specify the contents of the View
CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Consider the EMPLOYEE table having the following records:
Emp_id Emp_name Dept Salary
1 Adams Sales 40000
2 Neel HR 70000
3 Johnson Sales 50000
4 Smith Research 90000
5 Willams HR 80000
SQL > CREATE VIEW EMPLOYEE_VIEW AS
SELECT Emp_id, Emp_name
FROM EMPLOYEE;
Then, can query the view as follows:
SQL > SELECT * FROM EMPLOYEE_VIEW;
Consider the EMPLOYEE table having the following records:
Emp_id Emp_name
1 Adams
2 Neel
3 Johnson
4 Smith
5 Willams
The WITH CHECK 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 WITH CHECK OPTION
EMPLOYEE_VIEW with the WITH CHECK OPTION
CREATE VIEW EMPLOYEE_VIEW AS
SELECT Emp_id,Emp_name
FROM EMPLOYEE
WHERE Emp_id 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 Emp_id column,
because the view is defined by data that does not
have a NULL value in the Emp_id column.;
SQL 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.
SQL Updating a View
The FROM clause may not contain multiple
tables.
The WHERE clause may not contain subqueries.
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
abovementioned rules then you can update a
view.
SQL Updating a View
Following is an example to update the Emp_id
of Neel
SQL > UPDATE EMPLOYEE_VIEW
SET Emp_id = 35 WHERE name=‘Neel';
This would ultimately update the base table
EMPLOYEE and same would reflect in the view
itself.
Now, try to query base table, and SELECT
statement would produce the following result:
SQL Drop a View
To delete a view with the DROP VIEW
command.
SQL DROP VIEW Syntax
DROP VIEW view_name