MySQL 基礎(chǔ)---視圖

視圖: 本質(zhì)上是一種虛擬表,其內(nèi)容與真實(shí)表相似,包含一系列帶有名稱的列和行數(shù)據(jù)。

視圖的特點(diǎn)如下:

  • 視圖的列可以來自不同的表,是表的抽象和在邏輯意義上建立的新關(guān)系。
  • 視圖是由基本表(實(shí)表)產(chǎn)生的表(虛表)。
  • 視圖的建立和刪除不影響基本表。
  • 對(duì)視圖內(nèi)容的更新(添加、刪除和修改)直接影響基本表。
  1. 創(chuàng)建視圖
# 創(chuàng)建數(shù)據(jù)庫
 create database view;
# 使用數(shù)據(jù)庫
 use view;
# 創(chuàng)建表
 create table t_product(
          id int primary key,
          name varchar(20),
          price float
 );
# 查看當(dāng)前表中的數(shù)據(jù)
 select * from t_product;
# 創(chuàng)建視圖
create view view_selectproduct
   as
         select id,name from t_product;
# 使用視圖--實(shí)質(zhì)是將語句進(jìn)行了封裝
 select * from view_selectproduct;
  1. 創(chuàng)建各種視圖
    (1)封裝實(shí)現(xiàn)查詢常量語句的視圖, 即所謂常量視圖。
    create view view_test1
        as
            select 3.1415926;

(2)封裝使用聚合函數(shù)(SUM、MIN、MAX、COUNT等)查詢語句

      create view view_test2
        as
            select count(name) from t_student;

(3) 封裝實(shí)現(xiàn)排序功能(ORDER BY)查詢語句的視圖

      create view view_test3
        as
            select name from t_product order by id desc;

(4) 封裝實(shí)現(xiàn)表內(nèi)連接查詢語句的視圖

    create view view_test4
        as
            select s.name 
                        from t_student as s, t_group as g 
            where s.group_id=g.id 
            and g.id=2;

(5)封裝實(shí)現(xiàn)表外連接(LEFT JOIN 和 RIGHT JOIN)查詢語句的視圖。

    create view view_test5
        as
            select s.name 
                      from t_student as s 
            left join 
                      t_group as g 
            on s.group_id=g.id 
            where g.id=2;

(6)封裝實(shí)現(xiàn)子查詢相關(guān)查詢語句的視圖。

    create view view_test6
        as
            select s.name
                       from t_student as s
            where s.group_id in (
                  select id from t_group
            );

(7)封裝了實(shí)現(xiàn)記錄聯(lián)合(UNION和UNION ALL)查詢語句視圖

      create view view_test7
        as
            select id, name from t_student
            union all
            select id, name from t_group;
  1. 查看視圖
    (1)SHOW TABLES語句查看視圖名show tables;
    (2)SHOW TABLE STATUS語句查看視圖詳細(xì)信息show table status \G;show table status from view \G;show table status from view like "view_selectproduct" \G;
    (3)SHOW CREATE VIEW語句查看視圖定義信息show create view view_selectproduct \G;
    (4)DESCRIBE|DESC語句查看視圖設(shè)計(jì)信息desc view_selectproduct;

  2. 刪除視圖

drop view view_selectproduct,view_select_selectproduct1;
  1. CREATE OR REPLACE VIEW語句修改視圖
# 修改視圖語句
create or replace view view_selectproduct
         as
                 select name from t_product;
# 查詢視圖
select * from view_selectproduct;
  1. ALERT 語句修改視圖
 alter view view_selectproduct
          as
                  select id,name from t_product;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容