視圖

視圖(view):是一種有結(jié)構(gòu),但是沒結(jié)果的虛擬表

創(chuàng)建視圖

基本語法:create view 視圖名字 as select 語句;

創(chuàng)建單表視圖:基表只有一個

創(chuàng)建多表視圖:基表來源至少兩個

視圖:單表和多表

create view my_v1 as

select * from my_student;

create view my_v2 as

select * from my_class;

create view my_v3 as

select * from my_student as a left join my_class as

c on s.c_id=c.id;-- 錯誤,id重復(fù)

多表

create view my_v3 as

select s.*,c.c_name,c.room from my_student as s

join my_class as c on s.c_id=c.id;

查看視圖

show tables [like] / desc 視圖名 / show create table 視圖名;

-- c查看視圖創(chuàng)建語句

show create view my_v3\G(\G橫向查看)

修改視圖

alter view 視圖名字 as 新的select語句;

alter view my_v1 as

select id,name,age,sex,height,c_id from my_student;

刪除視圖

drop view 視圖名字;

drop view my_v4;

視圖數(shù)據(jù)操作

新增數(shù)據(jù)

多表視圖不能新增數(shù)據(jù)

可以向單表視圖插入數(shù)據(jù)

insert into my_v2 values(3,'Python1910','A204');

視圖中包含的字段必須有基表中所有不能為空、或沒有默認(rèn)值的字段

insert into my_v1 values(null,'陳立農(nóng)',130,'男',183,3);

視圖是可以向基表插入數(shù)據(jù)的

刪除數(shù)據(jù)

多表視圖不能刪除數(shù)據(jù)

delete from my_v3 where id=1;

單表視圖可以刪除數(shù)據(jù)

delete from my_v2 where id=4;

更新數(shù)據(jù)

更新限制:with check option;

多表視圖更新數(shù)據(jù)

update my_v3 set c_id=4 where id=6;

視圖:age字段限制更新

create view my_v4 as

select * from my_student where age>30 with check option;

-- 表示視圖的數(shù)據(jù)來源都是年齡大于30的,是由where age>30決定的

-- with chhheck

-- option決定通過視力更新的時候,不能將已經(jīng)得到的數(shù)據(jù)age>30的改成<30的

-- 將視圖可以查到的數(shù)據(jù)改成年齡小于30

update my_v4 set age=29 where id=5;

-- 可以修改數(shù)據(jù)讓視圖可以查到:可以改,但是無效果

update my_v4 set age=32 where id=3;?

視圖算法

視圖算法:系統(tǒng)對視圖以及外部查詢視圖的select語句的一種解析方式

視圖算法分三種

undefined:未定義(默認(rèn)的)

temptable:臨時表算法

merge:合并算法

算法指定:在創(chuàng)建視圖的時候create algorithm=指定算法 view 視圖名字 as select語句;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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