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

Views in SQL

Uploaded by

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

Views in SQL

Uploaded by

Tanisha Waichal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Class: - SYIF-3K Course: - Database Management System (313302)

Views in SQL
Views in SQL are a kind of virtual table. A view also has rows and columns like tables,
but a view doesn’t store data on the disk like a table. View defines a customized query that
retrieves data from one or more tables, and represents the data as if it was coming from a
single source.
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 conditions.
StudentDetails

StudentMarks

Syntax

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE condition;

Example: 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:

Example: 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:
DELETE VIEWS in SQL
SQL allows us to delete an existing View. We can delete or drop View using the DROP
statement.

Syntax
DROP VIEW view_name;

Example
In this example, we are deleting the View MarksView.
DROP VIEW MarksView;

UPDATE VIEW in SQL


If you want to update the existing data within the view, use the UPDATE statement.

Syntax

UPDATE view_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

Uses of a View
A good database should contain views for 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 multiple joined
tables.
3. 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.
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 a 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.

You might also like