sqlmysql
10. 尚硅谷MySQL视图.pdf

视图

虚拟表,和普通表一样使用
MySQL5.1版本出现的新特性,是通过表动态生成的数据
比如舞蹈班和普通班级的对比
视图和表的区别

创建语法的关键字 是否实际占用物理空间 使用
视图 create view 只是保存了sql逻辑 一般不能增删改
create table 保存了数据 增删改查

视图的好处

  1. sql语句提高重用性,效率高
  2. 简化了复杂sql操作,不比知道它的查询细节
  3. 和表实现了分离,提高了安全性 ```sql

    案例:查询姓张的学生名和专业名

    SELECT stuname, majorname FROM stuinfo s
    1. INNER JOIN major m ON s.`majorid` = m.`id`
    WHERE s.stuname LIKE ‘张%’;

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 ‘张%’;

  1. <a name="gvrlf"></a>
  2. ### 视图的创建
  3. 语法<br />`**CREATE VIEW 视图名**`<br />`**AS**`<br />`**查询语句;**`
  4. ```sql
  5. USE myemployees;
  6. #1.查询姓名中包含a字符的员工名、部门名和工种信息
  7. #①创建
  8. CREATE VIEW myv1
  9. AS
  10. SELECT last_name, department_name, job_title
  11. FROM employees e
  12. JOIN departments d ON e.department_id = d.department_id
  13. JOIN jobs j ON j.job_id = e.job_id;
  14. #②使用
  15. SELECT *
  16. FROM myv1
  17. WHERE last_name LIKE '%a%';
  18. #2.查询各部门的平均工资级别
  19. #①创建视图查看每个部门的平均工资
  20. CREATE VIEW myv2
  21. AS
  22. SELECT AVG(salary) ag, department_id
  23. FROM employees
  24. GROUP BY department_id;
  25. #②使用
  26. SELECT myv2.`ag`, g.grade_level
  27. FROM myv2
  28. JOIN job_grades g ON myv2.`ag` BETWEEN g.`lowest_sal` AND g.`highest_sal`;
  29. #3.查询平均工资最低的部门信息
  30. SELECT *
  31. FROM myv2
  32. ORDER BY ag
  33. LIMIT 1;
  34. #4.查询平均工资最低的部门名和工资
  35. CREATE VIEW myv3
  36. AS
  37. SELECT *
  38. FROM myv2
  39. ORDER BY ag
  40. LIMIT 1;
  41. SELECT d.*, m.ag
  42. FROM myv3 m
  43. JOIN departments d ON m.`department_id` = d.`department_id`;

视图的修改

方式一
**create or replace view 视图名**
**as**
**查询语句;**

  1. SELECT * FROM myv3
  2. CREATE OR REPLACE VIEW myv3
  3. AS
  4. SELECT AVG(salary), job_id
  5. FROM employees
  6. GROUP BY job_id;

方式二
**alter view 视图名**
**as**
**查询语句;**

  1. ALTER VIEW myv3
  2. AS
  3. SELECT *
  4. FROM employees;

删除视图

**drop view 视图名, 视图名, ...;**

  1. DROP VIEW emp_v1,emp_v2,myv3;

查看视图

**DESC 视图名;**
**SHOW CREATE VIEW 视图名;**

  1. DESC myv3;
  2. SHOW CREATE VIEW myv3;

视图的更新

对视图的更新,原始表同样会更新

  1. CREATE OR REPLACE VIEW myv1
  2. AS
  3. SELECT last_name, email, salary * 12 * (1 + IFNULL(commission_pct, 0)) "annual salary"
  4. FROM employees;
  5. CREATE OR REPLACE VIEW myv1
  6. AS
  7. SELECT last_name, email
  8. FROM employees;
  9. SELECT * FROM myv1;
  10. SELECT * FROM employees;
  11. #1.插入
  12. INSERT INTO myv1
  13. VALUES ('张飞', 'zf@qq.com');
  14. #2.修改
  15. UPDATE myv1
  16. SET last_name = '张无忌'
  17. WHERE last_name = '张飞';
  18. #3.删除
  19. DELETE
  20. FROM myv1
  21. WHERE last_name = '张无忌';

某些视图不能更新

包含以下关键字的sql语句
分组函数 group by having

union union all
常量视图 from一个不能更新的视图
Select中包含子查询

where子句的子查询引用了from子句中的表

  1. #具备以下特点的视图不允许更新
  2. #①包含以下关键字的sql语句:分组函数、distinct、group by、having、union或者union all
  3. CREATE OR REPLACE VIEW myv1
  4. AS
  5. SELECT MAX(salary) m, department_id
  6. FROM employees
  7. GROUP BY department_id;
  8. SELECT * FROM myv1;
  9. #更新❌
  10. UPDATE myv1
  11. SET m=9000
  12. WHERE department_id = 10;
  13. #②常量视图
  14. CREATE OR REPLACE VIEW myv2
  15. AS
  16. SELECT 'john' NAME;
  17. SELECT * FROM myv2;
  18. #更新❌
  19. UPDATE myv2
  20. SET NAME='lucy';
  21. #③Select中包含子查询
  22. CREATE OR REPLACE VIEW myv3
  23. AS
  24. SELECT department_id, (SELECT MAX(salary) FROM employees) 最高工资
  25. FROM departments;
  26. #更新❌
  27. SELECT *
  28. FROM myv3;
  29. UPDATE myv3
  30. SET 最高工资=100000;
  31. #④join
  32. CREATE OR REPLACE VIEW myv4
  33. AS
  34. SELECT last_name, department_name
  35. FROM employees e
  36. JOIN departments d
  37. ON e.department_id = d.department_id;
  38. #更新❌
  39. UPDATE myv4
  40. SET last_name = '张飞'
  41. WHERE last_name = 'Whalen';
  42. INSERT INTO myv4
  43. VALUES ('陈真', 'xxxx');
  44. #⑤from一个不能更新的视图
  45. CREATE OR REPLACE VIEW myv5
  46. AS
  47. SELECT * FROM myv3;
  48. #更新
  49. UPDATE myv5
  50. SET 最高工资=10000
  51. WHERE department_id = 60;
  52. #⑥where子句的子查询引用了from子句中的表
  53. CREATE OR REPLACE VIEW myv6
  54. AS
  55. SELECT last_name, email, salary
  56. FROM employees
  57. WHERE employee_id IN (
  58. SELECT manager_id
  59. FROM employees
  60. WHERE manager_id IS NOT NULL
  61. );
  62. #更新❌
  63. UPDATE myv6
  64. SET salary=10000
  65. WHERE last_name = 'k_ing';