SQL column
Summary of basic knowledge of SQL database
Summary of advanced knowledge of SQL database
Definition of view
The view is a visual table.
This chapter explains how to create, update, and delete views.
CREATE VIEW statement
In SQL, a view is a visualization table based on the result set of the SQL statement.
The view contains rows and columns, just like a real table. The fields in the view are the fields in one or more real tables in the database.
You can add SQL functions, where to add, and connect statements to the view, or you can present the data as if the data came from a single table.
CREATE VIEW syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Note: The view always shows the latest data! Whenever a user queries a view, the database engine uses the SQL statement of the view to reconstruct the data.
CREATE VIEW example,
let’s take the table Customers as an example
The "Customer_GD" (Guangdong customer list) view lists all the customers in Guangdong Province from the "Customers" table. This view was created using the following SQL:
CREATE VIEW Customer_GD AS
SELECT *
FROM Customers
WHERE 省份='广东省'
We can query the above view like this:
SELECT * FROM Custom![](https://s4.51cto.com/images/blog/202103/10/3f424a514eeddce0d8825a955c6bed5a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)er_GD
Take a look at the results in the view:
Below we take the "Products" table as an example
We select all products whose prices are higher than the average price in the "Products" table:
CREATE VIEW [Products_Above_Average_Price] AS
SELECT 名称,价格
FROM Products
WHERE 价格>(SELECT AVG(价格) FROM Products)
We can query the above view like this:
SELECT * FROM [Products_Above_Average_Price]
The results are as follows:
We can also add conditions to the view. Now, we only need to check the price of the "rice" category:
SELECT * FROM [Products_Above_Average_Price]
WHERE 名称='大米'
This will only show the price of rice
Updating the view
You can use the following syntax to update the view:
CREATE OR REPLACE VIEW 语法
ALTER VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Now, we want to add a "Specification" column to the "Products_Above_Average_Price" view. We will update the view with the following SQL:
ALTER VIEW [Products_Above_Average_Price] AS
SELECT 名称,价格,规格
FROM Products
WHERE 价格>(SELECT AVG(价格) FROM Products)
We re-query the view:
SELECT * FROM [Products_Above_Average_Price]
The results are as follows:
To delete a view,
you can delete a view with the DROP VIEW command.
DROP VIEW 语法
DROP VIEW view_name
For example, we want to delete the newly created view "Products_Above_Average_Price"
DROP VIEW [Products_Above_Average_Price]
After re-querying the view, it will show that the view is invalid
Prove that the view no longer exists.
annotation
View is a relatively important part of the database. On the premise of isolating the entity table, it can also allow users to query the required data, which can play a role in protecting the underlying data. At the same time, for different groups of people, the view can also play a role in simplifying the user's understanding of the data.