MySQL Database - What are MySQL Views?

A MySQL view (View) is a virtual table. Like a real table, a view is also composed of columns and rows, but the view does not actually exist in the database. The row and column data comes from the tables used in the query that defines the view, and is also dynamically generated when the view is used.

The database only stores the definition of the view, and does not store the data in the view. These data are all stored in the real table referenced by the definition view query. When using a view to query data, the database will retrieve the corresponding data from the real table. Therefore, the data in the view is dependent on the data in the real table. Once the data in the real table changes, the data displayed in the view will also change.

The view can select information that is useful to the user from the original table, and information that is useless to the user or that the user does not have permission to understand can be directly blocked, which is similar to filtering. Doing so not only simplifies the application, but also ensures the security of the system.

For example, the following database has a table department for company departments. The table includes department number (d_id), department name (d_name), function (function) and office address (address). The structure of the department table is as follows:

mysql> DESC department;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| d_id     | int(4)      | NO   | PRI | NULL    |       |
| d_name   | varchar(20) | NO   | UNI    | NULL    |       |
| function | varchar(50) | YES  |     | NULL    |       |
| address  | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

There is also an employee table worker. The table contains the employee's job number (num), department number (d_id), name (name), gender (sex), date of birth (birthday) and home address (homeaddress). The structure of the worker table is as follows:

mysql> DESC worker;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| num         | int(10)     | NO   | PRI | NULL    |       |
| d_id        | int(4)      | YES  | MUL | NULL    |       |
| name        | varchar(20) | NO   |     | NULL    |       |
| sex         | varchar(4)  | NO   |     | NULL    |       |
| birthday    | datetime    | YES  |     | NULL    |       |
| homeaddress | varchar(50) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

Since the scope of power of the leaders of each department is different, the leaders of each department can only see the employee information of the department; moreover, the leaders may not care about the employees' birthdays and home addresses. In order to achieve this goal, a view can be established for the leaders of each department. Through this view, the leaders can only see the specified information of the employees in this department.

For example, create a view called product_view for the production department. Through the view product_ view, the leader of the production department can only see the job number, name and gender of the employees in the production department. The information of the department tables and the information of the worker tables still exist in their respective tables, and no data information is saved in the view product_view. When the information in the department table and worker table changes, the information displayed in the view product_view will also change accordingly.

Tip: If you often need to query the data of specified fields from multiple tables, you can create a view on these tables, and display the data of these fields through this view.

MySQL's view does not support the function of input parameters, so there is still a lack of interactivity. But for operations that do not change much, using views can greatly simplify user operations.

Views are not the same as data tables in that they differ in the following ways:

  • The view is not a real table in the database, but a virtual table whose structure and data are based on the query of the real table in the data.
  • The query operation SQL statement stored in the database defines the content of the view. The column data and row data come from the actual table referenced by the view query, and these data are dynamically generated when the view is referenced.
  • Views have no actual physical records and are not stored in the database in the form of datasets. The corresponding data is actually stored in the real table referenced by the view.
  • Views are windows to data, and tables are content. The table is the storage unit of the actual data, and the view only displays the data in different ways, and its data source is still the actual table.
  • A view is a way to view a data table. It can query the data composed of certain fields in the data table. It is just a collection of some SQL statements. From a security point of view, the data security of the view is higher, and the user who uses the view does not touch the data table and does not know the table structure.
  • The establishment and deletion of a view only affects the view itself, not the corresponding basic table.

Advantages of Views

Although views and tables are different in essence, after a view is defined, its structure is the same as that of a table, and operations such as query, modification, update, and deletion can be performed. At the same time, views have the following advantages:

1) Customize user data and focus on specific data

In the actual application process, different users may have different requirements for different data.

For example, when databases exist at the same time, such as student basic information tables, curriculum tables, and teacher information tables, etc., different users can use their own data according to their needs. Students view the view to modify their basic information, arrange course personnel to view the view to modify the curriculum and teacher information, and teachers view the view of student information and course information table.

2) Simplify data manipulation

When using queries, aggregation functions are often used, and information about other fields must be displayed at the same time. It may also need to be associated with other tables. The statement may be very long. If this action occurs frequently, you can create a view to simplify the operation.

3) Improve data security

Views are virtual and do not physically exist. To protect the security of the underlying data, you can only grant user view permissions without specifying the permissions to use tables.

4) Share required data

By using views, each user does not have to define and store the data they need, but can share the data in the database, and the same data only needs to be stored once.

5) Change the data format

By using views, retrieved data can be reformatted and organized for output to other applications.

6) Reuse SQL statements

The view provides the encapsulation of the query operation and does not contain data itself. The presented data is retrieved from the base table according to the view definition. If the data in the base table is added or deleted, the view also presents the updated data. Once a view is defined, it can be easily reused after writing the required queries.

Pay attention to distinguishing the essence of view and data table, that is, view is a virtual table based on real table, and its data sources are all based on real table.

When using views, you should also pay attention to the following points:

  • Sufficient access rights are required to create views.
  • There is no limit to the number of views that can be created.
  • Views can be nested, that is, queries that retrieve data from other views are created to create views.
  • Views cannot be indexed, and cannot have associated triggers, defaults, or rules.
  • Views can be used with tables.
  • Views do not contain data, so any retrieval required in the query must be performed each time the view is used. If you create complex views or nested views with multiple joins and filter conditions, you may find that the performance of the system is severely degraded. Therefore, when deploying a large number of view applications, system testing should be performed.

Tip: The ORDER BY clause can be used in a view, but if the view retrieves data in the SELECT statement also contains the ORDER BY clause, the ORDER BY clause in the view will be overwritten.

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130461645