2012-05-03 SQL 學習筆記

2012.5.3 SQL 學習筆記

——張偉


順序:建數據庫->建表->設主鍵


數據類型:
->bit 相當於布爾類型(只能是0或1)
->char(10)限制長度只能10個字符
->bigint 大整數時候用
->nvarchar(50)
->nvarchar(MAX)無限長的字符
備註:加n表示可能含有中文
->varchar不含中文信息的字符

varchar和char的區別:
->char當數據長度不滿足時,自動用空格填充
->varchar按當前輸入的長度存儲

SQL語句:是和數據庫交談的語句
->語句中字符串用單引號
->關鍵字不區分大小寫,但字符串值(單引號括起來的)區分
->create table +表名(創建表)
->drop table+表名(刪除表)
->insert into person1(number,name,age,nickname) values(3,‘張三’,20,‘aa’)插入語句

SQL分爲:
->DDL數據定義語言
->create table, drop table,alter table等
->DML數據操作語言
->select,insert等


主鍵的選擇策略:
->必須滿足唯一,不能有重複,通常用int,bigint或uniqueidentify數據類型

主鍵自增:(標識列:又稱自動增長字段)
->在標識規範裏可改動,即把標識規範設置爲是
->標識增量:用於按指定的大小增長
->一個表只能有一個標識列(通常都用在主鍵上)

GUID生成器:
->一種算法,每次生成的字符串永遠不會重複
->用網卡MAC、納秒級時間、芯片ID算出的
->SQL中生成GUID函數:newid()
->C#中生成GUID方法:Guid.NewGuid(),返回是GUID類型

自動增長字段優缺點:
->優點:佔用空間小,無需人員干涉,容易讀懂
—> 缺點:效率低,數據導入導出不方便

GUID優缺點:
->優點:效率高,數據導入導出方便
->缺點:佔用空間大,不易讀
->做主鍵時與插入的數據順序正好相反

業界主流傾向於使用GUID


數據插入:
->insert into person3(name,age) values('aa',36)或insert into values ('bb',23)有自動增長的字段變量不需要寫
->insert into person4(id,name,age) values(newid(),'aa',23)設置沒有字段增長時需要寫

數據更新:
->update person3 set age=30;把所有age設置爲30
->update person3 set age=50,name='a';將所有的數據age設置爲50,name設置爲a
->update person3 set age=age+2;可以單獨統一增長設置
->update person3 set Nicname=N'a' where age>=20;把年齡大於20歲的人的nicname都設置爲a(前面加N是爲了防止有中文發生錯誤)
->update person3 set Nicname=N'a' where age=20;把年齡等於20歲的人的nicname設置爲a,注意只有一個=號,不同於C#
->update person3 set Nicname=N'a' where age=20 or age=30(或表達式,還有and,not)把年齡等於20或等於30歲的、、、、

刪除數據:
->delete from person
->delete只是刪除數據,表還在,drop表示徹底消除了
->delete from person where age>20;把person表中年齡大於20歲的數據刪掉

數據檢索:
->select * from person;把person表中所有數據檢索出來
->select name from person;把person表中所有的名字顯示出來
->select * from person where age>20;把person表中年齡大於20歲的數據顯示出來
->select name as 姓名,age as 年齡 from person where age>20;按自己易懂的方式顯示出年齡大於20歲的成員數據
->select newid(),select 1+3,select getdate(),select 00version這些語句可以檢索和表無任何關係的內容
->select count(*) from person;統計表中所有數據條數
->select max(age) from person;求年齡最大值
>select min(age) from person;求年齡最小值
>select avg(age) from person;求年齡平均值
>select sum(age) from person;求年齡和
->select count(*) from person where age>20;統計person表中年齡大於20歲的人的個數

數據彙總:
->即聚合函數:MAX,MIN,SUM,AVG等

數據排序:(order by)
->select * from person order by age asc;按年齡排序由小到大
->asc升序
->select * from person order by age desc;按年齡排序由大到小
->desc降序
->select * from person order by age asc,salary desc;將person表中的年齡按升序排列,如果年齡相同則工資按降序排列(用逗號隔開,靠前的優先)
->select * from person where age>20 order by age asc,salary desc;篩選數據後的排序,where一定要在order by之前

通配符匹配查詢:
->單字符:半角下劃線_,只代表一個字符
->select * from person where name like '_ucy';檢索出名字中只包含四個字符且最後爲ucy字符的人
->多字符:用百分號%,代表0或多個字符
->select * from person where name like '%n%';檢索出名字中所有包含n字符的人名

空值處理:null(表示不知道,不是表示沒有)
->有null參與的所有運算結果都是nulll
->select *from person where name is null;檢索出名字爲不知道的。注意用is,不爲空就用is not null,不能用=或!=

