1. 程式人生 > >SQL必知必會--基礎篇(一)

SQL必知必會--基礎篇(一)

    最近又把《SQL 必知必會》仔細翻了一遍,因此將基礎知識整理回顧,加深印象。

  • sql 結構化查詢語言(Structured  Query Language  的縮寫),用於訪問和處理資料庫;
  • sql 不區分大小寫,處理時空格被忽略;
  • 多條語句必須以分號(;)分隔,建議每條語句末端都使用分號。

本篇包含知識點如圖:

假設有兩張表:student(sno,name,sex,age,class,addr) ,sno為主鍵

                        grade(id,sno,Math,Chinses,English),id為主鍵

以下sql語句,基於mysql

資料庫編寫

一、檢索資料(select)

select sno from student;           # 檢索單個列
select sno,sex,age  from student;  # 檢索多列
select * from student;             # 檢索所有列      優點:能檢索出名字未知的列   缺點:降低檢索效能

檢索唯一不同/不重複 值 :  distinct

select distinct name from student;

注意:distinct 關鍵字,作用於所有列,而不僅僅是跟在其後的那一列。

下面的語句,因為指定的兩列不完全相同,所以會返回student所有的行。

select distinct name,addr from student;

限制檢索結果:limit 

select name from student    # 檢索student表中從第 3 行起的 4 行資料
limit 4 offset 2;           # 第一個被檢索的是第0行,所以 2 實際是檢索第 3 行

limit 指定返回的行數,offset 指定從哪裡開始。

二、排序(order by)

升序(預設的):ASC    降序:DESC     (order by 必須是select 的最後一條子句)

單個列排序

select name,age feom student
order by age;                    # 按年齡升序排列

多個列排序

select name,age,class from student
order by age,class;                   # 先按年齡排,年齡相同再按班級排

指定方向排序

select name,age,class from student
order by age DESC,class DESC;         #若在多個列上降序,必須對每一列指定DESC關鍵字 

三、過濾資料(where / and / or / not / is null / between / in )

where +條件

select name,age,class feom student    
where age > 15;                           # 檢索年齡大於15歲的
select name,age,class feom student
where age <= 18;                          # 檢索年齡小於等於18的
select name,age,class feom student
where age = 14;                           # 檢索年齡等於14的
select name,age,class feom student
where age != 12;                          # 檢索年齡不等於12的

邏輯運算:and / or / not         優先順序:( ) > not> and >or

select name,age,class feom student
where age = 10 and class = 14;        # 同時滿足兩個條件的值   年齡=10 且 班級=14
select name,age,class feom student
where age = 15 or class = 16;         # 滿足其中任一條件的值   年齡=15 或者 班級=16
select name,age,class feom student
where not age = 15;                   # 不包含該條件的值       年齡!=15

特殊條件:is null / between / in

(1)NULL:無值,它與欄位包含0,空字串或僅僅包含空格不同;無法比較NULL和0,因為他們是不等價的。

判斷是null值:is null            判斷不是null值:is  not null

select name,addr from student
where addr is null;              # 地址是null值
select name,addr from student
where addr is not  null;         # 地址不是null值

(2)範圍值檢索  between and / or

select name,age from student
where age between 12 and 16;    # 年齡在12~16之間
select name,age from student
where age in(10,12,14,16,18);   # 年齡是括號中的值的

PS: in 與 or 能完成相似的功能,但 in 更好。

因為:in操作符的語法更清楚、直觀;求值順序更易於管理;執行速度相對or要快一些;可以包含其他select語句。

(3)like 模糊檢索(% ,_ , [ ])

%:任何字元出現任意次數,匹配0個、1個或多個(不匹配null)

select name from student
where name like '%mark%';    # 匹配 name 包含 mark 的
select name from student
where name like '%mark';     # 匹配 name 以 mark 結尾的
select name from student
where name like 'mark%';     # 匹配 name 以 mark 開頭的 

_ : 匹配單個字元(總是剛好匹配一個字元,不能多也不能少)

select name from student
where name like '_ark';     # 匹配長度為4,且後三個字元為ark的,例如 mark/lark/hark
select name from student
where name like 'mar_';     # 匹配長度為4,且前三個字元為mar的,例如 mard/marl/maef
select name from student
where name like '_ma_';     # 匹配長度為4,且中間兩個字元為ma的,例如 mmaa/smad/kmaf

[ ] :匹配括號中任意一個字元,只能匹配單個字元;可以用字首字元^來否定

select name from student
where name like '[mk]%';    # 匹配以 m 或 k 開頭的,例如 mark/kind/mind/kol
select name from student
where name like '%[mn]';    # 匹配以 m 或 n 結尾的,例如 moon/han
select name from student
where name like '[^abc]%';  # 匹配不以 a 或 b 或 c 開頭的,例如 doop/lamb

PS:使用萬用字元的技巧

使用萬用字元檢索,要比其他的檢索花費更長的處理時間;能不用就不用,非要用就儘量不要放在搜尋的開始處。把萬用字元放在開始處,搜尋起來是最慢的。