Mysql study notes advanced

review:

Mysql study notes finishing

view

☞ Interpretation: A virtual table, the row and column data is derived from the table used in the query of the defined view, and it is dynamically generated when the view is used, only the sql logic is stored without the data result (ps: no data is stored The result means that the current SQL statement query result will show what the view is. Once the data in the real table changes, the result in the view will also change).

☞ Popular explanation: temporary and virtual, some query sql results can be saved as views for reuse. To give an example in life, each class selects a part of the students to form a temporary class. This temporary class can do certain things. When the temporary class is not needed to do certain things, this class can They are disbanded and recombined when they are needed. The analog view is this function. The things in the view itself are virtual, but they can be temporarily implemented when they are needed.

☞ Scene

    ● The same query results are used in multiple places

    ● The SQL statement used for the required query results is more complex

☞ Example

    ★ 普通的查询: SELECT s.studentname, m.majorname FROM student s INNER JOIN major m ON s.majorid = m.majorid WHERE s.studentname LIKE '张%';

    ● 存为视图: CREATE VIEW v1 AS SELECT s.studentname, m.majorname FROM student s INNER JOIN major m ON s.majorid = m.majorid;

    ○ Use view query: SELECT studentname, majorname FROM v1 WHERE studentname LIKE'张%';  //but alias cannot be used

▶ Create view

    ● Grammar

        CREATE VIEW <view name> AS <query statement>;  //Query statement is generally for more complex SQL

    ● Use

        CREATE VIEW myemp1 AS SELECT e.last_name, d.department_name, j.job_id FROM employees e INNER JOIN departments d ON e.department_id = d.department_id JOIN jobs j ON j.job_id = e.job_id;

        SELECT * FROM myemp1 WHERE last_name LIKE'%a%';  //It is equivalent to giving a name to the query statement

    ● Advantages

        ○ Realize the reuse of sql statements

        ○ Simplify the operation of complex SQL statements and simplify the details

        ○ Protect data and improve security (provide specific data information to the requester without providing the entire original table data, which protects the privacy of users)

▶ Modify view

    ● Method 1: CREATE OR REPLACE VIEW <name of existing view> AS <query statement>;

    ● Method 2: ALTER VIEW <view name> AS <query statement>;

▶ Delete view / view structure of view

    ● DROP VIEW <The name of the view to be deleted, which can be separated by commas>; //Delete the view

    ● DESC <view name>;  //Check the structure of the view

▶ Update of the view (the data in the real table will also be modified synchronously after the update)

    ● Theoretically, the view is allowed to do update operations like tables (including inserting data, modifying data and deleting data), but in the following situations, update operations are not allowed, and these scenarios basically cover more than 95% sql statement, so it can be considered that the view is not allowed to do update operations

        ○ Update operations are not allowed when the following keywords are included ( grouping function, DISTINCT, GROUP BY, HAVING, UNION or UNION ALL )

        ○ Constant view is not allowed to update

        ○ Update is not allowed when the SELECT contains subqueries

        ○ Connection queries (including sql92 and sql99 syntax) are not allowed to update (can be modified but cannot be inserted)

        ○ After FROM is a view that cannot be updated (meaning that when adding a view, if the following FROM is a view that cannot be updated, then when a new view is added, the new view cannot be updated)

        ○ The subquery of the WHERE clause refers to the table in the FROM clause, as shown in the following colored table:

            CREATE OR REPLACE VIEW myv10 AS
            SELECT last_name, salary FROM employees WHERE employee_id IN (
                SELECT manager_id FROM employees WHERE manager_id IS NOT NULL
            );

▶ The difference between view and table

  Create grammar Whether to occupy physical space Is CRUD allowed
view create view Occupies a small part of the physical space to store SQL logic Views are mainly used for checking, and generally do not allow additions, deletions and modifications
table create table Occupy physical space to store data Allow addition, deletion and modification

to be continued. . .

 

Guess you like

Origin blog.csdn.net/ip_JL/article/details/87619044