- 注册时间
- 2008-9-13
- 最后登录
- 1970-1-1
- 在线时间
- 0 小时
- 阅读权限
- 200
- 积分
- 0
- 帖子
- 24482
- 精华
- 4
- UID
- 9
  
|
SQL分类:& J: m. i' z0 T) L' H7 I
DDL―数据定义语言(Create,Alter,Drop,DECLARE)0 C8 u5 q8 p- x- o% u$ X9 }
DML―数据操纵语言(Select,Delete,Update,Insert)
D2 t# f" o( v | F+ i1 C- R; o! vDCL―数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)% k" G0 ]) D( Q5 D# J
3 I# B' Z/ X+ W; w
首先,简要介绍基础语句:
: M7 y9 f# ]) x8 e1、说明:创建数据库6 Q, t# c2 g+ `, i0 n; j8 |
Create DATABASE database-name
`4 _, l/ ^. \" \% X6 {6 h2、说明:删除数据库8 f* v$ [ \- `5 l9 R9 f2 H2 l
drop database dbname
7 Z8 }8 o9 \% P( h3 Y& d& R+ q3、说明:备份sql server- S) a+ Y# W D X/ b0 l
--- 创建 备份数据的 device; T1 o% O; ^& z3 K% P' U6 J; s% `
USE master
% l& P6 D5 L7 u: `% aEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
# L" {6 [; L3 c) a--- 开始 备份
7 l1 X! s9 x& R: R! w2 JBACKUP DATABASE pubs TO testBack
" | z3 q! ~) f3 \7 | A" h$ w4、说明:创建新表* w# k& }. { ]* Y, Z! l, B
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)* S7 Y, y- l) G1 H U1 E, W5 C
根据已有的表创建新表:! }0 c5 c2 @& k0 O6 I3 l V
A:create table tab_new like tab_old (使用旧表创建新表)
; \+ {0 X7 H J! NB:create table tab_new as select col1,col2… from tab_old definition only
5 G; |, r, m6 _3 D5、说明:删除新表
+ R! k4 W; f$ ^8 r; Ldrop table tabname
8 O" j9 I4 C: Z1 Q W5 f# @8 m6、说明:增加一个列# g' J* U0 _, ^/ {- ]" N& a
Alter table tabname add column col type( f- k& _+ F' X! P- B5 o
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
1 ?( |& P5 q9 h2 i4 ~9 Y) Y1 h6 A7、说明:添加主键: Alter table tabname add primary key(col)
4 ?0 W9 K! S/ H# D: {说明:删除主键: Alter table tabname drop primary key(col)' P6 B9 _" e+ R" H x7 L
8、说明:创建索引:create [unique] index idxname on tabname(col….)! V% b/ I% W# o, @
删除索引:drop index idxname
$ a) u8 G G5 o! |注:索引是不可更改的,想更改必须删除重新建。9 g6 D5 x! C4 h: ~ S+ S, J2 P
9、说明:创建视图:create view viewname as select statement
0 C$ ~9 h/ j; Q1 H" [1 V删除视图:drop view viewname
! V; C4 A$ r# ^ w$ A8 D* v6 u10、说明:几个简单的基本的sql语句
7 i! X* q6 \* d- K选择:select * from table1 where 范围
3 k5 B$ y' i5 q& [' ?; ~插入:insert into table1(field1,field2) values(value1,value2)
* [. `7 ~+ u, d" Q2 u删除:delete from table1 where 范围
8 m: k* m+ U6 r4 D更新:update table1 set field1=value1 where 范围0 _3 @3 n8 l4 u9 K
查找:select * from table1 where field1 like ’%value1__’
- t. j' i& B1 s; ^$ X排序:select * from table1 order by field1,field2 [desc]* j( A R+ I% d. U9 g
总数:select count * as totalcount from table1$ n, B* H9 Q& [7 |- m' i! L
求和:select sum(field1) as sumvalue from table1
K4 D* t' Q* R平均:select avg(field1) as avgvalue from table1; ?4 Y" Q0 a7 I- m" M( M
最大:select max(field1) as maxvalue from table1
/ d2 C: X. Y5 b6 d最小:select min(field1) as minvalue from table19 @ {1 f" _, P
11、说明:几个高级查询运算词
7 }( `4 g3 B* \) o/ BA: UNION 运算符2 _4 a) `9 Q/ n
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 K! I. x9 X I
B: EXCEPT 运算符6 }: }6 f5 Q3 Y3 N8 v0 v: ^5 v" [+ L
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
) p7 m: y4 W5 [C: INTERSECT 运算符
) ]% G; f0 o" B; R; @INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。. ~% ~ g3 t1 ]; m' Y& K) \
注:使用运算词的几个查询结果行必须是一致的。9 x4 v. q$ w+ ]% ~6 ?7 v* }
: x' ~- l% m( |3 Z. X: H$ Q1 }, o# X3 J. p0 t12、说明:使用外连接
0 B9 M2 ?9 n: b) s+ c+ `: ]. }A、left outer join:
# R8 b+ u' t/ w8 _1 i6 }, k左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。( ]. @. H6 _" b$ F
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
4 G. f' {) `5 T. z4 m3 hB:right outer join:& ~# }; |0 g% |+ p+ z; O' v; q
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。" \4 b8 a/ n+ C; G2 Y
C:full outer join:4 l& L2 g# K3 O% t1 ~' \+ S* H5 i/ |# v
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。6 A5 y G6 [: v- I( E$ T) i0 f
( c$ z( @& D0 {5 K1 V
其次,大家来看一些不错的sql语句
$ \0 z; @' r, Q( C3 o1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)6 R! W& L; b8 F4 f4 C0 ?! Q0 L
法一:select * into b from a where 1<>1/ O9 r; Q; u" q W! B) X# k
法二:select top 0 * into b from a _/ x! G6 o4 s! W: N' [* H7 d2 c# n
, q' i2 m( d' Y( i" v) a
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)! A( L9 L# {# t: R" D0 K2 V- w7 u
insert into b(a, b, c) select d,e,f from b;
8 i' B9 z& ?! M+ E. D3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)3 C6 N3 S- @/ K1 @4 G# H) R
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
& F1 x3 O0 t8 E* ?1 s* b例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..7 F6 h% h6 A& }* L2 T3 R+ h ^
. B2 ^3 I4 s7 \) N4、说明:子查询(表名1:a 表名2:b)! Q% g& k7 j3 V& [& V1 L
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)
4 j- D' Y7 \- [
7 G& c# K; ~% z& A5、说明:显示文章、提交人和最后回复时间
! J0 O; y" t; E9 F p# m* n- Zselect a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
5 L- j# F1 f; e' d" p& m6 B5 U+ F& W* M
6、说明:外连接查询(表名1:a 表名2:b)2 \7 H: V' \. f" M2 @
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
Q1 o3 e2 }% G
+ |7 w" \( N% D6 |7、说明:在线视图查询(表名1:a )
! |' Z( X* J @+ x1 @select * from (Select a,b,c FROM a) T where t.a > 1;& O9 O: g0 M' S* j
/ @/ f1 H; n: W% I1 c3 h- ?: b) i
8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
! f2 k' A2 ?8 {- \2 Dselect * from table1 where time between time1 and time2* d9 g+ m4 A" }2 W2 e4 V/ r: X
select a,b,c, from table1 where a not between 数值1 and 数值2, P8 c; L; m5 h5 s8 v
( ~4 n$ S9 P8 d" q* y. I& T
9、说明:in 的使用方法5 c. a6 f' l2 s% x
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
6 q; z. b' ^; \, S6 s4 P& ?- O6 k6 L6 f- w. Y' [; m
10、说明:两张关联表,删除主表中已经在副表中没有的信息
* _# L( K+ m! u9 S: Qdelete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )+ ]+ J! P/ h1 G! n; W; q
& f( N& r8 a6 ?6 y5 F$ S! x4 K11、说明:四表联查问题:
7 N5 ]; I# s( \2 \; T+ X. f( ]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 X( A) b+ K1 n, c; B& ]3 \& y! j- z; `* y; G9 |8 m
12、说明:日程安排提前五分钟提醒. w5 x% S4 M# M
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5/ n7 B X5 \- J! n/ x
3 s% ]3 v' s% b4 ^( M* b8 A3 W; X% y! X
13、说明:一条sql 语句搞定数据库分页
. y4 S0 ?# n0 J& Lselect top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段
* y0 G, t/ f8 N. {5 C- ^' g8 j$ j5 q
14、说明:前10条记录8 C+ Z4 A- Y- `. R' w$ o
select top 10 * from table1 where 范围5 l0 g7 i: {: A1 z% e1 Q
0 @$ q! f3 A, ~/ j* c9 h" U15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
. t& e# \4 O8 g: R: S9 `( m: hselect a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)7 D- |# D& V- s5 v* A/ K
- @6 z; u3 n C: _; z) g; Q" z
16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表/ ]; z. z7 R; Z% s/ ?4 }
(select a from tableA ) except (select a from tableB) except (select a from tableC)
4 _: n1 ]5 ~% Z
& @& v* U9 b8 R% a6 b17、说明:随机取出10条数据- B) v0 d+ ~( m9 M6 n( p p% ~ U2 t
select top 10 * from tablename order by newid()! O+ R0 {& M$ a2 k- H# k
3 S; @( i. z i3 K# N: q# l4 k* v18、说明:随机选择记录( c/ E( Z& x2 S' C. S1 S
select newid()
) ?, b% e5 C0 [6 T, c7 e6 u" G) z( Q2 ?" ^* S1 i
19、说明:删除重复记录
; L. L0 X) w/ J" n/ F9 z% ODelete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
" V* f2 q9 Q& q1 I3 V1 u9 O/ @" C' l
20、说明:列出数据库里所有的表名
% ?, h) D' V9 O7 xselect name from sysobjects where type='U'& L G4 a( U$ S8 b- M* D: p/ K: [
% H! y4 B" C) V) G! ^2 w
21、说明:列出表里的所有的
3 N- y9 P: m! t6 k8 C# J% Nselect name from syscolumns where id=object_id('TableName')/ U% h0 O# A7 A* e7 M) l
7 X( U$ z7 n' m3 K) g; D" u x
22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
6 G- r* u K2 N& W$ c1 Q- Z6 H: {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
$ Y. |; ]1 Q2 v显示结果:
$ u2 u7 J( y* R& ]2 Dtype vender pcs
8 E3 k; ]/ B: {. t- G; H% y; m4 |电脑 A 1
% j8 P; h4 C1 j! h' X电脑 A 1
) o, O- f% F$ [2 k9 ?光盘 B 2" o; c) M, K9 s
光盘 A 2
. p( O5 H2 w! y8 u手机 B 3: P# l+ Y4 Y. t$ W1 ^ a
手机 C 3! }5 ?( l: R: ~7 k
6 c1 @' W) |. R" A. u: T23、说明:初始化表table1, S# R0 K2 a4 o( k6 v$ g! a
TRUNCATE TABLE table1" z- [ R7 `/ [
1 x' J0 D+ k2 K6 x% J
24、说明:选择从10到15的记录9 S9 d; ?: f2 t6 I
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc |
|