學習 SQL 文檔
2024-07-21 02:08:29
供稿:網友
數據庫: sql sever 2000
注意:
如果你將sql sever配置為使用完整安全或混合安全,那么你可以使用可信連接。如果你使用標準安全,你則需要提供用戶 帳號 和密碼。
庫名: pubs (包含一個虛擬的出版商使用的各個表;安裝好就有的,本文例子就用此表講解)
調試工具: sql 查詢分析器 (允許執行交互的sql查詢,在把查詢語句寫進程序之前進行測試是非常有用的。)
選庫 : 在查詢窗口頂部的 db下拉框中選擇數據庫pubs,這樣你就選擇了數據庫。
1 例子
1.1 記錄查詢 ( 附 :有編號)
1.1.1 簡單select查詢語句
1.1.1.1 描述:
select 字段 1, 字段 2, …… from 表 [where 條件]
1.1.1.2 sql語句:
" select au_lname, phone from authors
" select * from authors where au_lname ='ringer'
1.1.1.3 結果:
1.1.1.4 注意:
1.1.2 操作多個表
1.1.2.1 描述:
1.1.2.2 sql 語句
" select au_lname ,title from authors, titles
" select title,pub_name from titles,publishers where titles.pub_id=publishers.pub_id
1.1.2.3 結果:
1.1.2.4 注意:
1.1.3 操作字段
1.1.3.1 描述:
1.1.3.2 sql 語句
" select phone as '電話號碼' from authors where au_lname ='ringer'
" select phone '電話號碼' from authors where au_lname ='ringer'
" select price * 2 from titles
" select price "original price", price * 2 "new price" from titles
1.1.3.3 結果:
1.1.3.4 注意:
你可以使用大多數標準的數學運算符來操作字段值,如加(+),減(-),乘(*)和除(/)。
你也可以一次對多個字段進行運算.
1.1.4 排序查詢結果
1.1.4.1 描述:
1.1.4.2 sql 語句
" select au_lname from authors order by au_lname
" select au_lname ,au_fname from authors order by au_lname ,au_fname
" select au_lname,au_fname from authors order by au_lname ,au_fname desc
1.1.4.3 結果:
1.1.4.4 注意:
警告:
不是特別需要時,不要對查詢結果進行排序,因為服務器完成這項工作要費些力氣。這意味著帶有order by 子句的select語句執行起來比一般的select語句花的時間長。
1.1.5 取出互不相同的記錄
1.1.5.1 描述:
1.1.5.2 sql 語句
" select distinct au_lname from authors where au_lname = 'ringer'
1.1.5.3 結果:
1.1.5.4 注意:
警告:
如同order by子句一樣,強制服務器返回互不相同的值也會增加運行開銷。福氣不得不花費一些時間來完成這項工作。因此,不是必須的時候不要使用關鍵字distinct。
1.1.6 集合函數
1.1.6.1 描述:
? 可以統計記錄數目,平均值,最小值,最大值,或者求和。
1.1.6.2 sql 語句
" select avg( lowqty ) 'the_average' from discounts
" select count( au_lname ) from authors where au_lname= 'ringer'
" select count( distinct au_lname ) from authors where au_lname= 'ringer'
" select count( * ) from authors where au_lname= 'ringer'
" select sum( min_lvl ) from jobs
" select max( min_lvl ) from jobs
" select min( min_lvl ) from jobs
1.1.6.3 結果:
1.1.6.4 注意:
1.1.7 通過匹配來取出數據
1.1.7.1 描述:
? 百分號是通配符的例子之一。它代表 0個或多個字符。
? 中括號([])用來匹配處在指定范圍內的單個字符。
? '[abc]%'任何一個其名字以這些字符中的任一個開頭記錄都將被返回。
? 脫字符( ^)來排除特定的字符。
? 通過使用下劃線字符( _),你可以匹配任何單個字符。
1.1.7.2 sql 語句
" select royalty from titles where royalty >= 10 and royalty <= 12
" select royalty from titles where royalty between 10 and 12
" select royalty from titles where royalty not between 10 and 12
" select royalty from titles where royalty = 10 or royalty = 12
" select royalty from titles where royalty in (10,12)
" select type from titles where type like '%popular_comp%'
" select type from titles where type like '[a-m ]%'
" select type from titles where type like '[abc]%'
" select type from titles where type like '[a-fm]%'
" select type from titles where type like '[^(a-fmt)]%'
1.1.7.3 結果:
1.1.7.4 注意:
注意:
如果你想匹配百分號或下劃線字符本身,你需要把它們括在方括號中。如果你想匹配連字符 (-),應把它指定為方括號中的第一個字符。如果你想匹配方括號,應把它們也括在方括號中。例如,下面的語句返回所有其描述中包含百分號的站點:
1.1.8 轉換數據
1.1.8.1 描述:
? sql sever 把大部分數值從一種類型轉換為另一種類型。例如,要比較smallint型和int型數據的大小,你不需要進行顯式的類型轉換。sql sever會為你完成這項工作。
? 當想在字符型數據和其它類型的數據之間進行轉換時,需要自己進行轉換操作。
? 函數 convert( )
1.1.8.2 sql 語句
" select convert( char(8),price) + '$' as '錢' from titles
1.1.8.3 結果:
1.1.8.4 注意:
函數 convert( ) 帶有兩個變量。第一個變量指定了數據類型和長度。第二個變量指定了要進行轉換的字段。