- 注册时间
- 2008-9-13
- 最后登录
- 1970-1-1
- 在线时间
- 0 小时
- 阅读权限
- 200
- 积分
- 0
- 帖子
- 24482
- 精华
- 4
- UID
- 9
  
|
SQL分类:) ]: x. e' f, r& |
DDL―数据定义语言(Create,Alter,Drop,DECLARE) Z5 }1 d# Y6 Y: k: x# `# {% o
DML―数据操纵语言(Select,Delete,Update,Insert)
- K( @( u$ [! x i' Q& [. yDCL―数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
; A+ z2 s6 [% c: j8 f# ^9 \$ ^, c' `- b
首先,简要介绍基础语句:& B4 J; C/ F7 b
1、说明:创建数据库
# t* L( x2 l) r# E8 ?( ?Create DATABASE database-name
6 E0 |, B/ T. g x$ N- o2、说明:删除数据库+ @( j3 _& k' l& \* [' e
drop database dbname) M2 X1 _# \% J9 z( F
3、说明:备份sql server8 a. t7 G6 |! O T4 Z! i
--- 创建 备份数据的 device
: l% j+ Z8 d, l( [7 `$ X- {. r) SUSE master0 u; n" J, E$ l
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
" b$ e* |5 C7 d# D" _--- 开始 备份2 e* b- P3 \! r; |$ Y- c4 O
BACKUP DATABASE pubs TO testBack
9 e! Q3 I( p4 k( ~3 ~% j4、说明:创建新表
$ z5 ^7 F% b4 j6 l5 w' o' ycreate table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
6 ~' C) N# k" Z; ^6 D' A根据已有的表创建新表:
& X% O, m8 Q, s4 S0 p+ D2 G* RA:create table tab_new like tab_old (使用旧表创建新表)0 L7 ?) A" q8 C1 A7 V, s% @: M
B:create table tab_new as select col1,col2… from tab_old definition only
/ a4 f/ r0 K4 {7 i4 a- ~/ ~6 `5、说明:删除新表
* V& Q" L, y+ o/ O Wdrop table tabname7 r1 U2 P, d& `' _! R
6、说明:增加一个列* f$ N" t* m7 X/ ?) r) o+ O j6 `
Alter table tabname add column col type; D7 J/ Z$ \; }* H
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。0 C4 Z4 T( r4 P) {: G0 P+ I7 f/ Z/ E
7、说明:添加主键: Alter table tabname add primary key(col)
" G& c2 v4 ] ?) |说明:删除主键: Alter table tabname drop primary key(col)
7 e8 }0 T. B& Y: X q$ n" Z' ~4 u8、说明:创建索引:create [unique] index idxname on tabname(col….)
! \# N* b0 B. M5 D N删除索引:drop index idxname
, u5 S% s, g" n/ C5 E# w注:索引是不可更改的,想更改必须删除重新建。; n9 G! k" U% ^
9、说明:创建视图:create view viewname as select statement* G7 l" `8 N1 m
删除视图:drop view viewname
7 A8 O |( s4 @9 r10、说明:几个简单的基本的sql语句$ p7 @' z" @" s# ^
选择:select * from table1 where 范围, ^ m( _' y0 Y: }' v& V, H
插入:insert into table1(field1,field2) values(value1,value2) D% f( E) r9 }: J
删除:delete from table1 where 范围
1 |7 J: w- c+ f; m更新:update table1 set field1=value1 where 范围
6 S s( U* Z4 `( ~) S查找:select * from table1 where field1 like ’%value1__’ ' _+ |8 O$ L3 g1 k) J0 T1 N
排序:select * from table1 order by field1,field2 [desc]# b s2 J. N {: M0 t
总数:select count * as totalcount from table1* ^& g1 W7 N, e3 g8 e4 B4 K
求和:select sum(field1) as sumvalue from table1% {, F# P- X) \7 N
平均:select avg(field1) as avgvalue from table1
! \; r# K" T/ ] X, N最大:select max(field1) as maxvalue from table1, U1 r! J0 f4 m( A) X8 E4 O
最小:select min(field1) as minvalue from table17 i9 d5 i& o- z$ x1 v# B. J
11、说明:几个高级查询运算词0 v* s- C, ?" Z0 \0 K
A: UNION 运算符# n. w% m" I8 N" ?" ^/ _$ Y
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。/ j4 y, M: F0 {. r
B: EXCEPT 运算符; Z, m3 [$ \7 j& A8 ]% F
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
. ^. U; q2 g' _ h% `* l! |C: INTERSECT 运算符# F9 h7 u$ O8 u* c- T
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。+ `2 f0 Z: Z# |( x0 r* n
注:使用运算词的几个查询结果行必须是一致的。
: t% e( P2 u+ T$ F3 ?2 ?# F. j% c, V3 g }
12、说明:使用外连接
+ g, w" j. n# P. T+ yA、left outer join:! A0 m) _# ^+ N2 e' {7 C# x
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
8 N) ]1 C" D# l5 G6 N/ W5 YSQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c4 i! o6 V b% ?" Q6 @
B:right outer join:
[8 q+ C- \% D5 B5 v0 n右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
+ l3 B3 d1 U, ]% |! _& Y, W$ v, YC:full outer join:
+ [# \! F( T- M8 A% j! `) k1 K2 S全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。* Y6 q6 Z3 M* f. _" M' R
8 n' U9 [0 E+ H g) V
其次,大家来看一些不错的sql语句
; u) w5 u- _7 P; F% M) O1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
* v1 ]( s3 B: O! K法一:select * into b from a where 1<>1
5 ` M! |5 R) p法二:select top 0 * into b from a7 Q% |! j2 P& h' r
0 {8 k$ N( j3 p2 s0 {2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)+ x! I9 k( O7 f m0 N* v
insert into b(a, b, c) select d,e,f from b; M+ l* b& p( Q _4 B
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
" a1 G9 i3 m5 }9 x4 Minsert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件" `1 m0 c( Z: _) G
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
; K2 ^6 S: C+ k* M* \, }3 k
$ z- W+ B9 X$ G; n4、说明:子查询(表名1:a 表名2:b)
?; R2 J% C9 d7 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)
9 K }# I$ f* W* |2 }6 T) b L+ o! X1 m- u7 l% y
5、说明:显示文章、提交人和最后回复时间7 w" O) C8 W9 H" B: E; e: _
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
& p2 A4 G P4 J, Q( H( }' y% R3 g3 f
6、说明:外连接查询(表名1:a 表名2:b)
+ Y1 h Z( d# ^ x+ C7 q2 mselect a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
1 p$ f$ S f$ z: z4 P; O
; x2 s* g, Q) R' a- @! w+ b! N7、说明:在线视图查询(表名1:a )
# B9 P/ D( e2 [: K9 K' Hselect * from (Select a,b,c FROM a) T where t.a > 1;
3 k4 H, e$ i* d: n; ~. n O
) K: o& k9 K1 w$ f8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
$ x* g( d( g2 I0 D) h6 R2 ^select * from table1 where time between time1 and time2
5 K, H. u3 s# I3 `: Lselect a,b,c, from table1 where a not between 数值1 and 数值2
8 o9 f( q- g5 g N G- [' n, k( x$ I7 ?# I3 V3 u
9、说明:in 的使用方法
1 h$ B, B4 {. p% m1 I( fselect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
: d/ M+ p+ x4 u2 n6 n. K9 K. Z( Q/ K0 ^# Y& ]9 z+ V3 C; z
10、说明:两张关联表,删除主表中已经在副表中没有的信息
$ d9 z! `& U6 k, L! A @delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )3 [0 ?: g5 p. s, v5 t, z; c
h$ v/ z% ]( w+ R11、说明:四表联查问题:. V( k& n. p, }. 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 .....
: ?1 E0 G( y9 O: U# @/ {4 B( @; h e
12、说明:日程安排提前五分钟提醒; {" b7 D9 @ M. l* ^
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
" M* n/ m0 R! f8 E) {; `3 v2 u: }+ p
13、说明:一条sql 语句搞定数据库分页
4 G6 @) M+ i: Y( D3 d) O( vselect top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段( v0 a0 U* }0 q O
8 @3 a5 s3 L( X i2 M4 ~14、说明:前10条记录
" c/ C" h3 K) e- r* fselect top 10 * from table1 where 范围' M( U; x% d9 z0 |/ ?5 R
1 H! c8 w8 Z' z/ ?* l" T
15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)' M- Z/ s8 G; m, L6 u8 }
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)4 V! {5 q" w: }2 q
4 \% [% H# M' F0 N0 Z16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表+ a& c3 Q/ q+ a) e
(select a from tableA ) except (select a from tableB) except (select a from tableC)2 V& |4 t" A% k/ O( F
& Q2 u5 F4 r, n8 ?3 M. y; o2 c7 z# V17、说明:随机取出10条数据
, c: M, ~) t _/ Cselect top 10 * from tablename order by newid()& e" h% S: G' w0 e* f
, e! s+ t0 |. f# V5 X/ m9 D18、说明:随机选择记录
l! w$ Y Z$ x7 lselect newid()
% E# {; E p0 y- g8 n
2 v' C, o( F6 S+ A" M/ F19、说明:删除重复记录
8 }* t# `; U* o, e3 w' o3 sDelete from tablename where id not in (select max(id) from tablename group by col1,col2,...)$ S& @, Z( c/ C) c
5 h, }' _' l5 X& \' k20、说明:列出数据库里所有的表名
0 W3 ], D# Q7 j, Cselect name from sysobjects where type='U'' R3 L$ Y* p7 w3 L& T
, y+ q' c; n3 V# f3 t21、说明:列出表里的所有的
, G+ c! z7 f0 h; f4 ~; o+ hselect name from syscolumns where id=object_id('TableName')1 J. v$ V: q M4 W2 M0 [- t4 n
! ^3 ~/ |( d8 v3 y8 T0 x
22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。3 G* Y6 [$ c) n5 R# e
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 type5 J9 S2 u! C4 K! C, U7 {8 F
显示结果:
! u1 j6 n9 A- _2 n8 i! [type vender pcs
- v( E. D* ^; U/ [5 R+ d电脑 A 1
$ R5 O' N" p, _' D* z9 R2 ^电脑 A 1
% G+ B, L" U3 l5 G9 L光盘 B 2
( x; S4 ^! r0 C9 n. H4 E光盘 A 2
5 J9 q; k- D' j5 J; J# A手机 B 3' B* ]: {# t5 Z8 Z: B( [
手机 C 3) K' G7 p0 a; g9 e
, _: I! L! T& p& ~% c/ l
23、说明:初始化表table19 E# G! S: Q5 D) T) y6 n
TRUNCATE TABLE table18 Q' q6 d$ K$ ~5 M& _
3 c+ h" ~* _5 s o! a24、说明:选择从10到15的记录, H. n g. e- Q9 |# x1 x! _
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc |
|