视图就是一个虚拟的表,将特定的数据查询结果保存为视图

创建一个视图:

  1. create view emp_salary_view as (select * from employees where salary>2000);

此时就出现一个视图:
image.png

  1. select * from emp_salary_view esv where job='销售';

image.png

覆盖视图:

  1. create or replace view emp_salary_view as (select * from employees where job='售后');

删除视图

  1. drop view emp_salary_view;

Algorithm参数

  1. merge 可以更新真实表中的数据
  2. temptable 不可以更新表中的数据
  3. undefined 没有定义该参数,但是默认为merge,因为MySql认为这样更有效

    检查

    with check option
    1. create view emp_salary_view as (select * from employees where salary>2000) with check option;
    这样再修改视图中的数据后,会对修改的内容做检测,如果不符合where条件就不允许数据修改

    视图不可更新部分

    如果视图中的数据不是直接来源于基表,那么这个字段的数据是无法进行操作的
    列如:avg(salary) 、sum(salary)等