47
SQL 的的的的的的的 7-1 的的的的 select 7-1-1 的的的的的的 7-1-2 where like group by 的 order by 7-1-3 的的的的的的的 7-1-4 的的的的的的的 7-1-5SQL 的的的的的 7-2insert 的的的的 7-2-1 的的 1 7-2-2 的的 2 7-2-3 的的的的 7-3delete 的的的的的 update 的的的的 7-3-1delete 7-3-2update 7-3-3update 的的的的

SQL 的資料處理語言

  • Upload
    spike

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

SQL 的資料處理語言. 7-1 選取查詢 select 7-1-1 運算子與函數 7-1-2 where 、 like 、 group by 和 order by 7-1-3 定義欄位的表格 7-1-4 多個資料表查詢 7-1-5SQL 的查詢語言 7-2insert 新增資料 7-2-1 插入 1 7-2-2 插入 2 7-2-3 選取插入 7-3delete 刪除資料和 update 更新資料 7-3-1delete 7-3-2update 7-3-3update 集合資料. - PowerPoint PPT Presentation

Citation preview

Page 1: SQL 的資料處理語言

SQL 的資料處理語言7-1選取查詢 select7-1-1運算子與函數7-1-2 where 、 like 、 group by 和 order by7-1-3定義欄位的表格7-1-4多個資料表查詢7-1-5SQL的查詢語言7-2insert新增資料7-2-1插入 17-2-2插入 27-2-3選取插入7-3delete刪除資料和 update更新資料7-3-1delete7-3-2update7-3-3update集合資料

Page 2: SQL 的資料處理語言

• 資料處理語言 DML(data manipulation language) 。當資料庫的 schema 已經被編譯,使用者可以使用資料處理語言 DML 來操作資料庫。一般的資料處理語言包括抽取資料、插入資料、刪除資料和修改資料這幾種。

• 我們經常用到選取查詢 (select) 敘述、插入(insert) 敘述、更新敘述 (update) 、刪除敘述 (delete) ,這些都是 SQL 的資料操作。

Page 3: SQL 的資料處理語言

1-1 選取查詢 select

• select 欄位名稱串列• from 資料表串列• where 條件• group by 群組條件• order by 排序條件• having by 篩選經過 group by 群組之後的資料。• 使用 select來選取 total_tutor資料表中的資料編號為 5號的資料。

• select * from total_tutor where number=3

Page 4: SQL 的資料處理語言
Page 5: SQL 的資料處理語言

• 選取 total_tutor 資料表的 number 和 name欄位,我們使用 total_tutor.Number 和 total_tutor.name 這兩個欄位,並且查出編號 number=3 的資料。

• select total_tutor.Number,total_tutor.name from total_tutor where number=3

Page 6: SQL 的資料處理語言

我們設定 tutor.salary的資料為 m並且當其 m為 350才符合,而且資料的編號必需大於 150,使用 experience為群組。select tutor.number,tutor.name,tutor.salary as m from tutor where number >150 group by experience having m=350

Page 7: SQL 的資料處理語言

1-1-1 運算子與函數• 在選取敘述中我們用了 number > 150 條件,

而 > 大於就是一個運算子。 MySQL 提供我們基本的運算子,我們可以使用算術運算子來運算資料並且限制資料的條件。

Page 8: SQL 的資料處理語言

算術運算子

Page 9: SQL 的資料處理語言

邏輯運算子

Page 10: SQL 的資料處理語言

比較運算子

Page 11: SQL 的資料處理語言

範例 and

• select number,name,experience• from tutor • where name like “ 吳 %” and experience >

5• order by number desc• 顯示姓吳的和經驗大於 5 在 tutor 資料表的

所有資料

Page 12: SQL 的資料處理語言

範例 or

• 顯示姓吳的或姓徐的家教老師。• Select number,name,experience

• From tutor

• Where name like “ 吳 %” or name like “ 徐%”

Page 13: SQL 的資料處理語言

• MySQL 對於 group by 群組敘述也提供了我們簡單的函數• 群組資料和這些函數讓我們可以分析資料裡面的內容,使

得方便運用及分析資料。• Count 函數:與 group by 敘述,可以得到該群組的數目。• Count(*) 取得群組的數值• 使用 count(*) ,可以算出資料編號從 5 到 150 號的經驗群

組 group by experience ,經驗為 1 的總共有 9 人。• Select total_tutor.name,total_tutor.experience,COUNT(*)

from total_tutor where total_tutor.number <150 and total_tutor.number >5 group by experience

Page 14: SQL 的資料處理語言
Page 15: SQL 的資料處理語言

• avg( 欄位名稱 )• avg( 欄位名稱 ) 搭配 group by 就可以算出該群組

的平均值• 我們以經驗為群組資料編號從 5 到 350 作分析,

得出經驗第 4 和第 6 等級的家教老師終點費需求較底。

