U2-5 Views
U2-5 Views
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
Django 5 months
C++ 4-5months
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
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
impossible
• Creating Views from Multiple Tables
• CREATE VIEW CourseView AS SELECT
ScalerCourses.Name, ScalerCourses.Duration,
AuthorDetails.Author