Mysql Advanced-Views

introduce

View is a virtual table. The data in the view does not actually exist in the database. The row and column data come from the tables used in the query of the custom view and are dynamically generated when the view is used. In layman's terms, the view only saves the SQL logic of the query and does not save the query results. So when we create a view, the main work falls on creating this SQL query statement. We mainly use views to encapsulate some more complex and commonly used select statements to improve development efficiency

 grammar

Create view

CREATE [OR REPLACE] VIEW 视图名称[(列名列表)] AS SELECT语句 [ WITH [
CASCADED | LOCAL ] CHECK OPTION ]

query view

Just like the operation of query table, when querying data, you can directly operate the view as a temporary table.

查看创建视图语句:SHOW CREATE VIEW 视图名称;
查看视图数据:SELECT * FROM 视图名称 ...... ;

Revise

You can overwrite the old view by creating a new view with the same name to modify it.

方式一:CREATE [OR REPLACE] VIEW 视图名称[(列名列表)] AS SELECT语句 [ WITH
[ CASCADED | LOCAL ] CHECK OPTION ]
方式二:ALTER VIEW 视图名称[(列名列表)] AS SELECT语句 [ WITH [ CASCADED |
LOCAL ] CHECK OPTION ]

delete

DROP VIEW [IF EXISTS] 视图名称 [,视图名称] ...

Case

-- 创建视图 
create or replace view stu_v_1 as select id,name from student where id <= 10;

-- 查询视图 
show create view stu_v_1; select * from stu_v_1; select * from stu_v_1 where id < 3;

-- 修改视图 
create or replace view stu_v_1 as select id,name,no from student where id <= 10;

alter view stu_v_1 as select id,name from student where id <= 10;

-- 删除视图 
drop view if exists stu_v_1;

View insert data

Create a view with a query ID less than or equal to 10. We will find that the data with IDs 6 and 17 can be successfully inserted. But when we execute the query, the data returned does not have a record with ID 17.

create or replace view stu_v_1 as select id,name from student where id <= 10 ;


select * from stu_v_1;

insert into stu_v_1 values(6,'Tom');

insert into stu_v_1 values(17,'Tom22');

Because when we created the view, the specified condition was id<=10, and the data with id 17 did not meet the conditions, so it was not queried. check option, but this data has indeed been successfully inserted into the base table. If we specify conditions when we define a view, then when we insert, modify, or delete data, can we ensure that the conditions must be met before the operation can be performed, otherwise the operation cannot be performed? The answer is yes, you need to use the view's

 Check options

When you create a view using the WITH CHECK OPTION clause, MySQL checks each row that is being changed, such as insert, update, delete, through the view to make it conform to the view's definition. MySQL allows creating a view based on another view, and it also checks the rules in the dependent view for consistency. In order to determine the scope of the check, mysql provides two options: CASCADED and LOCAL , The default value is CASCADED a>.

 CASCADED

cascade. For example, the v2 view is based on the v1 view. If the check option is cascaded when the v2 view is created, but the check option is not specified when the v1 view is created. Then when performing the check, not only v2 will be checked, but also the associated view v1 of v2 will be checked in cascade. That is, if v1 has no check option, the check option of v2 is CASCADED, and v1 will be automatically added. Check this option on CASCADED

 

 LOCAL

 local. For example, the v2 view is based on the v1 view. If the check option is local when the v2 view is created, but the check option is not specified when the v1 view is created. Then when performing the check, only v2 will be checked, and v2's associated view v1 will not be checked. That is, if the upper-level view does not have a check option, it will not be checked

 

 View update

For a view to be updateable, there must be a one-to-one relationship between the rows in the view and the rows in the underlying table, which is also a reflection of the table structure.A view is not updateable if it contains any of the following:

A. Aggregation function or window function (SUM(), MIN(), MAX(), COUNT(), etc.)

B. DISTINCT

C. GROUP BY

D. HAVING E. UNION or UNION ALL

 View function

1). Simple

Views not onlysimplify users' understanding of data, they can alsosimplify their operations a>. Frequently used queries can be defined as views, so that users do not have to specify all the conditions for subsequent operations every time.

2).Safety

The database can be authorized, but it cannot be authorized to specific rows and columns of the database. Throughview users can only query and modify the data they can see.

3). Data independence

Views can help usersshield the impact of real table structure changes.

おすすめ

転載: blog.csdn.net/weixin_64133130/article/details/134227468