• Select total_tutor.experience,avg(salary) from total_tutor where total_tutor.number <350 and total_tutor.number >5 group by experience

Page 16: SQL 的資料處理語言
Page 17: SQL 的資料處理語言

• max( 欄位 )

• max( ) 函數可以算出該欄位經過群組後的最大值

• 從編號 2 到編號 50 ,經驗 6 的群組中最高要求薪資為每小時 350 元的鐘點費。

• Select total_tutor.experience,max(salary) from total_tutor where number >2 and number <50 group by experience

Page 18: SQL 的資料處理語言
Page 19: SQL 的資料處理語言

• min( ) 函數可以算出該欄位經過群組後的最小值

• 在 total_tutor 資料表中,第三級中最少的鐘點費也要 300 元

• Select total_tutor.experience,min(salary) from total_tutor where number >2 and number <50 group by experience

Page 20: SQL 的資料處理語言
Page 21: SQL 的資料處理語言

• stddev( ) 函數• stddev( ) 函數可以算出該欄位經過群組後的標準

差• 我們可以發現第三級、第五級、第七級和第八級

的標準差較小,也就是在同等級中鐘點費較為平均,但第一級和第六級的標準差大,也就是其差異很大。

• Select total_tutor.experience,stddev(salary) from total_tutor where number >2 and number <50 group by experience

Page 22: SQL 的資料處理語言
Page 23: SQL 的資料處理語言

• sum( ) 函數• 我們使用 sum( ) 函數可以求出群組後的總

值• 在 total_tutor 資料表中,以編號 0 號到 50

號資料可以群組分類算出該類經驗欄的總值。

• Select total_tutor.experience,sum(experience) from total_tutor where number >2 and number <50 group by experience

Page 24: SQL 的資料處理語言
Page 25: SQL 的資料處理語言

1-1-2 where 、 like 、 group by 和order by

• where 子句• 語法:• where 欄位名稱 = 運算式 或 ‘字串’• 使用 where 來限制條件• 我們使用 where 來尋找編號為 3 的資料,

並且顯示 total_tutor 資料表中 number 、 name 和 salary 的欄位資料。

• select number, name,salary from total_tutor where number=3

Page 26: SQL 的資料處理語言

• where 欄位名稱 like 子句• 語法 where欄位名稱 like• select total_tutor.number,total_tutor.name fro

m total_tutor where name like "吳%"• %為萬用字元,他是任意長度的任意字元。• 我們可以使用 like子句找尋吳開頭的名子,我們只要在吳後面加入%,就是代表只要為吳開頭的就可以顯示。這是 total_tutor資料表中以吳為開頭的資料。

Page 27: SQL 的資料處理語言

• group by 子句• 將指定同值欄位的紀錄資料群組起來• 語法: group by 群組欄位• select number,name,experience• from tutor where name like “ 吳 %”• group by experience• 我們使用 group by 將經驗相同的值群組起來,下

面範例我們將經驗為 6 的群組起來了編號 127 和313 成為編號 127

Page 28: SQL 的資料處理語言
Page 29: SQL 的資料處理語言

• have 子句• 語法: have 條件• 使用 having 子句來限制條件,通常跟在 group by 之後。• 在這裏限制編號 250 到 256 的資料,我們使用 having n>250 and n<

256 。• select tutor.number as n,tutor.name• from tutor• where tutor.number <256 and tutor.number >250• group by experience• having n >250 and n<256• order by salary• 這裏顯示資料從 250 到 256 以經驗 experience 為群組的資料,並且

以薪資高的排序資料。

Page 30: SQL 的資料處理語言

• order by 子句• 我們使用 order by 來排序某個欄位• 語法: order by 欄位• select number,name,experience• from tutor where name like “ 吳 %”• order by number• 我們根據編號欄位來排序姓名為吳開頭的

資料。

Page 31: SQL 的資料處理語言

• 使用遞減來排序欄位• 語法: Order by 欄位 desc

• select number,name,experience

• from tutor where name like “ 吳 %”

• order by number desc

• 在 tutor 資料表中搜尋以吳為開頭的資料,並依據其編號使用遞減 desc 來排序欄位。

Page 32: SQL 的資料處理語言

1-1-3 定義欄位的表格• 我們使用表格 as 變數名稱,來定義表格。

通常 as 命名表格的代號。• 語法:表格 as 變數名稱• 定義表格 tutor 為 t , t.number 的編號在 1

56 到 153 之間• select number,name,experience

• from tutor as t

• where t.number<156 and t.number >153

Page 33: SQL 的資料處理語言

1-1-4 多個資料表查詢• 當我們需要兩個表格以上作顯示時使用已定義過的表格代號,將更為方便。

• 語法:• select 表格 1.欄位名稱,表格 2.欄位名稱• from 表格 1,表格 2• 我們將 tutor資料表定義為 t的代號,並將 asir資料表定義為 a的代號,然後再用 select來操作資料表的資料。

