视图

视图的优点:集中分散的数据,简化查询语句,重用SQL语句,保护数据安全,共享所需数据,更改数据格式      //视图: 数据库对象

mysql-> create or replace view mysql_test.cust_view

    ->  as 

    ->  select * from mysql_test.cust

    ->  where cust_sex='M'

    ->  with check option;     //保证今后对该视图的修改都必须符合客户性别为男性这个条件

mysql-> drop view if exists mysql_test.cust_view

mysql-> alter view mysql_test.cust_view  //与创建视图只是相差关键字alter

    ->  as 

    ->  select * from mysql_test.cust

    ->  where cust_sex='M'

    ->  with check option;  

mysql-> show create view mysql_test.cust_view;

mysql-> insert into mysql_test.cust_view      //使用insert语句通过视图向基本表插入数据 原表会变

    ->  values(909,'周明','M','武汉市','洪山区'); 

mysql-> update mysql_test.cust_view

    ->  set cust_address='上海市';

mysql-> delete from mysql_test.cust_view  //牵扯到多表的话,不能用delete语句了

    ->  where cust_name='周明';

mysql-> select cust_name,cust_address

    ->  from mysql_test.cust_view

    ->  where cust_id  =905;

原文地址:https://www.cnblogs.com/lsxsx/p/13388263.html