MySql 視圖

視圖的創(chuàng)建

  • 視圖用create view語句來創(chuàng)建
  • 使用show create view viewname來查看創(chuàng)建視圖的語句
  • drop view viewname刪除視圖
  • 更新視圖是,可以先用drop再用create,也可以直接用create or replace view

利用視圖簡(jiǎn)化復(fù)雜的聯(lián)結(jié)

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';

使用視圖重新格式化檢索出來的數(shù)據(jù)

\1.PNG
\1.PNG
\2.PNG
\2.PNG

使用視圖過濾不想要的數(shù)據(jù)

create view customeremaillist as
select cust_id, cust_name, cust_email
from cutomers
where cust_email is not null;

使用視圖與計(jì)算字段

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;


參考書籍:

  • MySQL必知必會(huì)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 什么是視圖 視圖(View)是一種虛擬存在的表,對(duì)于使用視圖的用戶來說基本上是透明的。視圖并不在數(shù)據(jù)庫(kù)中實(shí)際存在,...
    微日月閱讀 241評(píng)論 0 0
  • 視圖 視圖是一個(gè)虛擬表,其內(nèi)容由查詢定義。同真實(shí)的表一樣,視圖包含一系列帶有名稱的列和行數(shù)據(jù)。但是,視圖并不在數(shù)據(jù)...
    StrongZhao閱讀 803評(píng)論 0 1
  • MYSQL索引類型 按邏輯來分: 1.主鍵索引是一種特殊的唯一索引,不允許有空值 創(chuàng)建、刪除語句:alter ta...
    檸檬烏冬面閱讀 2,843評(píng)論 0 1
  • 最近項(xiàng)目上有使用到數(shù)據(jù)庫(kù)視圖,覺得需要把相關(guān)的知識(shí)整理一下方便學(xué)習(xí)。今天先簡(jiǎn)單介紹一下視圖的概念和一些基本原理。 ...
    孫進(jìn)不后退閱讀 11,718評(píng)論 1 13
  • 1.什么是視圖由查詢結(jié)果形成的一張?zhí)摂M表2.什么時(shí)候用到視圖某個(gè)結(jié)果出現(xiàn)的非常頻繁,(經(jīng)常要用這個(gè)結(jié)果來做子查詢)...
    日風(fēng)和閱讀 499評(píng)論 0 1