- 注册时间
- 2008-9-13
- 最后登录
- 1970-1-1
- 在线时间
- 0 小时
- 阅读权限
- 200
- 积分
- 0
- 帖子
- 24482
- 精华
- 4
- UID
- 9
  
|
SQL分类:
7 ~8 W. v$ N; f. o+ t8 `7 vDDL―数据定义语言(Create,Alter,Drop,DECLARE)
- G* o# E" K6 \* W2 \ bDML―数据操纵语言(Select,Delete,Update,Insert)" J: M# q2 g0 {& I0 j9 F1 K
DCL―数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)# ]2 s% o Z) }/ U$ d
; F9 B4 ^9 {) m. q# p
首先,简要介绍基础语句:
6 N8 Y2 G; {$ Y0 m* |1、说明:创建数据库
* b- r- k: g; }7 d; v5 mCreate DATABASE database-name
$ q$ @- D3 [4 G2、说明:删除数据库
( j- {6 H# h% J& W: E# |drop database dbname
7 e( V9 S- G9 I- X% d' x, H3、说明:备份sql server
' T$ M# e, z8 m) _" N: c. b! N( W--- 创建 备份数据的 device
+ e# j9 b, P6 L7 P2 o% wUSE master3 K' V% s4 ?7 r7 @2 O
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'& a1 l1 T; E7 f9 o6 p
--- 开始 备份
1 Z; \/ M" l. C* T2 m2 X2 m/ SBACKUP DATABASE pubs TO testBack1 w/ H6 _' l+ u4 z
4、说明:创建新表
. L# M! ]2 {9 t& U5 J8 screate table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
( M" ^, [0 p2 G3 ]: [根据已有的表创建新表:' ]! |( Q' Y+ i3 t0 W
A:create table tab_new like tab_old (使用旧表创建新表)
6 r5 s. I. y6 C( ?/ u- x) E! Q9 R! jB:create table tab_new as select col1,col2… from tab_old definition only
) {$ Q1 l8 m6 K0 R$ Y5、说明:删除新表
1 J0 ]$ U1 }2 g" e6 ldrop table tabname: Q- ?0 k: u# X
6、说明:增加一个列+ _9 d: |6 o# O# F# J
Alter table tabname add column col type, a$ S# t# a @+ W. d; ?
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。5 |0 f$ z2 C+ m8 D" ?+ a5 Z) P/ Q
7、说明:添加主键: Alter table tabname add primary key(col)" C" c" g0 ~/ n9 d( u
说明:删除主键: Alter table tabname drop primary key(col)2 T; w! P6 u, x9 R; M2 T
8、说明:创建索引:create [unique] index idxname on tabname(col….), O% L- I5 i8 Z9 ~3 P
删除索引:drop index idxname- O8 A/ m: q: [" ~* V0 u+ h4 p6 W
注:索引是不可更改的,想更改必须删除重新建。( e, p' ^1 ?4 n9 H/ h4 n! Y- s
9、说明:创建视图:create view viewname as select statement
/ D6 q0 d& D: e5 g! l删除视图:drop view viewname
% Q5 ^9 E9 O1 {# V! E8 P10、说明:几个简单的基本的sql语句
) U; |4 H" @3 A1 l @0 Q选择:select * from table1 where 范围
. p; m* V, V1 z4 ?插入:insert into table1(field1,field2) values(value1,value2), {' c/ F8 \% w9 E! N r' j9 G1 x
删除:delete from table1 where 范围 f' ^5 p5 q+ l# V
更新:update table1 set field1=value1 where 范围8 Z, r- @- ?( Z4 G. U
查找:select * from table1 where field1 like ’%value1__’ - G1 p! |: l& d+ }
排序:select * from table1 order by field1,field2 [desc], X# i9 i+ k# b0 n1 I
总数:select count * as totalcount from table1; R; N. B) K+ z* p1 p
求和:select sum(field1) as sumvalue from table1 g- x( m- {9 x, @% P6 V
平均:select avg(field1) as avgvalue from table1
$ f5 W& o5 `. P' T J- F最大:select max(field1) as maxvalue from table1
1 Y0 S! T: C: k" h% B最小:select min(field1) as minvalue from table19 N9 }. L# s( J0 R- ?! U- K
11、说明:几个高级查询运算词$ K) L8 F1 ]( ^! }
A: UNION 运算符 u* |/ K+ O A
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
5 j9 i9 }/ ?# r* r' tB: EXCEPT 运算符
+ x; ?2 D! e$ y" L6 S6 ~EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。) y2 }5 S# g( b' x
C: INTERSECT 运算符
" c* B( e1 G$ z. EINTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
. T& v5 C4 v/ I注:使用运算词的几个查询结果行必须是一致的。4 i( l/ g$ e9 r, ^* z1 x
: `, ]: b l# S- k) m
12、说明:使用外连接0 L+ u2 ^& d0 g" H. B0 m( H& w
A、left outer join:
; ~& y# W, v v, s4 ^左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
- f9 E; r8 g. [& t9 P; p5 vSQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
1 T5 u* _) f8 g$ E- }8 c* K" yB:right outer join:
7 D9 z9 Q/ [% q# w: d+ J* l# e右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。2 y* A, G) a6 m+ ?: `: u
C:full outer join:6 ^; b$ \0 O0 o5 ^/ B
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
0 h) P. B% l+ Y! O& d6 z) M: A0 } S. M2 D( C( o
其次,大家来看一些不错的sql语句$ x; h. t0 {! @# s
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)! f# I- @! X& |$ g$ ?& M
法一:select * into b from a where 1<>1
; v- x! Z/ K ?+ m法二:select top 0 * into b from a
5 n3 ]% X& H' }$ k! R! Q
; y# J1 i8 n# P4 U2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
8 c# S6 l ?& t& Zinsert into b(a, b, c) select d,e,f from b;
- m& r3 i- a' G3 n6 w2 q& `3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)3 @1 H$ N$ E" k$ p) V
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件. J8 B% n8 t) |- c
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
2 K/ Q4 c$ o5 D8 f: ~6 Z& B1 Z; O' g. |8 |6 ]
4、说明:子查询(表名1:a 表名2:b) l8 {+ c& G0 [/ B
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)
+ |+ j0 k% D4 @# {
5 {2 j3 Q* ?4 K$ e9 ]2 O9 R J5、说明:显示文章、提交人和最后回复时间
2 M3 y+ a$ |( n& b3 a! Q0 {select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
" Y3 q: H, j: |1 {# @/ f' Q1 p
0 g, I; G6 j& x5 |5 ~5 \& x8 s/ d6、说明:外连接查询(表名1:a 表名2:b)
* N+ ?* R2 J! F- E Vselect a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c5 R) X; b5 ?+ e4 L- W# |
2 r9 v- {8 g( w6 @& Z) a" b1 J
7、说明:在线视图查询(表名1:a )
: G' S( _$ S7 n! f# `6 U/ E- Dselect * from (Select a,b,c FROM a) T where t.a > 1;
, d$ z3 J" H0 M1 R+ e
0 s( F9 f' Q ~ P8 @8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
' y5 P7 D: b! |" t5 b+ oselect * from table1 where time between time1 and time2
0 D4 w7 ?- d. y! \select a,b,c, from table1 where a not between 数值1 and 数值2
; f6 L e, J& ~" N7 e% L% c" \) z; D8 Y! h# N
9、说明:in 的使用方法
. F5 ]% a1 E/ |. c) {* }select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’): K: g r( I G$ M7 x) i1 F: t+ q
; G0 ?* K# S$ }( t4 J& H0 B
10、说明:两张关联表,删除主表中已经在副表中没有的信息8 u& e2 _' Z( X
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
2 n% Z1 u7 x: W* l( ~) w/ {6 A* q1 Q% G# K9 X- {3 w$ U4 o9 M1 |5 C. A
11、说明:四表联查问题:
1 I4 D8 w' X4 F, s+ v1 i# I% oselect * 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 .....
0 Z$ k& A# D) ]7 T5 t' H- o# ~( X$ w1 ]4 X
12、说明:日程安排提前五分钟提醒6 k$ Y. o# F- v' H! e& i$ f5 d
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
4 ^! D0 `2 T- a9 @" g, A: D& E. b7 y3 a; P& Z' v
13、说明:一条sql 语句搞定数据库分页6 Y1 z, u4 `4 a Y* y! G
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段
- n: g: t( s+ x* y
' o" h, p" C7 I* t$ W& t14、说明:前10条记录
" A3 R& L) P) q* O* R A: C; i+ nselect top 10 * from table1 where 范围 H; S2 z) h1 `8 \! `1 H: W+ P2 }
' P- D- F6 \4 X
15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)# q, X5 j7 C3 {9 ~5 K
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)* r0 O% I0 j/ y
$ y' k9 ~0 `2 t, `16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表) Y9 H. U/ D: [0 s" a
(select a from tableA ) except (select a from tableB) except (select a from tableC)
" z7 l! B2 z3 g4 m( ]/ j) E& ]3 p3 D) V
17、说明:随机取出10条数据
" Q. C3 u& G- dselect top 10 * from tablename order by newid()/ t5 p: Q4 } s5 |
* g- K0 O' N; L$ U3 V* s0 U# u18、说明:随机选择记录
& w S% f$ G. s pselect newid()( a/ |2 F2 e$ w4 n; G5 e& o! Y" S1 | s0 @
: s4 w/ i% g( u! c1 s4 I' ?! T$ H
19、说明:删除重复记录
4 d1 W( m# W( fDelete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
1 Q7 g( o) W5 }9 c. K% O, h2 d- G$ o1 `8 f' n( Y/ r5 n5 W! z
20、说明:列出数据库里所有的表名
3 C3 W* j* i& U, Jselect name from sysobjects where type='U': M- l& \* J/ {/ x. b7 f
0 \- ~: r! x$ g# u9 {) D. }21、说明:列出表里的所有的
% l2 ^% I: H* u6 _- [5 `$ bselect name from syscolumns where id=object_id('TableName')
6 \# }- L$ j0 {+ {, _- h
' I: n5 ^; W# s9 l$ r4 G+ ?' u, u22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
/ m. B% K& r# l& S: w b+ I3 Iselect 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
* Q) X6 O" J9 s* y+ |显示结果:
& d& ~8 O) H; j, ~, K* \( J. H7 h1 mtype vender pcs5 r) F3 h5 }, K! T
电脑 A 16 `5 Q8 v. t- Z# g, M/ I
电脑 A 1
+ X0 j2 H% \) | V) a光盘 B 2
, ?* D* C: a# G& b" v光盘 A 2+ h9 @, N7 {4 p% T n) J* x6 A1 F. f
手机 B 3
5 r! }8 P, U- E7 s8 ^手机 C 3$ x; h% q3 {& y! m' U4 `
, r5 }+ x0 m6 P# d1 m( {/ P
23、说明:初始化表table1* u5 P1 f5 T5 l
TRUNCATE TABLE table1" _& b# n6 C, A
( _9 u" G6 d5 g
24、说明:选择从10到15的记录
; }+ } W! H+ \4 x% Qselect top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc |
|