0% found this document useful (0 votes)
32 views5 pages

DBMS Ex.9

If7gfr6ugh

Uploaded by

sagarpawar090042
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

DBMS Ex.9

If7gfr6ugh

Uploaded by

sagarpawar090042
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EX.9.

views

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 selec ng 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 condi ons.

-- Create StudentDetails table

CREATE TABLE StudentDetails (

S_ID INT PRIMARY KEY,

NAME VARCHAR(255),

ADDRESS VARCHAR(255)

);

INSERT INTO StudentDetails (S_ID, NAME, ADDRESS)


VALUES
(1, 'Harsh', 'Kolkata'),
(2, 'Ashish', 'Durgapur'),
(3, 'Pra k', 'Delhi'),
(4, 'Dhanraj', 'Bihar'),
(5, 'Ram', 'Rajasthan');

-- Create StudentMarks table

CREATE TABLE StudentMarks (

ID INT PRIMARY KEY,

NAME VARCHAR(255),

Marks INT,

Age INT

);

INSERT INTO StudentMarks (ID, NAME, Marks, Age)


VALUES
(1, 'Harsh', 90, 19),
(2, 'Suresh', 50, 20),
(3, 'Pra k', 80, 19),
(4, 'Dhanraj', 95, 21),
(5, 'Ram', 85, 18);
StudentDetails

StudentMarks

CREATE VIEWS in SQL


We can create a view using CREATE VIEW statement. A View can be created from a single table or
mul ple tables.
Syntax
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condi on;
SQL CREATE VIEW Statement Examples
Let’s look at some examples of CREATE VIEW Statement in SQL to get a be er understanding of how
to create views in SQL.
Example 1: Crea ng 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 2: Crea ng View from mul ple tables


In this example we will create a View named MarksView from two tables StudentDetails and
StudentMarks. To create a View from mul ple tables we can simply include mul ple 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:

UPDATE & DELETE:


Example 1: Update View to Add or Replace a View Field
We can use the CREATE OR REPLACE VIEW statement to add or replace fields from a view.
If we want to update the view MarksView and add the field AGE to this View
from StudentMarks Table, we can do this by:
CREATE OR REPLACE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS, StudentMarks.AGE
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
If we fetch all the data from MarksView now as:
SELECT * FROM MarksView;

OUTPUT:

Example 2: Dele ng a row from a View


Dele ng rows from a view is also as simple as dele ng rows from a table. We can use the DELETE
statement of SQL to delete rows from a view. Also dele ng a row from a view first deletes the row
from the actual table and the change is then reflected in the view.
In this example, we will delete the last row from the view DetailsView which we just added in the
above example of inser ng rows.
DELETE FROM DetailsView
WHERE NAME="Suresh";
If we fetch all the data from DetailsView now as,
SELECT * FROM DetailsView;
OUTPUT:

WITH CHECK OPTION Clause


The WITH CHECK OPTION clause in SQL is a very useful clause for views. It applies to an updatable
view.
The WITH CHECK OPTION clause is used to prevent data modifica on (using INSERT or UPDATE) if the
condi on in the WHERE clause in the CREATE VIEW statement is not sa sfied.
If we have used the WITH CHECK OPTION clause in the CREATE VIEW statement, and if the UPDATE or
INSERT clause does not sa sfy the condi ons then they will return an error.
WITH CHECK OPTION Clause Example:
In the below example, we are crea ng a View SampleView from the StudentDetails Table with a
WITH CHECK OPTION clause.
CREATE VIEW SampleView AS
SELECT S_ID, NAME
FROM StudentDetails
WHERE NAME IS NOT NULL
WITH CHECK OPTION;
In this view, if we now try to insert a new row with a null value in the NAME column then it will give
an error because the view is created with the condi on for the NAME column as NOT NULL. For
example, though the View is updatable then also the below query for this View is not valid:
INSERT INTO SampleView(S_ID)
VALUES(6);

You might also like