SQL Views
SQL Views
SQL Views
Last Updated : 11 Nov, 2024
Views in SQL are a type of virtual table that simplifies how users interact with
data across one or more tables. Unlike traditional tables, a view in SQL does
not store data on disk; instead, it dynamically retrieves data based on a pre-
defined query each time it’s accessed.
SQL views are particularly useful for managing complex queries, enhancing
security, and presenting data in a simplified format. In this guide, we will cover
the SQL create view statement, updating and deleting views, and using the
WITH CHECK OPTION clause.
StudentDetails
StudentMarks
https://www.geeksforgeeks.org/sql-views/ 1/15
24/11/2024, 15:11 SQL Views
You can create these tables on your system by writing the following SQL
query:
MySQL
https://www.geeksforgeeks.org/sql-views/ 2/15
24/11/2024, 15:11 SQL Views
Syntax
Parameters:
In this example, we will create a View named DetailsView from the table
StudentDetails. Query:
https://www.geeksforgeeks.org/sql-views/ 3/15
24/11/2024, 15:11 SQL Views
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.
Output:
In this example, we will create a view named StudentNames from the table
StudentDetails. Query:
Output:
https://www.geeksforgeeks.org/sql-views/ 4/15
24/11/2024, 15:11 SQL Views
In this example we will create a View named MarksView from two tables
StudentDetails and StudentMarks. To create a View from multiple tables we
can simply include multiple tables in the SELECT statement. Query:
Output:
Syntax
USE "database_name";
SHOW FULL TABLES WHERE table_type LIKE "%VIEW";
Using information_schema
https://www.geeksforgeeks.org/sql-views/ 5/15
24/11/2024, 15:11 SQL Views
SELECT table_name
FROM information_schema.views
WHERE table_schema = 'database_name';
OR
Example
Syntax
UPDATE view_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Note: Not all views can be updated using the UPDATE statement.
https://www.geeksforgeeks.org/sql-views/ 6/15
24/11/2024, 15:11 SQL Views
If you want to update the view definition without affecting the data, use the
CREATE OR REPLACE VIEW statement. you can use this syntax
1. The SELECT statement which is used to create the view should not include
GROUP BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex queries.
5. 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.
Examples
Let’s look at different use cases for updating a view in SQL. We will cover
these use cases with examples to get a better understanding.
If we want to update the view MarksView and add the field AGE to this View
from StudentMarks Table, we can do this by:
https://www.geeksforgeeks.org/sql-views/ 7/15
24/11/2024, 15:11 SQL Views
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 DetailsView which
we have created above in the example of “creating views from a single table”.
Output:
https://www.geeksforgeeks.org/sql-views/ 8/15
24/11/2024, 15:11 SQL Views
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.
Output:
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 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 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:
Uses of a View
A good database should contain views for the given reasons:
https://www.geeksforgeeks.org/sql-views/ 10/15
24/11/2024, 15:11 SQL Views
"This course is very well structured and easy to learn. Anyone with zero
experience of data science, python or ML can learn from this. This course
makes things so easy that anybody can learn on their own. It's helping me a lot.
Thanks for creating such a great course."- Ayushi Jain | Placed at Microsoft
Get hands on practice with 40+ Industry Projects, regular doubt solving
sessions, and much more. Register for the Program today!
https://www.geeksforgeeks.org/sql-views/ 11/15
24/11/2024, 15:11 SQL Views
Similar Reads
Difference Between Views and Materialized Views in PL/SQL
In PL/SQL views play an important role in data accessibility, data manipulation,
and query optimization. Views help restrict the user's data, which is not suppose…
5 min read
SQL | Views
In SQL, a view is a virtual table based on the result-set of an SQL statement. A
view also has rows and columns as they are in a real table in the database. We…
3 min read
https://www.geeksforgeeks.org/sql-views/ 12/15
24/11/2024, 15:11 SQL Views
MongoDB Views
In MongoDB, there are two primary types of views such as Standard Views and
On-demand Materialized Views. These views offer different ways of managing…
7 min read
Company Languages
About Us Python
Legal Java
In Media C++
Contact Us PHP
Advertise with us GoLang
GFG Corporate Solution SQL
Placement Training Program R Language
GeeksforGeeks Community Android Tutorial
Tutorials Archive
https://www.geeksforgeeks.org/sql-views/ 15/15