Co2 - Views in Dbms
Co2 - 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
Syntax:
Query:
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;
Retrieve the data from this view
Query:
Output:
Create another view named StudentNames from the table StudentDetails.
Query:
To create a View from multiple tables we can simply include multiple tables in
the SELECT statement.
Query:
Output
Deleting Views in DBMS
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:
Example:
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:
In this example, we will insert a new row in the View DetailsView which we have
created already
Example:
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
Reference book :
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