当前位置:网站首页>Views in MySQL

Views in MySQL

2022-06-22 13:22:00 Xiaotiantian 666

A view is a virtual table . The data in the view does not actually exist in the database , Row and column data comes from tables used in queries in the definition view , And it's dynamically generated when using views .
Generally speaking , The view only saves the of the query SQL Logic , Do not save query results . So when we create views , The main job is to create this SLQ On the query statement .

  • establish
    CREATE [OR REPLACE] VIEW View name [( List of names )] AS SELECT sentence [WITH [CASCADED | LOCAL] CHECK OPTION]
  • Inquire about
    View the create view statement :
    SHOW CREATE VIEW View name ;
    View view data :
    SELECT * FROM View name .....;
  • modify
    Mode one :CREATE [OR REPLACE] VIEW View name [( List of names )] AS SELECT sentence [WITH [CASCADED | LOCAL] CHECK OPTION]
    Mode two :ALTER VIEW View name [( List of names )] AS SELECT sentence [WITH [CASCADED | LOCAL] CHECK OPTION]
  • Delete
    DROP VIEW [IF EXISTS] View name [, View name ]...;

Check options for view

When using WITH CHECK OPTION Clause when creating a view ,MySQL Each row that is changing is checked through the view , for example : Insert 、 to update 、 Delete , To make it conform to the definition of the view .MySQL Allows you to create a view based on another view , It also checks the rules in the dependency view for consistency . In order to determine the scope of inspection ,mysql There are two options :
CASCADED and LOCAL, The default value is CASCADED.

Update of view

To make the view updatable , There must be a one-to-one relationship between the rows in the view and the rows in the underlying table . If the view contains any of the following , The view is not updatable :
1、 Aggregate function or window function (sum()、min()、max()、count() etc. )
2、DISTINCT
3、GROUP BY
4、HAVING
5、UNION perhaps NUION ALL

The function of view

  • Simple
    Views not only simplify the user's understanding of the data , They can also simplify their operations . Queries that are often used can be defined as views , Thus, the user does not have to specify all the conditions for each subsequent operation .
  • Security
    The database can authorize , However, it cannot be authorized to specific rows and columns in the database . Through view users can only query and modify the data they can see
  • Data independence
    View can help users shield the impact of changes in real table structure .
原网站

版权声明
本文为[Xiaotiantian 666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221230052609.html