0% found this document useful (0 votes)
28 views23 pages

U2-5 Views

Uploaded by

Dexter
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)
28 views23 pages

U2-5 Views

Uploaded by

Dexter
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/ 23

UNIT II

RELATIONAL MODEL AND SQL

Relational model concepts -- Integrity

constraints -- SQL Data manipulation – SQL

Data definition –Views -- SQL programming.


• A View in SQL is simply a virtual table created based
on a result set of another SQL statement.
• Views were introduced to reduce the complexity of
multiple tables and deliver data in a simple manner.
• Views help us maintain data integrity and provide
security to the data, thus acting as a security
mechanism.
Use of Views in SQL

• Views were introduced to reduce the complexity of the

multiple tables and deliver data in a simple manner.

• Views hide the complexity of the data in the database as they

join and simplify multiple tables into a single virtual table,

which is easier for a user to understand.


• Views also maintain data integrity as it presents
a consistent and accurate image of the data from the database
even if the underlying source is restructured, renamed,
or split.
• It can automatically check the data which a user or any other
third party is trying to access meets the conditions mentioned
in the views to maintain accuracy while displaying data.
SQL Creating a View:
CREATE VIEW syntax :
• CREATE VIEW view_name AS SELECT column1,
column2...column N FROM table1, table2...table N WHERE
condition;
• To see the data in the view, we can query the view using the
following SELECT statement:
• SELECT * FROM [view_name];
• Table 1 (ScalerCourses)
No Name Duration CourseLanguage Cost(Rs)

1 Python Foundation 3-4 months English 1500

2 Django 5 months English 1000

3 C++ 4-5 months Hindi 500

4 Interview Preparation 6 months English 1800

5 Node Js 6 months Hindi 2500


• Table 2 (AuthorDetails)
SNo Name Rating

1 Anshuman 5

2 Ravi 4

3 Raman 4.5

4 Yash 5

5 Jatin 4.5
Synax:
CREATE VIEW CourseView AS
SELECT Name, Duration
FROM ScalerCourses
WHERE Cost < 2000;
SELECT * FROM CourseView;
The output of the above query:
Name Duration

Python Foundation 3-4 months

Django 5 months

C++ 4-5months

Interview Preparation 6 months


• SQL Updating a View
• CREATE OR REPLACE VIEW CourseView AS SELECT
Name, Duration, CourseLanguage FROM ScalerCourses
WHERE Cost < 2000;
SELECT * FROM CourseView;
Output for the above statements is as follows:
Name Duration Course Language

Python Foundation 3-4 months English

Django 5 months English

C++ 4-5months Hindi

Interview
6 months English
Preparation
Inserting Rows in a View
• INSERT INTO CourseView(Name, Duration)
VALUES(“Java”, “4 months”);
• The following will be the data stored in our view after
executing the above query.
Name Duration
Python Foundation 3-4 months
Django 5 months
C++ 4-5months
Interview Preparation 6 months
Java 4 months
• Deleting Rows into a View
• DELETE FROM CourseView
WHERE Name = “Python Foundation”;
The data after executing the above query looks as follows.

Name Duration

Django 5 months

C++ 4-5months

Interview Preparation 6 months

Java 4 months
• SQL Dropping a View
DROP VIEW view_name;
For Example:
DROP VIEW CourseView;
• CREATE VIEW CourseView AS
SELECT Name, Duration
FROM ScalerCourses
WHERE Cost < 2000
WITH CHECK OPTION;
Now, if anyone inserts a new value to the view, it can be done as
follows:
INSERT INTO CourseView(Name, Duration, Cost)
VALUES(“Ruby”, “7 months”, 3000);
The above statement inserted a row that makes the condition in the
WHERE clause (Cost<2000) not true. This did not follow the
condition mentioned, so the following error message will be shown.
Error Code: 1369. CHECK OPTION failed
'classicmodels.CourseView'
• Managing SQL Views
• For managing the views, there are different aspects which
were quite explained well in the above topics like:
• Creating Views
• Updating Views(Replacing Views)
• Inserting Rows
• Renaming Views
• Deleting Views(Drop Statement)
• Listing Views(By querying we can list all the views in the
Database)
• Types of Views in SQL
• 1. System Defined View
The System Defined Views are always, accordingly, linked to
some User-Defined databases.
Information Schema View
SELECT * FROM INFORMATION_SCHEMA.CourseView
where TABLE_NAME='ScalerCourses‘
• Output:
All the rows and columns of the particular table (i.e.,
ScalerCourses here) which are selected are displayed. It displays
the “detailed information” of that table.
• Catalog View :
select * from sys.databases
Output:
Dynamic Management View:
Output:
2. User Defined View
• Simple View – Such views are created based on a
single table. In simple views, all such operations of
update, delete, etc., are possible.
• Creating a View from a single table is discussed
below for your further understanding.
• Complex View – On the other hand, if a view is created from

more than one table, it is called a complex view.

• Such views can contain group data. Moreover, in a complex

view, all such operations of update, delete and insert are

impossible
• Creating Views from Multiple Tables
• CREATE VIEW CourseView AS SELECT

ScalerCourses.Name, ScalerCourses.Duration,

AuthorDetails.Author

FROM ScalerCourses, AuthorDetails

WHERE Cost < 2000;

• SELECT * FROM CourseView;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy