视图定义

  1. CREATE VIEW view_name[(column_list)]
  2. AS
  3. SELECT statement
  4. [WITH [CASCADED|LOCAL] CHECK OPTION]

示例:

  1. create or replace view mt.cust_view
  2. as
  3. select * from mt.cust
  4. where cust_sex='m'
  5. with check option;

修改

ALTER

删除

  1. DROP VIEW [IF EXISTS] view_name[,view_name]...[RESTRIC|CASCADE]

查看视图定义

  1. SHOW CREATE VIEW mysql_test.cust_view;

更新视图数据

1、使用 insert 通过视图向基本表插入数据

  1. INSERT INTO mysql_test.cust_view
  2. VALUES(909,'张三','m','北京',null);

2、使用 update 语句通过视图修改基本表数据

  1. update mysql_test.cust_view
  2. set cust_address='上海市';

3、使用 delete 语句通过视图删除基本表数据

  1. delete from mysql_test.cust_view
  2. where cust_name='李四';

视图的优点

  1. 集中分散的数据
  2. 简化查询语句
  3. 重用 SQL 语句
  4. 保护数据安全
  5. 共享数据
  6. 更改数据的格式