0% found this document useful (0 votes)
8 views25 pages

Co2 - Views in Dbms

This document provides an overview of views in Database Management Systems (DBMS), detailing their creation, usage, and advantages, particularly in relation to data security and access control. It explains how to create, update, and delete views using SQL commands, along with examples of simple and complex views. The document also outlines the rules for updating views and includes references for further reading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

Co2 - Views in Dbms

This document provides an overview of views in Database Management Systems (DBMS), detailing their creation, usage, and advantages, particularly in relation to data security and access control. It explains how to create, update, and delete views using SQL commands, along with examples of simple and complex views. The document also outlines the rules for updating views and includes references for further reading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Department of CSE

COURSE NAME: DATABASE MANAGEMENT SYSTEM

COURSE CODE : 24AD2104R

Topic : VIEWS IN DBMS

Session -
AIM OF THE SESSION
To provide students with a comprehensive understanding of Views in DBMS, including their creation,
usage, advantages and real-world applications, with a focus on data security and role-based access
control..

INSTRUCTIONAL OBJECTIVES

This Session is designed to explain the concept and purpose of views in relational databases.
Demonstrate how to create, update, and drop views using SQL

LEARNING OUTCOMES

At the end of this session, students will be able to apply views to enforce restricted access to sensitive
data.
VIEWS IN DBMS
 Views in SQL are kind of virtual tables.
 A View can either have all the rows of a table or specific rows based on certain
condition.
 The changes made in a View are not reflected back to the actual table in the
database.
 SQL functions like WHERE, and JOIN can be applied to a view and present the
data as if the data were coming from one single table.
 A view of table always shows up-to-date data. Every time a user queries a view,
the table is recreated
Given below two tables are used for examples.

Table 1: StudentDetails
table2 : StudentMarks
CREATE VIEWS in SQL

 We can create a view using CREATE VIEW statement.


 A View can be created from a single table or multiple tables.

Syntax:

CREATE VIEW view_name AS


Key Terms:
SELECT column1, column2.....
•view_name: Name for the View
FROM table_name
•table_name: Name of the table
WHERE condition;
•condition: Condition to select rows
Example 1: Creating a Simple View from a Single Table

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;
Retrieve the data from this view
Query:

SELECT * FROM DetailsView;

Output:
Create another view named StudentNames from the table StudentDetails.

Query:

CREATE VIEW StudentNames AS


SELECT S_ID, NAME
FROM StudentDetails
ORDER BY NAME;

Query the view as,


output

SELECT * FROM StudentNames;


Example 2: Creating a View From Multiple Tables
Now Create a View MarksView that combines data from both 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;
Output of a view from multiple tables
To display data of View MarksView:

SELECT * FROM MarksView;

Output
Deleting Views in DBMS

A view is deleted with the DROP VIEW statement.

Syntax : DROP VIEW view_name;

Example: In this example, we are deleting the View MarksView.

Query: DROP VIEW MarksView;


Updating a View

If we 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];
Updating a View

If you want to update the view definition without affecting the data, use the
CREATE OR REPLACE VIEW statement. For example, let’s add the Age
column to the MarksView:

Syntax:

CREATE OR REPLACE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;
Updating a View

Example:

CREATE OR REPLACE VIEW MarksView AS


SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS,
StudentMarks.AGE
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
Updating a View

If we fetch all the data from MarksView now as:

SELECT * FROM MarksView;

Output:
Rules to Update Views in SQL:
Certain conditions need to be satisfied to update a view. If any of these conditions are not
met, the view can not be updated.
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.
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, ...)


VALUES(value1,value2,.....);
Inserting a row in a view

In this example, we will insert a new row in the View DetailsView which we have
created already

Example:

INSERT INTO DetailsView(NAME, ADDRESS)


VALUES("Suresh","Gurgaon");
Inserting a row in a view

If we fetch all the data from DetailsView now as,

SELECT * FROM DetailsView;


Deleting a row in 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.

Syntax

DELETE FROM view_name WHERE condition;


Deleting a row in a view
Example:

DELETE FROM DetailsView


WHERE NAME="Suresh";

If we fetch all the data from DetailsView now as, output:


SELECT * FROM DetailsView;
Summary
•Views are some kind of virtual tables created by original tables from the database.
•Views actually do not hold the actual data and just have the definition of the original data.
•Views act as a proxy or virtual table created from the original table.
 The view has two primary purposes:
 Simplifying complex SQL queries.
 Restricting users from accessing sensitive data.
•View can be created by using the CREATE VIEW statement.
 Simple view is the view that is made from a single table.
 Complex view is the view that is made from multiple tables.
•We can delete or drop a view using the DROP statement
•For updating the views we can use CREATE OR REPLACE VIEW statement.
•Inserting a row in a view follows the same syntax as inserting a row in a plain table.
•Deleting a row in the view follows the same syntax as deleting a row in a plain table
REFERENCES

Reference book :

1. Fundamentals of Database System-Elmasri Ramez , Navathe Shamkant


2. "Database Management Systems" by Raghu Ramakrishnan and Johannes Gehrke -
This book covers the basics of database management systems, including the concept
of index structures.
Terminal QUESTIONS

1. Define a view in DBMS. Explain how views are created, updated, and deleted
with suitable SQL commands and examples.
2. Discuss the advantages and disadvantages of using views in a database system.
Support your answer with real-world examples .
3. In a university database, students should only see their own academic
performance, while teachers can view all student data for their subjects.
Propose view definitions to meet this requirement and explain their usage.
THANK YOU

Team – DBMS

You might also like