SQL Views
Last updated
Last updated
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.
In this article, we will learn about creating, updating, and deleting views in SQL.
We will be using these two SQL tables for examples.
StudentDetails
StudentMarks
You can create these tables on your system by writing the following SQL query:
MySQL-- Create StudentDetails tableCREATE 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 tableCREATE 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);
We can create a view using CREATE VIEW statement. A View can be created from a single table or multiple tables.
Parameters:
view_name: Name for the View
table_name: Name of the table
condition: Condition to select rows
Let’s look at some examples of CREATE VIEW Statement in SQL to get a better understanding of how to create views in SQL.
In this example, we will create a View named DetailsView from the table StudentDetails. Query:
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:
If we now query the view as,
Output:
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:
To display data of View MarksView:
Output:
We can list View using the SHOW FULL TABLES statement or using the information_schema table. A View can be created from a single table or multiple tables.
Using information_schema
SQL allows us to delete an existing View. We can delete or drop View using the DROP statement.
In this example, we are deleting the View MarksView.
If you want to update the existing data within the view, use the UPDATE statement.
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
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.
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.
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:
If we fetch all the data from MarksView now as:
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”.
If we fetch all the data from DetailsView now as,
Output:
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.
If we fetch all the data from DetailsView now as,
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.
WITH CHECK OPTION Clause Example:
In the below example, we are creating a View SampleView from the StudentDetails Table with a WITH CHECK OPTION clause.
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:
NOTE: The default value of NAME column is null.
A good database should contain views for the given reasons:
Restricting data access – Views provide an additional level of table security by restricting access to a predetermined set of rows and columns of a table.
Hiding data complexity – A view can hide the complexity that exists in multiple joined tables.
Simplify commands for the user – Views allow the user to select information from multiple tables without requiring the users to actually know how to perform a join.
Store complex queries – Views can be used to store complex queries.
Rename Columns – Views can also be used to rename the columns without affecting the base tables provided the number of columns in view must match the number of columns specified in a select statement. Thus, renaming helps to hide the names of the columns of the base tables.
Multiple view facility – Different views can be created on the same table for different users.
Views in SQL are a kind of virtual table.
The fields in a view can be from one or multiple tables.
We can create a view using the CREATE VIEW statement and delete a view using the DROP VIEW statement.
We can update a view using the CREATE OR REPLACE VIEW statement.
WITH CHECK OPTION clause is used to prevent inserting new rows that do not satisfy the view’s filtering condition.