A view in SQL is a saved SQL query that acts as a virtual table. Unlike regular tables, views do not store data themselves. Instead, they dynamically generate data by executing the SQL query defined in the view each time it is accessed.
It can fetch data from one or more tables and present it in a customized format, allowing developers to:
- Simplify Complex Queries: Encapsulate complex joins and conditions into a single object.
- Enhance Security: Restrict access to specific columns or rows.
- Present Data Flexibly: Provide tailored data views for different users.
We will be using these two SQL tables for examples.
StudentDetails:
-- 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');
Output:
S_ID | Name | Address |
|---|
1 | Harsh | Kolkata |
|---|
2 | Ashish | Durgapur |
|---|
3 | Pratik | Delhi |
|---|
4 | Dhanraj | Bihar |
|---|
5 | Ram | Rajsthan |
|---|
StudentMarks:
-- 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);
Output:
ID | Name | Marks | Age |
|---|
1 | Harsh | 90 | 19 |
|---|
2 | Suresh | 50 | 20 |
|---|
3 | Pratik | 80 | 19 |
|---|
4 | Dhanraj | 95 | 21 |
|---|
5 | Ram | 85 | 18 |
|---|
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;
Key Terms:
- view_name: Name for the View
- table_name: Name of the table
- condition: Condition to select rows
Example 1: Creating a Simple View from a Single Table
Example 1.1: 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;
Use the below query to retrieve the data from this view
SELECT * FROM DetailsView;
Output:
Name | Address |
|---|
Harsh | Kolkata |
|---|
Ashish | Durgapur |
|---|
Pratik | Delhi |
|---|
Dhanraj | Bihar |
|---|
Example 1.2: Here, we will create a view named StudentNames from the table StudentDetails.
Query:
CREATE VIEW StudentNames AS
SELECT S_ID, NAME
FROM StudentDetails
ORDER BY NAME;
If we now query the view as,
SELECT * FROM StudentNames;
Output:
S_ID | Name |
|---|
2 | Ashish |
|---|
4 | Dhanraj |
|---|
1 | Harsh |
|---|
3 | Pratik |
|---|
5 | Ram |
|---|
Example 2: Creating a View From Multiple Tables
In this example we will create a View MarksView that combines data from bothtables StudentDetails and StudentMarks. To create a View from multiple tables we can simply include multiple 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:
Name | Address | Marks |
|---|
Harsh | Kolkata | 90 |
|---|
Pratik | Delhi | 80 |
|---|
Dhanraj | Bihar | 95 |
|---|
Ram | Rajsthan | 85 |
|---|
Managing Views: Listing, Updating, and Deleting
1. Listing all Views in a Database
We can list all the views in a database using the SHOW FULL TABLES statement or by querying the information_schema tables.
USE "database_name";
SHOW FULL TABLES WHERE table_type LIKE "%VIEW";
Using information_schema
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';
2. Deleting a View
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;
3. Updating a View Definition
If we want to update the existing data within the view, use the UPDATE statement.
UPDATE view_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
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:
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Note: Not all views can be updated using the UPDATE statement.
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.
- 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.
Advanced Techniques with Views
1. Updating Data Through Views
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:
Example:
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:
Name | Address | Marks | Age |
|---|
Harsh | Kolkata | 90 | 19 |
|---|
Pratik | Delhi | 80 | 19 |
|---|
Dhanraj | Bihar | 95 | 21 |
|---|
Ram | Rajasthan | 85 | 18 |
|---|
2. Inserting Data into Views
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".
Example:
INSERT INTO DetailsView(NAME, ADDRESS)
VALUES("Suresh","Gurgaon");
If we fetch all the data from DetailsView now as,
SELECT * FROM DetailsView;
Output:
Name | Address |
|---|
Harsh | Kolkata |
|---|
Ashish | Durgapur |
|---|
Pratik | Delhi |
|---|
Dhanraj | Bihar |
|---|
Suresh | Gurgaon |
|---|
3. 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.
Example:
DELETE FROM DetailsView
WHERE NAME="Suresh";
If we fetch all the data from DetailsView now as,
SELECT * FROM DetailsView;
Output:
Name | Address |
|---|
Harsh | Kolkata |
|---|
Ashish | Durgapur |
|---|
Pratik | Delhi |
|---|
Dhanraj | Bihar |
|---|
4. WITH CHECK OPTION Clause
The WITH CHECK OPTION clause in SQL is a very useful clause for views. It applies to an updatable view. It is used to prevent data modification (using INSERT or UPDATE) if the condition in the WHERE clause in the CREATE VIEW statement is not satisfied.
If we have used the WITH CHECK OPTION clause in the CREATE VIEW statement, and if the UPDATE or INSERT clause does not satisfy the conditions then they will return an error. In the below example, we are creating a View SampleView from the StudentDetails Table with a WITH CHECK OPTION clause.
Example:
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 condition 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);
Explore
Basics
Queries & Operations
SQL Joins & Functions
Data Constraints & Aggregate Functions
Advanced SQL Topics
Database Design & Security
My Profile