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

VIEWS in Database

Views in a database are like virtual tables that can select specific fields and rows from one or more underlying tables. Views can be created using the CREATE VIEW statement to select columns and apply conditions. Views can be updated, deleted, and inserted into using the same SQL statements as tables as long as certain conditions are met. Views are useful for restricting data access, simplifying queries across multiple tables, and storing complex queries.

Uploaded by

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

VIEWS in Database

Views in a database are like virtual tables that can select specific fields and rows from one or more underlying tables. Views can be created using the CREATE VIEW statement to select columns and apply conditions. Views can be updated, deleted, and inserted into using the same SQL statements as tables as long as certain conditions are met. Views are useful for restricting data access, simplifying queries across multiple tables, and storing complex queries.

Uploaded by

21112011024cs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

VIEWS in Database:

Views in database 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.

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

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.
 The SELECT statement which is used to create the view should not include
GROUP BY clause or ORDER BY clause.
 The SELECT statement should not have the DISTINCT keyword.
 The View should have all NOT NULL values.
 The view should not be created using nested queries or complex queries.
 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.

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..);

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");
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

Uses of a View:

A good database should contain views due to the given reasons:

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.

You might also like