0% found this document useful (0 votes)
29 views5 pages

Views

Views are virtual tables that allow users to see and manipulate data from one or more underlying tables. A view can filter or organize the data and simplify complex queries. There are two types of views - simple views contain a single base table while complex views contain joins or other operations across multiple tables. Views allow restricting data access, hiding complexity, simplifying commands for users, and storing complex queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Views

Views are virtual tables that allow users to see and manipulate data from one or more underlying tables. A view can filter or organize the data and simplify complex queries. There are two types of views - simple views contain a single base table while complex views contain joins or other operations across multiple tables. Views allow restricting data access, hiding complexity, simplifying commands for users, and storing complex queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

VIEWS

Views in SQL are kind of virtual tables. A view also has rows and columns as they are in
a real table in the database. We can create a view by selecting fields from one or more
tables present in the database. A View can either have all the rows of a table or specific
rows based on certain condition.
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;

CREATE TABLE EMP(


EMP_NO INT ,
E_NAME VARCHAR(20),
DEPT_NO INT);
INSERT INTO EMP VALUES (102,'JAY
ARORA','9384');
INSERT INTO EMP VALUES (99,'HARSH
ARORA','9385');
INSERT INTO EMP VALUES (201,'ROHIT
PATEL','9386');
INSERT INTO EMP VALUES (194,'BUNNY
ARORA','9388');
INSERT INTO EMP VALUES
(142,'SHREYAS','9389');
SELECT * FROM EMP;

CREATE TABLE DEPT(


E_NAME VARCHAR(30),
DEPT_NO INT);
INSERT INTO DEPT VALUES ('AJAY
RAHANDALE','9372');
INSERT INTO DEPT VALUES ('JAY
ARORA','9384');
INSERT INTO DEPT VALUES ('BUNNY
ARORA','9388');
INSERT INTO DEPT VALUES ('LUCKY
PARMAR','9379');
INSERT INTO DEPT VALUES ('VIVEK
PATEL','9369');
SELECT * FROM DEPT;
In this example, we will create a view named MyView from the table EMP.

Simple View

QUERY:
CREATE VIEW MyView AS SELECT * FROM EMP

If we now query the view as,

QUERY:
SELECT * FROM MyView

OUTPUT:
EMP_NO E_NAME DEPT _NO
102 JAY ARORA 9384
99 HARSH ARORA 9385
201 ROHIT PATEL 9386
194 BUNNY ARORA 9388
142 SHREYAS 9389

View Using Condition


QUERY:
CREATE VIEW MyView1 AS SELECT EMP_NO,E_NAME FROM EMP WHERE
EMP_NO BETWEEN 100 AND 200

If we now query the view as,


QUERY:
SELECT * FROM MyView1

OUTPUT:
EMP_NO E_NAME
102 JAY ARORA

194 BUNNY ARORA

142 SHREYAS
Creating View from multiple tables: In this example we will create a View named
MultiView from two tables EMP and DEPT. To create a View from multiple tables we
can simply include multiple tables in the SELECT statement.
QUERY:
CREATE OR REPLACE VIEW MultiView AS SELECT EMP.EMP_NO,
DEPT.DEPT_NO FROM EMP,DEPT WHERE EMP.DEPT_NO=DEPT.DEPT_NO

To display data of View MultiView


QUERY:
SELECT * FROM MultiView

EMP_NO DEPT _NO

102 9384

194 9388

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;

Example
DROP VIEW MyView;

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 create d 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;
Example
CREATE OR REPLACE VIEW MultiView AS SELECT EMP.EMP_NO,
DEPT.DEPT_NO FROM EMP,DEPT WHERE EMP.DEPT_NO=DEPT.DEPT_NO

Inserting a row in a view:


We can insert a row in a View in a same way as we do in a table. We can use the
INSERT INTO statement of SQL to insert a row in a View.
Syntax:
INSERT INTO view_name(column1, column2 , column3,..)
VALUES(value1, value2, value3..);

Deleting a row from a View:


Deleting rows from a view is also as simple as deleting rows from a table. We can
use the DELETE statement of SQL to delete rows from a view. Also deleting a
rowfrom a view first delete the row from the actual table and the change is then
reflected in the view.
Syntax:
DELETE FROM view_name
WHERE condition;

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 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.
A View in SQL as a logical subset of data from one or more tables. Views are used to
restrict data access. A View contains no data of its own but its like window through
which data from tables can be viewed or changed. The table on which a View is based
are called BASE Tables.
There are 2 types of Views in SQL: Simple View and Complex View. Simple views can
only contain a single base table. Complex views can be constructed on more than one
base table. In particular, complex views can contain: join conditions, a group by clause,
a order by clause.
The key differences between these types of Views are:

SIMPLE VIEW COMPLEX VIEW

Contains only one single base table or is created Contains more than one base tables or is
from only one table. created from more than one tables.

We cannot use group functions like MAX(),


COUNT(), etc. We can use group functions.

Does not contain groups of data. It can contain groups of data.

DML operations could be performed through a DML operations could not always be
simple view. performed through a complex view.

INSERT, DELETE and UPDATE are directly We cannot apply INSERT, DELETE and
possible on a simple view. UPDATE on complex view directly.

Simple view does not contain group by, distinct, It can contain group by, distinct,
pseudocolumn like rownum, columns defiend by pseudocolumn like rownum, columns
expressions. defiend by expressions.

NOT NULL columns that are not selected


Does not include NOT NULL columns from by simple view can be included in complex
base tables. view.

You might also like