0% found this document useful (0 votes)
33 views7 pages

Views and Triggers

Uploaded by

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

Views and Triggers

Uploaded by

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

SQL 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 selecting 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
conditions.
-- 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, 'Pratik', '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, 'Pratik', 80, 19),
(4, 'Dhanraj', 95, 21),
(5, 'Ram', 85, 18);

1. 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
SELECT column1, column2.....
FROM table_name
WHERE condition;

Parameters:
view_name: Name for the View
table_name: Name of the table
condition: Condition to select rows

Example 1: Creating View from a single table


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;

Example 2: Creating View from multiple tables


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;
2. LISTING ALL VIEWS IN A DATABASE
Syntax
USE "database_name";
SHOW FULL TABLES WHERE table_type LIKE "%VIEW";
OR
SELECT table_name
FROM information_schema.views
WHERE table_schema = 'database_name';
OR
SELECT table_schema, table_name, view_definition
FROM information_schema.views
WHERE table_schema = 'database_name';
OR
Show tables;
Show full tables;
SHOW FULL TABLES WHERE table_type LIKE "%VIEW";
3. DELETE VIEWS in SQL
SQL allows us to delete an existing View. We can delete or drop View using the DROP
statement.
Syntax
DROP VIEW view_name;
Example
In this example, we are deleting the View MarksView.
DROP VIEW MarksView;
4. UPDATE VIEW in SQL
If you 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];
Note: Not all views can be updated using the UPDATE statement.
If you want to update the view definition without affecting the data, use the CREATE OR
REPLACE VIEW statement. you can use this syntax
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
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 cannot be updated.
 The SELECT statement which is used to create the view should not include GROUP
BY clause or ORDER BY clause.
 The SELECT statement should not have the DISTINCT keyword.
 The View should have all NOT NULL values.
 The view should not be created using nested queries or complex queries.
 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.

Example 1: Update View to Add or Replace a View Field


CREATE OR REPLACE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS,
StudentMarks.AGE
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;

SELECT * FROM MarksView;

Example 2: Deleting a row from 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. Also deleting 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 inserting rows.
DELETE FROM DetailsView
WHERE NAME=" Dhanraj";

If we fetch all the data from DetailsView now as,


SELECT * FROM DetailsView;

Example 3: Update View to Insert a row in a view


We can insert a row in a View in the same way as we do in a table. We can use the
INSERT INTO statement of SQL to insert a row in a View.
In the below example, we will insert a new row in the View DetailsView which we
have created above in the example of “creating views from a single table”.
INSERT INTO DetailsView(NAME, ADDRESS)
VALUES("Suresh","Gurgaon");
If we fetch all the data from DetailsView now as,

SELECT * FROM DetailsView;

CREATE VIEW DetailsView1 AS


SELECT s_id, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;

INSERT INTO DetailsView1(s_id, ADDRESS)


VALUES(4,"Gurgaon");
CREATE TABLE Student_Trigger (
Student_RollNo INT NOT NULL PRIMARY KEY,
Student_FirstName VARCHAR(100),
Student_EnglishMarks INT,
Student_PhysicsMarks INT,
Student_ChemistryMarks INT,
Student_MathsMarks INT,
Student_TotalMarks INT,
Student_Percentage DECIMAL(5, 2)
);
INSERT INTO Student_Trigger (Student_RollNo, Student_FirstName,
Student_EnglishMarks, Student_PhysicsMarks, Student_ChemistryMarks,
Student_MathsMarks)
VALUES
(1, 'John', 85, 90, 80, 95),
(2, 'Emma', 75, 85, 90, 80),
(3, 'Michael', 90, 95, 85, 88),
(4, 'Sophia', 80, 75, 92, 85);
CREATE TRIGGER Student_After_Insert
BEFORE INSERT ON Student_Trigger
FOR EACH ROW
SET NEW.Student_TotalMarks = NEW.Student_EnglishMarks +
NEW.Student_PhysicsMarks + NEW.Student_ChemistryMarks +
NEW.Student_MathsMarks, NEW.Student_Percentage = (NEW.Student_TotalMarks /
400) * 100;

Select * from Student_Trigger;

INSERT INTO Student_Trigger (Student_RollNo, Student_FirstName,


Student_EnglishMarks, Student_PhysicsMarks, Student_ChemistryMarks,
Student_MathsMarks)
VALUES (201, 'Sorya', 88, 75, 69, 92);

Select * from Student_Trigger;

You might also like