view
Meaning: Understand as a virtual table and use it like a regular table
A new feature of MySQL version 5.1 is data generated dynamically from tables
For example: a comparison between a dance class and an ordinary class
Differences between views and tables:
Name | Create Grammar Keyword | Use | Occupy physical space |
---|---|---|---|
view | create view | Additions, deletions and alterations, but generally no additions, deletions or alterations can be made | Does not occupy, just saves sql logic |
surface | create table | crud | Occupy |
Benefits of views:
1. sql statement improves reuse and efficiency 2. Separation of tables to improve security
View Creation
Grammar: CREATE VIEW View Name AS Query statement;
View add-delete check
1,View the view's data ★ SELECT * FROM my_v4; SELECT * FROM my_v1 WHERE last_name='Partners'; 2,Insert data from view INSERT INTO my_v4(last_name,department_id) VALUES('False Bamboo',90); 3,Modify the view's data UPDATE my_v4 SET last_name ='Dream Girl' WHERE last_name='False Bamboo'; 4,Delete data from view DELETE FROM my_v4;
Some views cannot be updated
sql statements containing the following keywords: grouping function, distinct, group by, having, union, or union all Constant View Select contains subqueries join from a view that cannot be updated A subquery of the where clause references a table in the from clause
Update of view logic
#Mode 1: CREATE OR REPLACE VIEW test_v7 AS SELECT last_name FROM employees WHERE employee_id>100; //Mode 2: ALTER VIEW test_v7 AS SELECT employee_id FROM employees; SELECT * FROM test_v7;
Delete View
DROP VIEW test_v1,test_v2,test_v3;
View structure
DESC test_v7; SHOW CREATE VIEW test_v7;
1. Create Views
Grammar:
create view view name
as
Query statement;
#Case: Query the student name and professional name of Zhang SELECT stuname,majorname FROM stuinfo s INNER JOIN major m ON s.`majorid`= m.`id` WHERE s.`stuname` LIKE 'Zhang%'; CREATE VIEW v1 AS SELECT stuname,majorname FROM stuinfo s INNER JOIN major m ON s.`majorid`= m.`id`; SELECT * FROM v1 WHERE stuname LIKE 'Zhang%'; #1. Create Views /* Grammar: create view View Name as Query statement; */ USE myemployees; #1. Query employee name, department name and type information with a character in the name #1. Create CREATE VIEW myv1 AS SELECT last_name,department_name,job_title FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN jobs j ON j.job_id = e.job_id; #(2) Use SELECT * FROM myv1 WHERE last_name LIKE '%a%'; #2. Query the average wage level of each department #1. Create a view to see the average wage per department CREATE VIEW myv2 AS SELECT AVG(salary) ag,department_id FROM employees GROUP BY department_id; #(2) Use SELECT myv2.`ag`,g.grade_level FROM myv2 JOIN job_grades g ON myv2.`ag` BETWEEN g.`lowest_sal` AND g.`highest_sal`; #3. Query the Department with the lowest average wage SELECT * FROM myv2 ORDER BY ag LIMIT 1; #4. Query the name and salary of the Department with the lowest average wage CREATE VIEW myv3 AS SELECT * FROM myv2 ORDER BY ag LIMIT 1; SELECT d.*,m.ag FROM myv3 m JOIN departments d ON m.`department_id`=d.`department_id`;
2. Modification of Views
Mode 1:
Grammar:
create or replace view view name
as
Query statement;
Mode 2:
Grammar:
alter view view name
as
Query statement;
#2. Modification of Views #Mode 1: /* create or replace view View Name as Query statement; */ SELECT * FROM myv3 CREATE OR REPLACE VIEW myv3 AS SELECT AVG(salary),job_id FROM employees GROUP BY job_id; #Mode 2: /* Grammar: alter view View Name as Query statement; */ ALTER VIEW myv3 AS SELECT * FROM employees;
3. Delete View
Syntax: drop view view view name, view name,...;
#3. Delete View /* Syntax: drop view view view name, view name,...; */ DROP VIEW emp_v1,emp_v2,myv3;
4. View View
#4. View View DESC myv3; SHOW CREATE VIEW myv3;
5. Update of View
#5. Update of View CREATE OR REPLACE VIEW myv1 AS SELECT last_name,email,salary*12*(1+IFNULL(commission_pct,0)) "annual salary" FROM employees; CREATE OR REPLACE VIEW myv1 AS SELECT last_name,email FROM employees; SELECT * FROM myv1; SELECT * FROM employees; #1. Insert INSERT INTO myv1 VALUES('Zhang Fei','zf@qq.com'); #2. Modification UPDATE myv1 SET last_name = 'Zhang Wuji' WHERE last_name='Zhang Fei'; #3. Delete DELETE FROM myv1 WHERE last_name = 'Zhang Wuji'; #Views with the following characteristics do not allow updates #1. sql statements containing the following keywords: grouping function, distinct, group by, having, union, or union all CREATE OR REPLACE VIEW myv1 AS SELECT MAX(salary) m,department_id FROM employees GROUP BY department_id; SELECT * FROM myv1; #To update UPDATE myv1 SET m=9000 WHERE department_id=10; #(2) Constant View CREATE OR REPLACE VIEW myv2 AS SELECT 'john' NAME; SELECT * FROM myv2; #To update UPDATE myv2 SET NAME='lucy'; #3. Select contains subqueries CREATE OR REPLACE VIEW myv3 AS SELECT department_id,(SELECT MAX(salary) FROM employees) Maximum Wage FROM departments; #To update SELECT * FROM myv3; UPDATE myv3 SET Maximum Wage=100000; #④join CREATE OR REPLACE VIEW myv4 AS SELECT last_name,department_name FROM employees e JOIN departments d ON e.department_id = d.department_id; #To update SELECT * FROM myv4; UPDATE myv4 SET last_name = 'Zhang Fei' WHERE last_name='Whalen'; INSERT INTO myv4 VALUES('Chen Zhen','xxxx'); #from A view that cannot be updated CREATE OR REPLACE VIEW myv5 AS SELECT * FROM myv3; #To update SELECT * FROM myv5; UPDATE myv5 SET Maximum Wage=10000 WHERE department_id=60; #A subquery of the where clause references a table in the from clause CREATE OR REPLACE VIEW myv6 AS SELECT last_name,email,salary FROM employees WHERE employee_id IN( SELECT manager_id FROM employees WHERE manager_id IS NOT NULL ); #To update SELECT * FROM myv6; UPDATE myv6 SET salary=10000 WHERE last_name = 'k_ing';