• select t.name,a.name,a.tcourse• from tutor as t,asir as a• where (t.tcourse=a.tcourse and t.salary=a.salary) and (a.numb

er<15 and a.number >10)• 這是合併 tutor資料表和 asir資料表的資料。

Page 34: SQL 的資料處理語言
Page 35: SQL 的資料處理語言

• join 語法• select 表格 1. 欄位 , 表格 2. 欄位 from 表格 1 joi

n 表格 2• 我們使用 join來合併 tutor和 asir資料表。• select tutor.name,asir.name,asir.tcourse• from tutor join asir• where (tutor.tcourse=asir.tcourse and tutot.salary=asir.

salary) • and (asir.number <15 and asir.number >10)• 這是合併 tutor資料表和 asir資料表的資料。

Page 36: SQL 的資料處理語言

1-1-5SQL 的查詢語言• 一個 SQL的查詢語言可以由六個子句所組成,只有 select和 from是必須要的• select <欄位或函數串列 >• from <表格串列 >• where<條件 >• group by <群組欄位 >• having <群組條件 >• order by <欄位 >• 我們使用 tutor.number as n來設定編號的代號為 n,並且使用 having限制條件時使用運算式,運算式使用資料表的代號 n來作運算。

• Select tutor.number as n,tutor.name• From tutor• Where tutor.number <256 and tutor.number >250• Group by experience• Having n >250 and n<256• Order by salary• 這是編號 250到 256的資料,並且使用薪水作為排序的根據。

Page 37: SQL 的資料處理語言
Page 38: SQL 的資料處理語言

1-2insert 新增資料• 我們使用 insert 指令就可以把資料輸入資料

Page 39: SQL 的資料處理語言

1-2-1 插入 1

• 語法:• insert into 資料表• ( 欄位 1, 欄位 2, 欄位 3….)• values• ( 欄位 1 的值 , 欄位 2 的值 , 欄位 3 的值… ..)• 我們將資料“陳小胖” ,800,” 台北市北投區”按照欄位 (n

ame,salary,location) 依序插入到 tutor 資料表中。• insert into tutor• (name,salary,location)• values• (“ 陳小胖” ,800,” 台北市北投區” );

Page 40: SQL 的資料處理語言

1-2-2 插入 2

• 語法﹕• insert into 資料表• values

• ( 欄位 1 的值 , 欄位 2 的值 , 欄位 3 的值… )

• 我們使用 insert 指令將資料 (1,' 吳佳諺 ',98)插入 student 資料表中。

• INSERT INTO student VALUES (1,' 吳佳諺 ',98);

Page 41: SQL 的資料處理語言

1-2-3 選取插入• 我們可以將選取 select 的資料表 2 資料增加 insert 到資料表 1• 語法:• insert into 資料表 1• ( 資料表 1 的欄位 1, 資料表 1 欄位 2, 資料表 1 的欄位 3)• select ( 資料表 2 的欄位 1 ,資料表 2 的欄位 2 ,資料表 2 的欄位 3)• from 資料表 2• 我們使用 select 指令將 asir 資料表中編號從 50 到 53 的資料插入 tuto

r 資料表中。• insert into tutor• (name,salary,location)• select name,salary,location• from asir• where number<53 and number>50

Page 42: SQL 的資料處理語言

1-3delete 刪除資料和 update 更新資料

Page 43: SQL 的資料處理語言

1-3-1delete

• 我們使用 delete 子句就可以把資料列給刪除• 語法:• delete from 資料表• where 條件• 這樣就可以把 tutor 資料庫中編號為 321 到 323 的資料給

去刪除。• delete from tutor• where number<=323 and number >=321• 我們如果使用 delete 指令而沒有限制條件,則會將所有的

tutor 資料刪除。• delete from tutor

Page 44: SQL 的資料處理語言

1-3-2update

• 如果我們要把資料列給更新則要用 update 子句• 語法:• update 資料表• set 欄位 = 更新的資料• where 條件• 我們使用 update total_tutor 來更新資料編號為 3 的資料,

並且將其 depar 欄位設為”資訊工程”。• update total_tutor• set depar=" 資訊工程 "• where Number=3• 這是還未 update 的資料,其 depar 欄位為中國文學系。

Page 45: SQL 的資料處理語言

• 這是已經被更新的欄位資料, depar 欄位為資訊工程。

Page 46: SQL 的資料處理語言

1-3-3update 集合資料• 我們可以用 in來指定集合的資料。• update total_tutor

• set salary=salary*0.75

• where Number in (3,5,6,7,10)

• 我們使用 update來修正資料編號 Number在3、 5、 6、 7、 10的資料,將這些資料的薪水 salary欄位乘以 0.75。

Page 47: SQL 的資料處理語言