当前位置:网站首页>MySQL learning summary 10: detailed explanation of view use
MySQL learning summary 10: detailed explanation of view use
2022-06-13 03:24:00 【koping_ wu】
Mysql Learning summary ten : View usage details
1、 View overview
1.1 Why use views ?
On the one hand, views can help us use some of the tables instead of all the tables , On the other hand, you can also make different query views for different users .
such as , For a company's salesperson , We just want to show him some data , And some special data , For example, the purchase price , Will not be provided to him .
Another example , Personnel compensation is a sensitive field , Then it is only open to people above a certain level , This field is not available in other people's query views .
What I just talked about is just a usage scenario of the view , In fact, views have many functions .
1.2 View understanding
- View , It can be regarded as a virtual table , Itself does not store data . The essence of view , Can be seen as stored SELECT sentence .
- In the view SELECT The table involved in the statement , Called the base table .
- Do... For the view DML operation , It will affect the data in the corresponding base table . vice versa .
- Deletion of the view itself , It will not cause the deletion of data in the base table .
- Application scenarios of view : For small projects ( For example, dozens of tables ), Views are not recommended . For large projects ( For example, hundreds of tables ), Consider using views .
- Benefits of views : Simplify queries ; Control access to data .
2、 Create view
CREATE [OR REPLACE]
[ALGORITHM = {
UNDEFINED | MERGE | TEMPTABLE}]
VIEW View name [( Field list )]
AS Query statement
[WITH [CASCADED|LOCAL] CHECK OPTION]
Lite version :
CREATE VIEW View name
AS Query statement
2.1 Create a single table view
Take a chestnut :
CREATE VIEW empvu80
AS
SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 80;
2.2 Create a multi table union view
Take a chestnut :
CREATE VIEW emp_dept
AS
SELECT ename,dname
FROM t_employee LEFT JOIN t_department
ON t_employee.did = t_department.did;
2.3 Create a view based on the view
When we create a view , You can also continue to create views on top of it .
give an example : union “emp_dept” The view and “emp_year_salary” View query employee name 、 Department name 、 Annual salary information creation
“emp_dept_ysalary” View .
CREATE VIEW emp_dept_ysalary
AS
SELECT emp_dept.ename,dname,year_salary
FROM emp_dept INNER JOIN emp_year_salary
ON emp_dept.ename = emp_year_salary.ename;
3、 View view
grammar 1: View the table objects of the database 、 View objects
SHOW TABLES;
grammar 2: Look at the structure of the view
DESC / DESCRIBE View name ;
grammar 3: View the attribute information of the view , Execution results show , notes Comment by VIEW, Explain that the table is a view , Other information is NULL, It means that this is a virtual table .
# View view information ( Display the storage engine of the data table 、 edition 、 Number of data rows and data size, etc )
SHOW TABLE STATUS LIKE ' View name '\G
grammar 4: View the detailed definition information of the view
SHOW CREATE VIEW View name ;
4、 Update the view
4.1 General situation
MySQL Support use INSERT、UPDATE and DELETE Statement to insert data in a view 、 Update and delete operations . When the data in the view changes , The data in the data table will also change , vice versa .
4.2 Non updatable views
To make the view updatable , Depending on the There must be... Between the rows in the diagram and the rows in the underlying basic table one-on-one The relationship between . In addition, when the view definition is as follows , View does not support update operation :
- When defining the view, you specify “ALGORITHM = TEMPTABLE”, View will not support INSERT and DELETE operation ;
- The view does not contain all columns in the base table that are defined as non empty and do not specify a default value , View will not support INSERT operation ;
- In defining the SELECT Used in the statement JOIN The joint query , View will not support INSERT and DELETE operation ;
- In defining the SELECT The field list after the statement uses Mathematical expression or Subquery , View will not support INSERT, Nor does it support UPDATE Mathematical expressions are used 、 The field value of the subquery ;
- In defining the SELECT Statement is used in the field list after the DISTINCT 、 Aggregate functions 、 GROUP BY 、 HAVING 、UNION etc. , View will not support INSERT、UPDATE、DELETE;
- In defining the SELECT Statement contains a subquery , The subquery refers to FROM The watch at the back , View will not support
INSERT、UPDATE、DELETE; - The view definition is based on a Non updatable view ;
- Constant view .
Although view data can be updated , But on the whole , View as Virtual table , It is mainly used for Convenient query , It is not recommended to update the data of the view . Changes to view data , It is done by operating the data in the actual data table .
5、 modify 、 Delete view
5.1 Modify the view
The way 1: Use CREATE OR REPLACE VIEW Clause to modify the view
CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS
SELECT
employee_id, first_name || ' ' || last_name, salary, department_id
FROM employees
WHERE department_id = 80;
The way 2:ALTER VIEW
ALTER VIEW View name
AS
Query statement
5.2 Delete view
Deleting a view just deletes the definition of the view , The data of the base table will not be deleted .
The syntax for deleting a view is :
DROP VIEW IF EXISTS View name ;
DROP VIEW IF EXISTS View name 1, View name 2, View name 3,...;
Be careful : View based a、b Created a new view c, If the view a Or view b Delete , Will cause the view c Your query failed . this
Sample view c Need to manually delete or modify , Otherwise, it will affect the use of .
6、 Advantages and disadvantages of views
6.1 Benefits of views
1) It's easy to operate
Define frequently used query operations as views , It can make developers do not need to care about the structure of the data table corresponding to the view 、 The relationship between tables , There is no need to care about the business logic and query conditions between data tables , Simply manipulate the view , It greatly simplifies the operation of developers on the database .
2) Reduce data redundancy
The view is different from the actual data table , It stores query statements . therefore , In use , We want to get the result set by defining the query statement of the view . The view itself does not store data , Do not occupy the resources of data storage , Reduced data redundancy .
3) Data security
MySQL The user's response to the data Access restrictions On the result set of some data , The result set of these data can be realized by using views . Users do not have to query or manipulate the data table directly . This can also be understood as the view has Isolation, . The view is equivalent to adding a layer of virtual tables between the user and the actual data table .
meanwhile ,MySQL Users' access to data can be restricted to some views according to their permissions , Users do not need to query the data table , The information in the data table can be obtained directly through the view . This ensures the security of the data in the data table to a certain extent .
4) Adapt to flexible needs
When the requirements of the business system change , If you need to change the structure of the data table , The workload is relatively large , You can use views to reduce the amount of change . This method is often used in practical work .
5) Ability to decompose complex query logic
If there is complex query logic in the database , be The problem can be decomposed , Create multiple views to get data , Then combine the created multiple views , Complete complex query logic .
6.2 Disadvantages of deletion
If we create a view based on the actual data table , that , If the structure of the actual data table changes , We need to maintain the relevant views in time . Especially nested views ( Is to create a view based on the view ), Maintenance will become more complex , Poor readability , It can easily become a potential hidden danger of the system . Because the view is created SQL Queries may rename fields , It may also contain complex logic , These will increase the cost of maintenance .
In actual projects , If there are too many views , It will lead to the problem of database maintenance cost .
therefore , When creating views , you It should be combined with the actual project needs , Comprehensively consider the advantages and disadvantages of view , In this way, the view can be used correctly , Make the whole system optimal .
边栏推荐
- Polymorphism in golang
- [JVM Series 5] performance testing tool
- Personal understanding of grammar sugar
- Reading notes of effective managers
- Level II C preparation -- basic concepts of program design
- [JVM Series 5] JVM tuning instance
- JVM JMM (VI)
- Es and kibana deployment and setup
- Introduction to redis (using redis, common commands, persistence methods, and cluster operations)
- How to Draw Useful Technical Architecture Diagrams
猜你喜欢
Sparksql of spark
[JVM Series 7] garbage collector
Azure SQL db/dw series (13) -- using query store (2) -- report Introduction (2)
MASA Auth - SSO与Identity设计
Use and arrangement of wechat applet coordinate position interface (I)
MySQL index bottom layer (I)
MySQL 8.0 installation free configuration method
Review notes of RS data communication foundation STP
Alibaba cloud OSS access notes
Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
随机推荐
Using linked list to find set union
Common command records of redis client
Mongodb index -index
Detailed explanation of curl command
Available types in C #_ Unavailable type_ C double question mark_ C question mark point_ C null is not equal to
Personal understanding of grammar sugar
Summary of rust language practice
C simple understanding - generics
How to write product requirements documents
Use cypher to get the tree of the specified structure
C # simple understanding - static members and static classes
[JVM Series 5] performance testing tool
Wechat applet coordinate location interface usage (II) map interface
MySQL imports and exports multiple libraries at one time
Use of jstack
Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
MySQL index bottom layer (I)
C 10 new features_ C 10 new features
Alibaba cloud OSS access notes
C# . NET ASP. Net relationships and differences