|
| 1 | +原文地址:https://www.mysqltutorial.org/mysql-views/ |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +# MySQL Views |
| 6 | + |
| 7 | +**Summary**: in this tutorial, you will learn about MySQL views and how to manipulate views effectively. |
| 8 | + |
| 9 | +## Introduction to MySQL Views |
| 10 | + |
| 11 | +Let’s see the following tables `customers` and `payments` from the [sample database](https://www.mysqltutorial.org/getting-started-with-mysql/mysql-sample-database/). |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +This [query](https://www.mysqltutorial.org/mysql-basics/mysql-select-from/) returns data from both tables `customers` and `payments` using the [inner join](https://www.mysqltutorial.org/mysql-basics/mysql-inner-join/): |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +Here is the output: |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +Next time, if you want to get the same information including customer name, check number, payment date, and amount, you need to issue the same query again. |
| 24 | + |
| 25 | +One way to do this is to save the query in a file, either .txt or .sql file so that later you can open and execute it from MySQL Workbench or any other MySQL client tools. |
| 26 | + |
| 27 | +A better way to do this is to save the query in the database server and assign a name to it. This named query is called a **database view,** or simply, **view**. |
| 28 | + |
| 29 | +By definition, a view is a named query stored in the database catalog. |
| 30 | + |
| 31 | +To create a new view you use the `CREATE VIEW` statement. This statement creates a view `customerPayments` based on the above query above: |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +After you execute the `CREATE VIEW` statement, MySQL creates the view and stores it in the database. |
| 36 | + |
| 37 | +Now, you can reference the view as a table in SQL statements. For example, you can query data from the `customerPayments` view using the `SELECT` statement: |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +As you can see, the syntax is much simpler. |
| 42 | + |
| 43 | +Note that a view does not physically store the data. When you issue the `SELECT` statement against the view, MySQL executes the underlying query specified in the view’s definition and returns the result set. For this reason, sometimes, a view is referred to as a virtual table. |
| 44 | + |
| 45 | +MySQL allows you to create a view based on a `SELECT` statement that retrieves data from one or more tables. This picture illustrates a view based on columns of multiple tables: |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +In addition, MySQL even allows you to create a view that does not refer to any table. But you will rarely find this kind of view in practice. |
| 50 | + |
| 51 | +For example, you can create a view called daysofweek that return 7 days a week by executing the following query: |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +You can query data from the daysofweek view as follows: |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +This picture shows the output: |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## Advantages of MySQL Views |
| 64 | + |
| 65 | +MySQL views bring the following advantages. |
| 66 | + |
| 67 | +### 1) Simplify complex query |
| 68 | + |
| 69 | +Views help simplify complex queries. If you have any frequently used complex query, you can create a view based on it so that you can reference the view by using a simple `SELECT` statement instead of typing the query all over again. |
| 70 | + |
| 71 | +### 2) Make the business logic consistent |
| 72 | + |
| 73 | +Suppose you have to repeatedly write the same formula in every query. Or you have a query that has complex business logic. To make this logic consistent across queries, you can use a view to store the calculation and hide the complexity. |
| 74 | + |
| 75 | +### 3) Add extra security layers |
| 76 | + |
| 77 | +A table may expose a lot of data including sensitive data such as personal and banking information. |
| 78 | + |
| 79 | +By using views and privileges, you can limit which data users can access by exposing only the necessary data to them. |
| 80 | + |
| 81 | +For example, the table `employees` may contain SSN and address information, which should be accessible by the HR department only. |
| 82 | + |
| 83 | +To expose general information such as first name, last name, and gender to the General Administration (GA) department, you can create a view based on these columns and grant the users of the GA department the view, not the entire table `employees` . |
| 84 | + |
| 85 | +### 4) Enable backward compatibility |
| 86 | + |
| 87 | +In legacy systems, views can enable backward compatibility. |
| 88 | + |
| 89 | +Suppose, you want to normalize a big table into many smaller ones. And you don’t want to impact the current applications that reference the table. |
| 90 | + |
| 91 | +In this case, you can create a view whose name is the same as the table based on the new tables so that all applications can reference the view as if it were a table. |
| 92 | + |
| 93 | +Note that a view and table cannot have the same name so you need to [drop the table](https://www.mysqltutorial.org/mysql-drop-table) first before creating a view whose name is the same as the deleted table. |
| 94 | + |
| 95 | +## Managing views in MySQL |
| 96 | + |
| 97 | +- [Create views](https://www.mysqltutorial.org/mysql-views/mysql-create-view/) – show you how to use the `CREATE VIEW` statement to create a new view in the database. |
| 98 | +- [Understand view processing algorithms](https://www.mysqltutorial.org/mysql-views/mysql-view-processing-algorithms/) – learn how MySQL processes a view. |
| 99 | +- [Create updatable views](https://www.mysqltutorial.org/mysql-views/mysql-updatable-views/) – learn how to create updatable views. |
| 100 | +- Create views with a `WITH CHECK OPTION` – ensure the consistency of views using the `WITH CHECK OPTION` clause. |
| 101 | +- `LOCAL & CASCADED` and `WITH CHECK OPTION` – specify the scope of the check with `LOCAL` and `CASCADED` options. |
| 102 | +- [Show views](https://www.mysqltutorial.org/mysql-views/mysql-show-view/) – provide ways to find views in a database. |
| 103 | +- [Show create view](https://www.mysqltutorial.org/mysql-views/mysql-show-create-view/) – learn how to display the statement that creates a view. |
| 104 | +- [Rename views](https://www.mysqltutorial.org/mysql-views/mysql-rename-view/) – change the name of a view to another. |
| 105 | +- [Drop views](https://www.mysqltutorial.org/mysql-views/mysql-drop-view/) – guide you on how to remove one or more existing views. |
0 commit comments