0% found this document useful (0 votes)
56 views

U3-SQL View

Views are logical tables that store data from base tables. A view is defined by a SQL SELECT statement and does not physically store data itself. Views can be used to restrict access to columns, filter rows, or join data from multiple tables. This document discusses how to create views from single and multiple tables, delete views, and update views by adding or removing columns under certain conditions. It also provides examples of inserting, deleting, and selecting rows from a view similarly to a table.

Uploaded by

Smarty Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

U3-SQL View

Views are logical tables that store data from base tables. A view is defined by a SQL SELECT statement and does not physically store data itself. Views can be used to restrict access to columns, filter rows, or join data from multiple tables. This document discusses how to create views from single and multiple tables, delete views, and update views by adding or removing columns under certain conditions. It also provides examples of inserting, deleting, and selecting rows from a view similarly to a table.

Uploaded by

Smarty Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL | Views

VIEW

View is a logical table. It is a physical object which stores data logically. View just refers to data
that is tored in base tables.

A view is a logical entity. It is a SQL statement stored in the database in the system tablespace.
Data for a view is built in a table created by the database engine in the TEMP tablespace.

INDEX

Indexes are pointres that maps to the physical address of data. So by using indexes data
manipulation becomes faster.

An index is a performance-tuning method of allowing faster retrieval of records. An index


creates an entry for each value that appears in the indexed columns.

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.

In this article we will learn about creating , deleting and updating Views.
Sample Tables:
StudentDetails

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;

view_name: Name for the View


table_name: Name of the table
condition: Condition to select rows
Examples:
 Creating View from a single table:
 In this example we will create a View named DetailsView from the table
StudentDetails.
Query:
 CREATE VIEW DetailsView AS

 SELECT NAME, ADDRESS

 FROM StudentDetails

 WHERE S_ID < 5;

To see the data in the View, we can query the view in the same manner as we query a
table.
SELECT * FROM DetailsView;

Output:

 In this example, we will create a view named StudentNames from the table
StudentDetails.
Query:

 CREATE VIEW StudentNames AS

 SELECT S_ID, NAME

 FROM StudentDetails

 ORDER BY NAME;

If we now query the view as,

SELECT * FROM StudentNames;

Output:

 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;

To display data of View MarksView:

SELECT * FROM MarksView;

Output:

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;

view_name: Name of the View which we want to delete.


For example, if we want to delete the View MarksView, we can do this as:
DROP VIEW MarksView;

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:

CREATE OR REPLACE VIEW MarksView AS

SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS,


StudentMarks.AGE

FROM StudentDetails, StudentMarks

WHERE StudentDetails.NAME = StudentMarks.NAME;

If we fetch all the data from MarksView now as:

SELECT * FROM MarksView;

Output:

 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 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”.
INSERT INTO DetailsView(NAME, ADDRESS)

VALUES("Suresh","Gurgaon");

If we fetch all the data from DetailsView now as,

SELECT * FROM DetailsView;

Output:

 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 row from 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;

 view_name:Name of view from where we want to delete rows


 condition: Condition to select rows
Example:
In this example we will delete the last row from the view DetailsView which we just added
in the above example of inserting rows.
DELETE FROM DetailsView

WHERE NAME="Suresh";

If we fetch all the data from DetailsView now as,

SELECT * FROM DetailsView;

Output:

You might also like