聯(lián)合查詢,子查詢 視圖,視圖數(shù)據(jù)操作

聯(lián)合查詢

基本語句包括select 語句1? ?,? union【union選項】? ?,? ?select語句2。。。。

union選項? ?: all保留所有不管重復(fù)? ? ? ? ? distinct? ?去重? 默認(rèn)的

1 . select * from my_class?

union? ? --- 默認(rèn)去重

select * from my_class

2.?select * from my_class?

union? ?all? ?--- 不去重

select * from my_class

3. select id , c_name,room from my_class

union all ---不去重

select name,number,id from my_student

select * from my_student order by sex asc,height desc

--需求:男生升序,女生降序(年齡)? ?(select* from my_student where sex=' 男 ' order by age asc? ? limit? 999999)? ? ? ? ? ? union? ? ? ? ? ?(select * from my_student where sex=' 女 ' order by age desc limit? 999999)

子查詢(sub query)

1 按位置分類? ? ? ?

from 子查詢,

where子查詢,

?exists子查詢 。 select * from my_student where exists(select * from my_class)? ---是否成立

select * from my_student where exists(select * from my_class where id = 3)

select * from my_student where exist (select * from my_class where id = 2)

2 按結(jié)果分類? ? ??

[1]標(biāo)量 子查詢(一行一列),

select * from my_class? ? ? ? ? ? ? select * from my_student

select * from my_student where c_id=?? ? ?select id from my_class where c_name = 'Python1910'? ? ---id一定只有一個值(一行一列)

[2]列? 子查詢(一列多行),

select * from my_class? ? ? ? ? ? ? ? select * from my_student where c_id in(select id from my_class)

{??

1 。 =any等價于in? ?--其中一個就可以? ? ? ? ? ? ? ? ? ? select * from my_student where c_id =? any(select id from my_class)? ? ? ? ? ---所有結(jié)果除了NULL除外

2。 any等價于some? ?---二者是一樣的select * from my_student where c_id =? some(select id from my_class)? ? ? ? ? ? ??---所有結(jié)果除了NULL除外

3 。 == all為全部? ?? select * from my_student where c_id =? all(select id from my_class)? ???---NULL除外

}

---查詢年齡最大且身高最高? ? ? ?select * from my_student where age = (select max(age) from my_student) and height = (select max(age) from my_student)

[3]行 子查詢(多列一行或多行多列),select * from my_student where? ? ? ? ? ?--(age ,height)稱為行元素? ? ? ?(age,height) = (select? max (age),max(height) from my_student)

select * from my_student order by age desc,height desclimit 1 --可能查詢結(jié)果不是預(yù)想的

[4]表 子查詢(多行多列)

---插入學(xué)生? ? insert into? my_student value(null,'bc200007','小紅','23',''186)

--查找每個班身高最高的學(xué)生? ?select * from (select * from my_student order by height desc limit 999999 ) as student group? by c_id? ? ? ?-----每個班選出一個學(xué)生

視圖(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 s left join my_class as c on s.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?

外鏈接? 不符合條件不放入

內(nèi)鏈接? 符合條件放入

--查看視圖創(chuàng)建語句? ? show create view my_v3\G

查看視圖

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

show create view my_v3\G? --查看視圖創(chuàng)建語句

show views? 不可以v查看所有視圖

show * from my_v1

show * from my_v2

show * from my_v3

修改視圖

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

alter view my_ v1 as? ? ? ? ? ? ? ? ? select id,name,age,sex,height,c_id from my_student

刪除視圖

drop view? 視圖名字

create view my_v4 as? ? ? ? ? ? ? ? select * from my_student? ? ? ? ? ? ? ? ---刪除視圖v4? ? ? ? ? ? ? ? ? drop table my_v4? ?--刪除失?。ú荒軇h表)? ?drop? view my_v4? - -- 刪除成功

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

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

---多表視圖不能插入數(shù)據(jù)? ?insert into my_v3 value(null,'bc20200008','張山','男',15,180,3,'Python1910','A204')? ? ? -----插入失敗

--將學(xué)生表的學(xué)號字段設(shè)置成不允許為空? ?alter tabel my_student modify number char(10) not null? unique? ??

{ 2 }? 可以向單表視圖插入數(shù)據(jù),但是視圖中包含的字段必須有基表中所不能為空,或沒默認(rèn)值的字段

---單表視圖插入數(shù)據(jù)? : 視圖不包含所有不允許為空的字段? insert into my_v1 values(null,'張山','男',15,180,3)

--單表視圖插入數(shù)據(jù)? ?insert? into my_v2 values(2,'Python1903','A204')

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

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

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

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

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

更新限制? with check option

---視圖? age 字段限制更新

create view my_v4 as?

select * from? my_student where age>30 with cheak option

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

--將視圖可以查到數(shù)據(jù)改成年齡小與30? ?update my_v4 set? age = 29 where id = 5??

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

uodate my_v4 set? age = 32 where id=3

最后編輯于
?著作權(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ù)。

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