多值匹配:
->select * from person where age=20 or age=30 or age=32;檢索出年齡爲20歲,30歲和32歲的人
->select * from person where age in(20,30,32);同上,檢索出年齡爲20歲,30歲和32歲的人
->select * from person where age>20 and age<30;檢索出person表中年齡在20歲和30歲之間的人
->select * from person where age between 20 and 30;同上,檢索出person表中年齡在20歲和30歲之間的人

數據分組:group by 
->select count(*) from person;顯示出所有person表中數據的條數
->select age,count(*) from person group by age;按年齡檢索出數據,且顯示每個年齡的條數
->group by 必須放到where語句之後
->select age,avg(salary) from person group by age;按年齡檢索出數據,且顯示每個年齡段的平均工資。注意:沒有出現在group by子句中的列是不能放到select語句後的列名錶中的(聚合函數中除外)

Having 語句:
->在where函數中不能使用聚合函數,必須使用having 
->select age,count(*) from person group by age having count(*)>1;按年齡檢索,且顯示出每個年齡段的人數大於1的組

where和having區別:
->where:對分組前數據進行過濾
->having:對分組後數據進行過濾

去掉重複數據:distinct
-> select department from person;顯示出person表中department的所有信息
->select distinct department from person;只顯示出person表中不同department的信息

聯合結果集:union(合併後會自動去掉重複的)、union all(所有的都在,不去掉)
->select name, age from tempemployee;把零時工的年齡和姓名顯示出來
->select name,age from tempemployee union select name,age from employee;把零時工和正式工的年齡和姓名合爲一個結果 顯示出來
->select name,age,department from employee union select name,age,'臨時工',‘部門’ from tempemployee;前後兩個的變量數量和變量類型要相同,不夠的要補全

案例1:
要求查詢員工的最低年齡和最高年齡,零時工和正式工要分別查詢
->select '正式員工最高年齡' max(age) from employee;
union all
select '正式員工最低年齡' min(age) from employee;
union all
select '零時工最低年齡' max(age) from tempemployee;
union all
select '零時工最低年齡' min(age) from tempemployee;

案例2:
查詢每位正式員工的信息,包括工號,工資,並且在最後一行加上所有員工工資額合計
->select number,salary from employee union select '工資額合計',sum(salary) from employee;
數字函數:
->abs():求絕對值
->ceiling():舍入到最大整數,例如3.33被變成4,-3.6被變成-3
->floor():舍入到最小整數
->round():四捨五入

字符串函數:
->len():字符串長度
->lower(),upper():轉小寫,大寫
->ltrim(),rtrim():去左空格,去右空格
->substring():取子字符串

日期函數:
->getdate():當前日期
->dateadd(datepart,number,date):date爲計算的日期,number爲增量,datepart爲計量單位。例如:dateadd(day,3,date)即計算日期date的3天后的日期,dateadd(month,-8,date)爲計算日期date的8個月前的日期
->datediff(datepart,startdate,enddate):計算兩個日期之間的差額
->datepart(datepart,date):返回一個日期的特定部分,例如:datepart(month,getdate())爲返回當前時間的月份

類型轉換函數:
->cast(expression as date_type):例如cast('123',as int),cast('2008-03-09' as datetime)
->convert(date_type,expression):例如:convert(datetime,'2008-03-03'),convert(varchar(50),13)

空值處理函數:
->isnull(name,新名字):即如果爲空就編程新的名字,不是的還是保持原來的名字

case函數:
->類似於c#中switch case語法:例如
select name
{
case level
when 1 then a
when 2 then b
when 3 then c
else default
end
} as 等級 from person

select name,
{
case
when salary<2000 then '低收入'
when salary>=2000 and salary<=5000 then '中等收入'
else '高收入'
}as '收入水平' from person


case函數案例:
表中有ABC散列,用SQL語句實現:當A列大於B列時選擇A列否則選擇B列,當B列大於C列是選擇B列否則選擇C列。
->select(case when a>b then a else b end),(case when b>c then b else c end) from talbe

表連接:join 
->select o.billnumber,c.name,c,age from orders as o join customers as c on o.customerID=c.ID;當中o和c分別爲另起的別名,這樣就把訂單表和客戶表當中的ID對應起來了,並顯示出與各自ID對應的賬單號,姓名和年齡
->select o.billnumber,c.name,c,age from orders as o join customers as c on o.customerID=c.ID where c.age>15;顯示出所有年齡大於15歲的顧客購買的訂單號、客戶姓名、客戶年齡
->select o.billnumber,c.name,c,age from orders as o join customers as c on o.customerID=c.ID where c.age>(select avg(age) from customers);顯示年齡大於平均年齡的顧客購買的訂單 
 
子查詢:
將一個查詢語句當做一個結果集供其他SQL語句使用
->select * from reader where yearofjoin in{select distinct yearpublished from book};顯示出書出版那一年加入的會員
->select * from{select row_number() over(order by salary desc) as 列,number,name,salary,age from employee} as 表2 where 表2.列>=3 and 表2.列<=5;當中row_number()爲數據庫2005版以上新增的功能,即能增加一列並自動遞增行號。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章