- 注册时间
- 2008-9-13
- 最后登录
- 1970-1-1
- 在线时间
- 0 小时
- 阅读权限
- 200
- 积分
- 0
- 帖子
- 24482
- 精华
- 4
- UID
- 9
  
|
SQL分类:
/ R5 ^# o$ T8 Q/ e* ZDDL―数据定义语言(Create,Alter,Drop,DECLARE)0 O7 `0 y* a# P* |, J! m+ E& ?7 }
DML―数据操纵语言(Select,Delete,Update,Insert) P9 y$ o6 b: p0 v
DCL―数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)7 K% G. Z+ r( p: `; D% p
% X6 T) C }* O4 |
首先,简要介绍基础语句:- o! A" Q z' N. N% k
1、说明:创建数据库- ?) f$ H% Y8 |7 r' p N/ [
Create DATABASE database-name2 |& s8 {3 L& c) t
2、说明:删除数据库
7 n- V- L' B5 `9 x0 S6 w5 t, A7 n: `3 qdrop database dbname% e5 b: `& ]5 H- b: C( A
3、说明:备份sql server
Z( h3 X7 }* |& w1 Y--- 创建 备份数据的 device( P, G6 n! u8 {4 s$ I' b5 F+ N7 I
USE master& E% Q( I2 J% i5 E% }
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'! \! f, Y: a9 M' n/ r! J
--- 开始 备份1 [8 T4 E7 X: J7 A4 E. g% M
BACKUP DATABASE pubs TO testBack
( A Q* q7 O- `, z4、说明:创建新表
+ B$ z d$ h( v' X: }5 dcreate table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
) a$ v2 g- ~! j) l$ \) `根据已有的表创建新表:
3 ?) @6 G F- [& P; {2 _4 rA:create table tab_new like tab_old (使用旧表创建新表)
5 r. y% [* y$ H/ c' Z, aB:create table tab_new as select col1,col2… from tab_old definition only/ M. b' h6 g2 q6 A! K" i
5、说明:删除新表
% F/ n5 M/ P3 R5 i; u3 bdrop table tabname5 M `$ P0 f' W, [$ [3 @. Y
6、说明:增加一个列
* r- a0 I+ x# r+ v" Q( JAlter table tabname add column col type
( J7 X9 _. a- a4 c% h注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。( K+ }# Y% F8 A# {" d
7、说明:添加主键: Alter table tabname add primary key(col)
6 {9 S" x" L. V+ D3 f, ^9 H. w说明:删除主键: Alter table tabname drop primary key(col)
! F/ N- K/ q4 E; N: M: `8、说明:创建索引:create [unique] index idxname on tabname(col….)& U$ o0 B: h. U6 t5 \1 Z# Q
删除索引:drop index idxname
3 f# [2 w8 J" C注:索引是不可更改的,想更改必须删除重新建。
+ Q) I3 S: u8 ]& D. f8 G9、说明:创建视图:create view viewname as select statement2 L0 e. g, Y1 a# z
删除视图:drop view viewname- h( G7 V |; v1 u
10、说明:几个简单的基本的sql语句
1 d" c2 m* w) U. E% g- T+ `选择:select * from table1 where 范围
8 t5 e3 M: \( y1 Q2 _9 W9 ?9 p c插入:insert into table1(field1,field2) values(value1,value2)2 x5 x: T0 z+ J, H* D% O
删除:delete from table1 where 范围; ~: [* O/ }5 ]+ u6 ^. n
更新:update table1 set field1=value1 where 范围
7 \( _: i5 w) L( K" y! t查找:select * from table1 where field1 like ’%value1__’
" D1 V# ~ Y) c- ^; r' n8 V排序:select * from table1 order by field1,field2 [desc]
z% W9 ]- Q! L7 y8 P& \3 U总数:select count * as totalcount from table1
) u! G$ B& [/ a) C$ ]- z n+ O9 R求和:select sum(field1) as sumvalue from table1' }+ D6 }6 H! V1 E
平均:select avg(field1) as avgvalue from table1
" C' g9 J$ }9 \最大:select max(field1) as maxvalue from table1
2 X2 {, n6 P0 ~ U+ W最小:select min(field1) as minvalue from table1+ {9 e D j5 B0 z# ]1 ?( @% g
11、说明:几个高级查询运算词
2 i T$ m; `1 N. qA: UNION 运算符
9 M s; V, }; [+ uUNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
/ S2 R$ a0 o2 s1 h- _" F, g7 h4 HB: EXCEPT 运算符$ P& Y7 j5 y a; d
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
8 B3 ^. `7 o9 Q* ?1 gC: INTERSECT 运算符# U! ~. A( {6 ^$ g
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
' j/ P) c; ]' c+ d7 C+ e注:使用运算词的几个查询结果行必须是一致的。0 h. x" e0 S3 A# C" Z
7 m/ ]) c2 m/ N, t. e, O
12、说明:使用外连接
3 t) X$ P& w6 o* M' c6 uA、left outer join:/ B) H: x- v2 b, Q- e
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。& _+ P) A; v6 v ~7 u
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
+ v% T+ ^9 \ D, SB:right outer join:7 v x" Y/ Z( E V8 Y
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。% k7 U! X( B9 n( y, G
C:full outer join:4 F1 _9 |- B0 B! m
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。0 U2 k. f; N/ c" \% z/ m3 I
' F7 [$ X7 t# R" q! w& D其次,大家来看一些不错的sql语句
, @! d2 n+ Z) T n1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
4 _5 b: ~" J" C法一:select * into b from a where 1<>1
; d7 Y. f2 }' h7 l法二:select top 0 * into b from a, u$ k! S9 k& N9 ~
Q/ b% m- h4 }7 I2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)$ f( r2 ^, U& \4 X% A$ z( l
insert into b(a, b, c) select d,e,f from b;. m5 j( H# c. H) v6 U6 y. v9 |
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
8 O" C R* f" N4 M+ w0 Linsert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件$ o3 y5 d( q( \+ A4 n+ j! p
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
9 `) r' [2 Q& A0 V. ]% L; U2 G) c
% O" E: h! O( e9 r5 I! H' T4、说明:子查询(表名1:a 表名2:b)( ]/ b2 B( Y# h& d
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)
( N0 J* I/ {1 E9 F3 a& x. s# Z+ r
5、说明:显示文章、提交人和最后回复时间
# [6 u% h% D v( M0 }! [3 c: f dselect a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b6 Q: D( I# _5 Z/ `2 d8 ]# s3 o# X
. q4 B) {, J) A, P8 m5 A6、说明:外连接查询(表名1:a 表名2:b)
! f: L% k1 N6 W8 k2 W2 g" vselect a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c, h5 r# o d1 w6 _
7 D+ P% D: I0 V. ]5 k9 o0 H7、说明:在线视图查询(表名1:a )
" F5 m" t2 c; Nselect * from (Select a,b,c FROM a) T where t.a > 1;
; S- [, x* T" x! l# i) n
# l& K/ C7 n5 g: m: u9 j! b8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括- ]6 K0 ^. z8 v9 \ J5 q
select * from table1 where time between time1 and time29 |( R% S# o) ~2 G0 c
select a,b,c, from table1 where a not between 数值1 and 数值28 Z) w ^% {/ ~% J: ?& A& P# k
: A9 U7 h% d Q! l# \% W. ~9、说明:in 的使用方法+ q/ Y, X6 J& c+ Q
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)) S: v1 m- Q( T
: `" ^6 I6 _/ Q) x' O10、说明:两张关联表,删除主表中已经在副表中没有的信息
, k( m7 I9 f1 g- j, fdelete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
4 n! v( Z4 H/ J. R# a8 h, b
: {1 F! b1 O |8 ]5 _2 }11、说明:四表联查问题:; G8 H+ P9 Z- [1 ` B$ P1 ]* k( X9 j
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....
$ G: i; s- i3 ^2 ~& T: `3 ~7 O, @( D$ u+ f$ r
12、说明:日程安排提前五分钟提醒 V; `, c( P; D) L
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
+ [) z0 v% {+ v- l0 G, C O3 e
4 k$ Y0 s7 G! o: V. y, }13、说明:一条sql 语句搞定数据库分页
+ ?. O5 R0 B2 fselect top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段
) e) M! R, {3 ~* c8 [7 G! D2 u! C7 H5 r' Z! @5 E. C
14、说明:前10条记录
9 N! e! t6 V! C& r) iselect top 10 * from table1 where 范围
. W* v4 i# A9 R, Z% d; L# I9 e Y- |! V) a) [1 F
15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.); a' H4 P+ M! w4 m% K
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)- v0 f K1 `1 `8 ?7 \
3 d" r% x6 ^& g: r16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
6 H9 q: L0 U! {3 T(select a from tableA ) except (select a from tableB) except (select a from tableC)
- S$ P2 J: k% X3 E, L# l
; W# _3 I6 m! u* Y9 h, ]6 y9 a17、说明:随机取出10条数据
' A+ A9 P" G/ ^- }* o% K2 o( Sselect top 10 * from tablename order by newid()& ~6 W& N: s5 @) h, X7 }: r
1 n1 K* r5 b. e3 }18、说明:随机选择记录2 h; s' `5 [7 l X) N& r9 K& ]
select newid()2 n" k; W% @6 }
; h, V* N0 C# }8 h
19、说明:删除重复记录5 X( J0 P/ f, o) Y: L
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)3 A* B7 n- V8 a
0 ]1 g) u9 d. E+ n3 Q
20、说明:列出数据库里所有的表名
( I* |/ ? j7 ^% n+ fselect name from sysobjects where type='U'4 l4 r) s$ f3 i f2 n( q; `; j# r$ |
7 ^' K% w# d- v9 x k$ h: m6 m7 {3 L
21、说明:列出表里的所有的$ e! B" s. g2 O" Z3 U: \
select name from syscolumns where id=object_id('TableName'), m F8 }, {( k7 n1 K
4 n: @* p9 I2 y, ?9 F22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。: A4 r5 V& y2 K/ ^) a
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
2 K. [: v' g: Q( f显示结果:9 \0 {; T2 N& G, l3 p( ^
type vender pcs
3 k4 C) v) ^/ q: n( K' a l电脑 A 1( U5 j' R+ ?/ u3 ?7 l* ]# \; l3 p
电脑 A 1
9 y' g u, P* c# b$ E) E光盘 B 28 p) v: b' J! j1 q* L( n1 C( N
光盘 A 2
( @! H7 V v+ T7 s手机 B 3% i, c2 \8 B4 V! s' p1 y
手机 C 3& i. _# X$ d6 l1 ~2 U' N* G0 W
4 y- _( b, V* l3 O23、说明:初始化表table1; ]1 V2 S4 c I% G6 p: _
TRUNCATE TABLE table1
/ q9 M/ x: K9 D* W4 V8 C$ v5 `9 {7 @: K8 S: @! \! f
24、说明:选择从10到15的记录) P7 W4 Q# Z& f0 C
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc |
|