MySql view

This article is reproduced content, the original address: https://www.cnblogs.com/zyshi/p/6617331.html
What is a view
In layman's terms, a view is the result set returned after a SELECT statement is executed. So when we create a view, the main work falls on creating this SQL query statement.

Features of Views A
view is a reference to several basic tables, a virtual table, the result of query statement execution, and does not store specific data (if the basic table data changes, the view will also change);

You can perform addition, deletion, modification, and query operations like the basic table (ps: addition, deletion, and modification operations are conditional);

The role of the view
facilitates operations, especially query operations, reduces complex SQL statements, and enhances readability;

More secure, database authorization commands cannot be limited to specific rows and columns, but by creating views reasonably, permissions can be limited to row and column levels;

Usage Scenarios
When permissions are controlled, users are not expected to access some columns containing sensitive information in the table, such as salary…

Key information comes from multiple complex association tables, and views can be created to extract the information we need and simplify operations;

View Example 1 - Create View and Query Data Operation
There are three tables: user (user), course (course), and user course intermediate table (user_course). The table structure and data are as follows:
write picture description here
when we want to query all courses on Xiao Zhang For related information, you need to write a long SQL statement like this:
SELECT
uc. idAS id,
u. nameAS username,
c. nameAS coursename
FROM
user u
LEFT JOIN user_course ucON (( u. id= uc. userid))
LEFT JOIN course cON (( uc. courseid= c. id))
WHERE
u. name= 'Xiao Zhang'
but we can simplify the operation by the view, for example we create the view view_user_course as follows:
DROP VIEW
IF EXISTS view_user_course;

CREATE ALGORITHM = UNDEFINED
DEFINER = root@ localhost
SQL SECURITY DEFINER
VIEW view_user_courseAS (
SELECT
uc. idAS id,
u. nameAS username,
c. nameAS coursename
FROM
(
(
user u
LEFT JOIN user_course ucON (( u. id= uc. userid))
)
LEFT JOIN course cON (( uc. courseid= c. id))
)
);
* *ALGORITHM=UNDEFINED: Specify the processing algorithm of the view;
DEFINER= root@ localhost: Specify the creator of the view;
SQL SECURITY DEFINER: Specify the security verification method when the view queries data; **
After the view is created, we can directly use the following SQL statement in the view You can also get the required results by querying all the course-related information on Xiaozhang:
SELECT
vuc.username,
vuc.coursename
FROM
view_user_course vuc
WHERE
vuc.username = ‘小张’

View Example 2 - Add, delete, and modify data operations
Continue, we can try to add, delete, and modify data operations on the view view_user_course, as follows:

update view_user_course set username=’test’,coursename=’JAVASCRIPT’ where id=3

Because it is not possible to modify two tables at the same time on a view that is connected by multiple associated tables;

So what operations can be performed on the view?

The view and the table are in a one-to-one relationship: if there are no other constraints (such as fields not in the view, which are required fields in the basic table), data operations can be added, deleted, and modified;

For example, we create the user key information view view_user_keyinfo, as follows
DROP VIEW
IF EXISTS view_user_keyinfo;

CREATE ALGORITHM = UNDEFINED DEFINER = root@ localhostSQL SECURITY DEFINER VIEW view_user_keyinfoAS SELECT
u. idAS id,
u. accountAS account,
u. nameAS username
FROM
user u;
The addition, deletion and modification operations are as follows, and the operation is successful (note that other fields in the user table must be allowed to be empty, otherwise the operation will fail):

INSERT INTO view_user_keyinfo (account, username)
VALUES
('test1', 'test1');
DELETE
FROM
view_user_keyinfo
WHERE
username = 'test1';
UPDATE view_user_keyinfo
SET username = 'updateuser'
WHERE
id = 1
The view and the table are in a one-to-many relationship : If you only modify the data of one table, and there are no other constraints (such as fields not in the view, which are required fields in the basic table), you can perform the data modification operation, such as the following statement, the operation is successful;

update view_user_course set coursename=’JAVA’ where id=1;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325557979&siteId=291194637