数据蛙数据分析每周作业

【MYSQL】-1 使用视图

2019-02-10  本文已影响14人  silent_eyes_77

本周学习了<mysql 必知必会>,本文对于之前还未掌握的视图操作进行总结。

视图是什么?

视图是用于查看存储在别处的数据的一种设施,其本身不包含数据;

视图有什么作用?

1)重用sql语句
2) 简化复杂的sql操作
3)可以只使用表的组成部分,而非整个表
4)保护数据。可以给用户授予表特定部分的访问权限,而不是整个表的访问
5)更改数据格式和表示。视图可以返回与底层表的表示和格式不同的数据

视图使用规则

1)唯一命名
2)不限创建数目
3)创建视图要有权限
4)视图可以嵌套
5)order by 出现在视图语句和select 语句中,select 优先级更高
6)不能索引,不能有关联的触发器,无默认值

视图的几个应用

  1. 利用视图创建复杂的链接

    create view productcustomers as
    -> select cust_name,cust_contact,prod_id
    -> from customers,orders,orderitems
    -> where customers.cust_id=orders.cust_id
    -> and orderitems.order_num=orders.order_num;
    
     select cust_name,cust_contact from productcustomers
    -> where prod_id='TNT2';
    

    +----------------+--------------+
    | cust_name | cust_contact |
    +----------------+--------------+
    | Coyote Inc. | Y Lee |
    | Yosemite Place | Y Sam |
    +----------------+--------------+

对于常用操作,可以用视图格式化检索出的数据:
select 在单个组合计算列中返回供应商名和位置

1)不用视图:

select concat(RTrim(vend_name),'(',RTrim(vend_country),')')
    -> as vend_title
    -> from vendors
    -> order by vend_name;

+------------------------+
| vend_title |
+------------------------+
| ACME(USA) |
| Anvils R Us(USA) |
| Furball Inc.(USA) |
| Jet Set(England) |
| Jouets Et Ours(France) |
| LT Supplies(USA) |
+------------------------+
6 rows in set (0.00 sec)

2)用视图:现在,假如经常需要这个格式的结果,不必在每次需要的时候执行联结,创建一个视图,每次需要的时候使用它即可。为把此语句转换为视图,可以按如 下进行

create view vendorlocations 
    -> as select concat(RTrim(vend_name),'(',RTrim(vend_country),')')
    ->    as vend_title
    -> from vendors
    -> order by vend_name;

select * from vendorlocations;

+------------------------+
| vend_title |
+------------------------+
| ACME(USA) |
| Anvils R Us(USA) |
| Furball Inc.(USA) |
| Jet Set(England) |
| Jouets Et Ours(France) |
| LT Supplies(USA) |
+------------------------+

  1. 使用视图过滤不想要的数据:发送邮件时,过滤没有邮件地址的用户
 create view cutomeremaillist as 
    -> select cust_id,cust_name,cust_email
    -> from customers
    -> where cust_email is not null;
select * from cutomeremaillist;

+---------+----------------+---------------------+
| cust_id | cust_name | cust_email |
+---------+----------------+---------------------+
| 10001 | Coyote Inc. | ylee@coyote.com |
| 10003 | Wascals | rabbit@wascally.com |
| 10004 | Yosemite Place | sam@yosemite.com |
+---------+----------------+---------------------+

3 . 使用视图简化计算字段
1) 无视图操作:

select prod_id,quantity,item_price,quantity*item_price as expanded_price
   -> from orderitems
   -> where order_num=20005;

+---------+----------+------------+----------------+
| prod_id | quantity | item_price | expanded_price |
+---------+----------+------------+----------------+
| ANV01 | 10 | 5.99 | 59.90 |
| ANV02 | 3 | 9.99 | 29.97 |
| TNT2 | 5 | 10.00 | 50.00 |
| FB | 1 | 10.00 | 10.00 |
+---------+----------+------------+----------------+

create view orderitemsexpanded as 
    -> select order_num,prod_id,quantity,item_price,quantity*item_price 
    -> as expanded_price
    -> from orderitems;

select * from orderitemsexpanded where order_num=20005;

+-----------+---------+----------+------------+----------------+
| order_num | prod_id | quantity | item_price | expanded_price |
+-----------+---------+----------+------------+----------------+
| 20005 | ANV01 | 10 | 5.99 | 59.90 |
| 20005 | ANV02 | 3 | 9.99 | 29.97 |
| 20005 | TNT2 | 5 | 10.00 | 50.00 |
| 20005 | FB | 1 | 10.00 | 10.00 |
+-----------+---------+----------+------------+----------------+

上一篇下一篇

猜你喜欢

热点阅读