Reading Material Experiment 2.4
Reading Material Experiment 2.4
An SQL View is a specific representation of data from one or more tables. The tables
referred in the views are known as Base tables. Creating a view does not take any storage
space as only the query is stored in the data dictionary and the actual data is not stored
anywhere.
The maximum number of columns that can be defined in a SQL View are 1000 as in tables.
StudentMarks
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created from a single
table or multiple tables.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
To see the data in the View, we can query the view in the same manner as we query a table.
Creating View from multiple tables: In this example we will create a View named
MarksView from two tables StudentDetails and StudentMarks. To create a View from
multiple tables we can simply include multiple tables in the SELECT statement.
Query:
CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
DELETING VIEWS
We have learned about creating a View, but what if a created View is not needed any more?
Obviously we will want to delete it. SQL allows us to delete an existing View. We can delete
or drop a View using the DROP statement.
Syntax:
DROP VIEW view_name;
For example, if we want to delete the View MarksView, we can do this as:
UPDATING VIEWS
There are certain conditions needed to be satisfied to update a view. If any one of these
conditions is not met, then we will not be allowed to update the view.
1. The SELECT statement which is used to create the view should not include
GROUP BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex queries.
5. The view should be created from a single table. If the view is created using
multiple tables then we will not be allowed to update the view.
We can use the CREATE OR REPLACE VIEW statement to add or remove fields from a
view.
Syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1,coulmn2,..
FROM table_name
WHERE condition;
For example, if we want to update the view MarksView and add the field AGE to this View
from StudentMarks Table, we can do this as:
Syntax:
INSERT INTO view_name(column1, column2 , column3,..)
VALUES(value1, value2, value3..);
view_name: Name of the View
Example:
In the below example we will insert a new row in the View DetailsView which we have
created above in the example of “creating views from a single table”.
Output:
Output:
In the below example we are creating a View SampleView from StudentDetails Table with
WITH CHECK OPTION clause.
In this View if we now try to insert a new row with null value in the NAME column then it
will give an error because the view is created with the condition for NAME column as NOT
NULL.
For example,though the View is updatable but then also the below query for this View is not
valid:
Uses of a View :
A good database should contain views due to the given reasons:
1. 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.
2. Hiding data complexity –
A view can hide the complexity that exists in a multiple table join.
3. Simplify commands for the user –
Views allows the user to select information from multiple tables without requiring
the users to actually know how to perform a join.
4. Store complex queries –
Views can be used to store complex queries.
5. 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 to hide the names of the
columns of the base tables.
6. Multiple view facility –
Different views can be created on the same table for different users.
References
● SQL | Views - GeeksforGeeks
● Oracle SQL & PL/SQL: SQL Views (sql-plsql.blogspot.com)
● PL/SQL Create View
Video References
● SQL tutorial 60: SQL View in Oracle Database By Manish Sharma RebellionRider -
YouTube
● What is the view in oracle? simple view? complex view? - YouTube