SQL Views (Views)
A view is a visual table.
This chapter explains how to create, update, and delete views.
SQL CREATE VIEW Statement
In SQL, a view is a visual table based on the result set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from real tables in one or more databases.
You can add SQL functions, WHERE, and JOIN statements to a view, and present data as if it came from a single table.
SQL CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Parameter Description:
- CREATE VIEW: Declares that you want to create a view.
- view_name: Specifies the name of the view.
- AS: Specifies the keyword indicating the start of the view definition.
- SELECT column1, column2, ...: Specifies the columns included in the view, which can be table columns or computed columns.
- FROM table_name: Specifies the table from which the view retrieves data.
- WHERE condition: An optional part used to specify filter conditions that limit the rows in the view.
**Note:** A view always shows the latest data! Whenever a user queries a view, the database engine rebuilds the data using the view's SQL statement.
SQL CREATE VIEW Example
Suppose you have a table employees that contains employee information, with the following columns: employee_id, first_name, last_name, salary, and department_id. Now we will create a view that displays information about employees whose salary is above a certain threshold.
The example is as follows:
-- 创建包含高工资员工信息的视图
CREATE VIEW high_salary_employees AS
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE salary > 50000;
In this example, we create a view named high_salary_employees that contains information about employees whose salary is higher than 50000.
You can now use this view just like a regular table:
-- 查询高工资员工视图
SELECT *
FROM high_salary_employees;
This returns detailed information about all employees earning more than 50000, without having to write the same filter condition every time.
It is worth noting that a view is essentially a virtual table: it does not store data itself but is generated from the results of a query on the underlying tables. Therefore, if the data in the underlying tables changes, the contents of the view are updated accordingly.
SQL Updating a View
In SQL, you cannot directly use an UPDATE statement to update a view, because a view is a virtual table generated from a query result rather than a table that actually stores data.
Updating a view essentially means updating the data in the tables on which the view is based; the view will then reflect those changes.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Here, table_name is the name of the underlying table, column1, column2, ... are the columns to update, value1, value2, ... are the new values, and condition is the update condition.
Now we want to add a Category column to the Current Product List view. We will update the view with the following SQL:
For example, if you have a view named high_salary_employees that shows employees earning more than 50000 and is based on the results of a query on the employees table, you can update the data with the following steps:
-- 步骤 1: 更新 employees 表中的数据
UPDATE employees
SET salary = 60000
WHERE employee_id = 1001;
-- Step 2: Query the updated high-salary employee view
SELECT *
FROM high_salary_employees;
In this way, you update the data in the employees table, and the view high_salary_employees will reflect these changes.
SQL Dropping a View
In SQL, dropping (or deleting) a view is accomplished with the DROP VIEW statement.
The DROP VIEW statement is used to delete an existing view from the database. Its syntax is as follows:
DROP VIEW [IF EXISTS] view_name;
Parameter Description:
-
DROP VIEW: Indicates the view you want to delete.
-
IF EXISTS: An optional clause used to check whether the view exists. If it exists, the deletion is performed; if it does not, no error occurs. In some database systems this is optional.
-
view_name: Specifies the name of the view to delete.
After running the following statement, the view high_salary_employees will be deleted from the database.
-- 删除名为 high_salary_employees 的视图
DROP VIEW IF EXISTS high_salary_employees;
Note that this does not affect the data in the underlying tables; only the definition of the view is removed.
If you need to revoke or delete data in a table, you should use the DROP TABLE statement.
When using the DROP VIEW statement, make sure you really want to delete the view, because once it is deleted, its definition cannot be recovered.