![]() |
VOOZH | about |
A SQL View is a virtual table created from the result of a SELECT query. It does not store data physically but displays data stored in underlying tables. Views help simplify complex queries, enhance security, and present data in a cleaner, customized format.
Example: First, we will create a demo SQL database and table, on which we will use the TRUNCATE TABLE command.
Query:
CREATE VIEW StudentView AS
SELECT NAME, ADDRESS
FROM StudentDetails;
SELECT * FROM StudentView;
Output:
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;
First, we will create a demo SQL database and table, on which we will use the View command.
In this example, we 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:
In this example, we 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:
Here are some common operations used to manage views in SQL:
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';
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;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.
Certain conditions need to be satisfied to update a view. If any of these conditions are not met, the view can not be updated.
Here are some advanced ways to work with SQL 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:
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 StudentDetailswhich we have created above in the example of "creating views from a single table".
Example:
INSERT INTO StudentDetails(NAME, ADDRESS)
VALUES("John","German");
If we fetch all the data from DetailsView now as,
SELECT * FROM StudentDetails;Output:
Deleting rows from a view works the same as deleting from a table. Using the DELETE command removes the row from the base table, and the change automatically appears in the view. In this example, we delete the last row from StudentDetails added earlier.
Query:
DELETE FROM StudentDetails
WHERE NAME="John";
If we fetch all the data from DetailsView now as,
SELECT * FROM StudentDetails;Output:
The WITH CHECK OPTION clause ensures that any INSERT or UPDATE on an updatable view must satisfy the viewβs WHERE condition. If the condition is violated, SQL returns an error. In this example, a view is created with WITH CHECK OPTION to restrict changes that fall outside the defined criteria.
Query:
CREATE VIEW SampleView AS
SELECT S_ID, NAME
FROM StudentDetails
WHERE NAME IS NOT NULL
WITH CHECK OPTION;
UPDATE SampleView SET NAME = 'Mark' WHERE S_ID = 2;Output: