删除主键: Alter table tabname drop primary key(col)
上一篇 / 下一篇 2011-06-17 13:45:57 / 个人分类:Oracle
下列语句部分是Mssql语句,不可以在access中使用。51Testing软件测试网uS6R-agL
{b*f8f$GX0SQL分类:
7}$y3zMdaf0DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
-^s*p8ck1wT0DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)51Testing软件测试网*qJ0d8L-i+q7G[
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
Y_JLd6r K%^051Testing软件测试网 t(vfIZ~?2l
首先,简要介绍基础语句:51Testing软件测试网5w Z.nTj oT
1、说明:创建数据库
2tk6w`9n8_w;[0CREATE DATABASE database-name51Testing软件测试网O1u+FnK
2、说明:删除数据库51Testing软件测试网 C:fgV ~A5S
drop database dbname51Testing软件测试网;GB(R6mp\/^M*o2v
3、说明:备份sql server51Testing软件测试网][C?4Yp
---创建备份数据的device51Testing软件测试网7\r8{1w m:Z
USE master51Testing软件测试网,X3K Vd/AA
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'51Testing软件测试网@ r/O1T0I'XHf%L
---开始备份51Testing软件测试网_(Uw](}
BACKUP DATABASE pubs TO testBack
| yYe in;r T*[2?l/R04、说明:创建新表51Testing软件测试网,tt?$Ce2Y0E6H
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)51Testing软件测试网^m C,D^ Pq\
根据已有的表创建新表:
_yY}:g};P g8c0A:create table tab_new like tab_old (使用旧表创建新表)
#C2`h5Y!p{0B:create table tab_new as select col1,col2… from tab_old definition only
5sB ["\IT'{M|x05、说明:删除新表drop table tabname
h+b,Q:F.E!o06、说明:增加一个列
J0TW']?c%O Vn'w0Alter table tabname add column col type51Testing软件测试网C$|dm)q`!F-f
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。51Testing软件测试网:c'uTs+{8{&|
7、说明:添加主键:Alter table tabname add primary key(col)51Testing软件测试网&lBBpDa
说明:删除主键:Alter table tabname drop primary key(col)51Testing软件测试网 u,i3A JnJ~6Af
8、说明:创建索引:create [unique] index idxname on tabname(col….)
] NQXJ)t N0删除索引:drop index idxname51Testing软件测试网*t3_7Q'L W gqH
注:索引是不可更改的,想更改必须删除重新建。
@#N;hGG$] [&c;R09、说明:创建视图:create view viewname as select statement
DE TS~pr1Jq7V0删除视图:drop view viewname
{aC1~0K{K010、说明:几个简单的基本的sql语句51Testing软件测试网{9v&[};h-E~
选择:select * from table1 where范围51Testing软件测试网Z1M&mSc0Snr
插入:insert into table1(field1,field2) values(value1,value2)51Testing软件测试网G&gr(t%E9Y$X
删除:delete from table1 where范围
Exp Eq0更新:update table1 set field1=value1 where范围51Testing软件测试网 dZC9h%a
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!51Testing软件测试网c/\z6{fWn
排序:select * from table1 order by field1,field2 [desc]
T;ZVT6ws@+jJF0总数:select count * as totalcount from table1
A&Ae Qlx/Q9}/TG0求和:select sum(field1) as sumvalue from table1
^u)_/ZK"wSN{_0平均:select avg(field1) as avgvalue from table151Testing软件测试网$n9r5Q _$N
最大:select max(field1) as maxvalue from table1
Yo(a p#UH!gG0最小:select min(field1) as minvalue from table1
!L*L*J,D{j011、说明:几个高级查询运算词
(M3HD|(C2lR X0A:UNION运算符51Testing软件测试网bb R`-W*r
UNION运算符通过组合其他两个结果表(例如TABLE1和TABLE2)并消去表中任何重复行而派生出一个结果表。当ALL随UNION一起使用时(即UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自TABLE1就是来自TABLE2。51Testing软件测试网 ]B/Ha9z o:Y?
B:EXCEPT运算符51Testing软件测试网5~RIS:l[(s
EXCEPT运算符通过包括所有在TABLE1中但不在TABLE2中的行并消除所有重复行而派生出一个结果表。当ALL随EXCEPT一起使用时(EXCEPT ALL),不消除重复行。
h9G,\\A|/fB0C:INTERSECT运算符
nq,wYK$vY%F0INTERSECT运算符通过只包括TABLE1和TABLE2中都有的行并消除所有重复行而派生出一个结果表。当ALL随INTERSECT一起使用时(INTERSECT ALL),不消除重复行。51Testing软件测试网4}Y |n$Bv"V2_:NG
注:使用运算词的几个查询结果行必须是一致的。
.I6j3WK"[012、说明:使用外连接
7OQj M3{8mvN1GW0A、left outer join:
/Fj(g7SY8di;U@0左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。51Testing软件测试网 vd_#g#@&\6T1P,q[
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c51Testing软件测试网"bw3KBd0x
B:right outer join:
a:w;s+p IE0右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
Tk h&h1`{xHb k+X0C:full outer join:51Testing软件测试网h*i'wI7LZPM&i#l&L
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
wbs7|1nFZ0
2| J ?"C9W0KN9L0其次,大家来看一些不错的sql语句
D;b6}%faI]Z D/u01、说明:复制表(只复制结构,源表名:a新表名:b) (Access可用)
V"zL VZZ1{i0法一:select * into b from a where 1<>151Testing软件测试网r0F4^ p/D?
法二:select top 0 * into b from a
&qc8M$s"a?3a0
)iNWD[J,mZ7y)I02、说明:拷贝表(拷贝数据,源表名:a目标表名:b) (Access可用)
r LD"}#]0insert into b(a, b, c) select d,e,f from b;
^MI.uv A/U7t,b4X051Testing软件测试网 ]v|*P q[ J7Mq w7u#al
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
'};rOw2hM7ER0insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where条件51Testing软件测试网re~Z!E/[Z
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
(S c&NA#rr2q,p+X0
^},JtoE*y0} F04、说明:子查询(表名1:a表名2:b)
D^.Li0oi F6x0select a,b,c from a where a IN (select d from b )或者: select a,b,c from a where a IN (1,2,3)51Testing软件测试网UK0Ds`JM
51Testing软件测试网R.mV ]6L'W$i
5、说明:显示文章、提交人和最后回复时间51Testing软件测试网(};TVUu,}&W5^
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
n9s,D*Z0rl051Testing软件测试网ibqZ3z
6、说明:外连接查询(表名1:a表名2:b)
|&Ny*yF^6eW)z*s0select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c51Testing软件测试网,n[py*zv
51Testing软件测试网`#P D,Q$Zc
7、说明:在线视图查询(表名1:a )
z yz-m6Tw0select * from (SELECT a,b,c FROM a) T where t.a > 1;51Testing软件测试网@&pg jsq
%]^Q&nj9v1k08、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括51Testing软件测试网B.p5GLeQ*v7v)K*tKT
select * from table1 where time between time1 and time251Testing软件测试网5Mm;MeRl,Trv
select a,b,c, from table1 where a not between数值1 and数值251Testing软件测试网zE$uO7~ PW
51Testing软件测试网uxlV1i L k/x.@
9、说明:in的使用方法
#{k?S F6c0select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)51Testing软件测试网,RD?X/T+i
51Testing软件测试网^i\$d:Uu&WU
10、说明:两张关联表,删除主表中已经在副表中没有的信息
:Y"L v1V0WA0d|0delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
D3T$e'UT`1f0
+IKx'Bbb"c/k011、说明:四表联查问题:
%LpHcI {t0select * 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 .....
2I'zh6Kv)Z q;Gl"Eli051Testing软件测试网/vojl _H:C
12、说明:日程安排提前五分钟提醒51Testing软件测试网%E1[3_ _tV
SQL: select * from日程安排where datediff('minute',f开始时间,getdate())>5
1l~:z1?,R SS051Testing软件测试网~5HC5B b.~5R
13、说明:一条sql语句搞定数据库分页51Testing软件测试网*t%}8hn.m P!g+h k!D
select top 10 b.* from (select top 20主键字段,排序字段from表名order by排序字段desc) a,表名b where b.主键字段= a.主键字段order by a.排序字段51Testing软件测试网A?z-f9b7O,Ks]Z
51Testing软件测试网aD'dD0wJ
14、说明:前10条记录
`.CI3N2C0select top 10 * form. table1 where范围51Testing软件测试网c}koI
On.[1K9LSM*`g015、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)51Testing软件测试网9}1L4A ?,c`9XW F
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
"i+H#mS8QS(`i051Testing软件测试网OD.d p;f]
16、说明:包括所有在TableA中但不在TableB和TableC中的行并消除所有重复行而派生出一个结果表
,Y mZ8LB-Lq0(select a from tableA ) except (select a from tableB) except (select a from tableC)
}lHm bV7u;Z0
6IkC v5P"@ | C017、说明:随机取出10条数据
G:c$UdTdf0select top 10 * from tablename order by newid()51Testing软件测试网W%tBT@~ @%K
51Testing软件测试网IG'rjvb0A4{
18、说明:随机选择记录
3E*v` A'A!\j0select newid()51Testing软件测试网_wr3Y9c(tc:h
51Testing软件测试网?#d)h.r!v(ns"F8Zvp
19、说明:删除重复记录51Testing软件测试网kyi+q*wc@"Fnc
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)51Testing软件测试网lm(OPVg:XA
51Testing软件测试网j.y.vgT _ aJy:f}7q
20、说明:列出数据库里所有的表名
.NJ8H^d In0select name from sysobjects where type='U'51Testing软件测试网)f6@ mER$Z$Eg
51Testing软件测试网,cx"s$O7N L%~hTc
21、说明:列出表里的所有的
7_{zL3GTGptY W#i0select name from syscolumns where id=object_id('TableName')
8K9[I"Y&F\051Testing软件测试网lk_7y5ss)G;c`4R
22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select中的case。51Testing软件测试网/M-nOtjo
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
;B-H,}WH0显示结果:51Testing软件测试网}rY!jX*cL9jCi
type vender pcs51Testing软件测试网0w9V,n;K$X%_
电脑A 151Testing软件测试网%M } c.IE0ko
电脑A 1
0K"iy9M:o'G\g0光盘B 2
x$M(O]x(r$[0光盘A 2
L h{ Q,EO0手机B 351Testing软件测试网 GX5T-?\0V3n8mt
手机C 3
?YKy#J{ p051Testing软件测试网^MG"\\
23、说明:初始化表table151Testing软件测试网KtOVT#j
TRUNCATE TABLE table1
'J#\I%|i.l~0DP0
"U f"Osnfe/m024、说明:选择从10到15的记录51Testing软件测试网G_:Tt.mVj5N3~|
select top 5 * from (select top 15 * from table order by id asc) table_别名order by id desc51Testing软件测试网J+W w)[}"w(z
51Testing软件测试网;d*l6~Nn F(Z2h
随机选择数据库记录的方法(使用Randomize函数,通过SQL语句实现)51Testing软件测试网vq?4~L#W!}?
对存储在数据库中的数据来说,随机数特性能给出上面的效果,但它们可能太慢了些。你不能要求ASP“找个随机数”然后打印出来。实际上常见的解决方案是建立如下所示的循环:
:I h| { g%Kpl$v0Randomize
;X#uf Rz1V0RNumber = Int(Rnd*499) +1
2f1I3MW @&| B?m%m*T0 51Testing软件测试网^^ o'p+T'G]
While Not objRec.EOF51Testing软件测试网1{k'Py7w
If objRec("ID") = RNumber THEN
#H!nk k6jYG~L!A C0...这里是执行脚本...
+h1Jn/Q/^0end if
5i$f%G-i,]R~ O JA$Dy0objRec.MoveNext51Testing软件测试网 PZ&@n/W#`#f?
Wend
~)rn/@2\wb:^%c.R0 51Testing软件测试网-_b4w5E(d7P \:{8F
这很容易理解。首先,你取出1到500范围之内的一个随机数(假设500就是数据库内记录的总数)。然后,你遍历每一记录来测试ID的值、检查其是否匹配RNumber。满足条件的话就执行由THEN关键字开始的那一块代码。假如你的RNumber等于495,那么要循环一遍数据库花的时间可就长了。虽然500这个数字看起来大了些,但相比更为稳固的企业解决方案这还是个小型数据库了,后者通常在一个数据库内就包含了成千上万条记录。这时候不就死定了?
&{/P(z4jE0R:xdTO0 采用SQL,你就可以很快地找出准确的记录并且打开一个只包含该记录的recordset,如下所示:51Testing软件测试网 @4ZeyE R
Randomize
8y1kA{gN0J$t0RNumber = Int(Rnd*499) + 151Testing软件测试网!eYWZ"^Xm}fc
51Testing软件测试网.N0C7w c1K)e
SQL = "SELECT * FROM Customers WHERE ID = " & RNumber51Testing软件测试网:U:C+W5J~%d
51Testing软件测试网$J$?&XI%dT
set bjRec = ObjConn.Execute(SQL)
#O6Bt,FKN.aQ)s5ye0Response.WriteRNumber & " = " & objRec("ID") & " " & objRec("c_email")51Testing软件测试网-D4k*z _._.L8O o]
V+y%_1c#L0 不必写出RNumber和ID,你只需要检查匹配情况即可。只要你对以上代码的工作满意,你自可按需操作“随机”记录。Recordset没有包含其他内容,因此你很快就能找到你需要的记录这样就大大降低了处理时间。51Testing软件测试网a*f*H(`Kqo7v\
再谈随机数51Testing软件测试网0Wx-bU(I
现在你下定决心要榨干Random函数的最后一滴油,那么你可能会一次取出多条随机记录或者想采用一定随机范围内的记录。把上面的标准Random示例扩展一下就可以用SQL应对上面两种情况了。51Testing软件测试网7N$g E8HQg'_ h
为了取出几条随机选择的记录并存放在同一recordset内,你可以存储三个随机数,然后查询数据库获得匹配这些数字的记录:51Testing软件测试网UaN$x!A%L:g
SQL = "SELECT * FROM Customers WHERE ID = " & RNumber & " OR ID = " & RNumber2 & " OR ID = " & RNumber3
6U-u$A L.BO1OH]0
c,q3P6|!?D(p"f/E0 假如你想选出10条记录(也许是每次页面装载时的10条链接的列表),你可以用BETWEEN或者数学等式选出第一条记录和适当数量的递增记录。这一操作可以通过好几种方式来完成,但是SELECT语句只显示一种可能(这里的ID是自动生成的号码):
~xJ A9Y2J0SQL = "SELECT * FROM Customers WHERE ID BETWEEN " & RNumber & " AND " & RNumber & "+ 9"
#m#SkuA"U_051Testing软件测试网W,R%K-E5A;V{'qi%fc
注意:以上代码的执行目的不是检查数据库内是否有9条并发记录。51Testing软件测试网G%a:X+Ei0N
$qe v4^8g8j u0
d b*[9R"SDr1jb0随机读取若干条记录,测试过
N2dJ%Bv o| |VL0Access语法:SELECT top 10 * From表名ORDER BY Rnd(id)51Testing软件测试网R at)\;~p:]6d
Sql server:select top n * from表名order by newid()51Testing软件测试网5z C@1^/kL1wSE
mysql elect * From表名Order By rand() Limit n
&D/?7A4{OF0Access左连接语法(最近开发要用左连接,Access帮助什么都没有,网上没有Access的SQL说明,只有自己测试,现在记下以备后查)
%A oo'OBO0语法elect table1.fd1,table1,fd2,table2.fd2 From table1 left join table2 on table1.fd1,table2.fd1 where ...51Testing软件测试网F2[y3R0N:r,x k
使用SQL语句用...代替过长的字符串显示
4tnZzxf?3E-h1q0语法:
(n$BCB|J$RN&LI0SQL数据库:select case when len(field)>10 then left(field,10)+'...' else field end as news_name,news_id from tablename51Testing软件测试网{$Du j*YL
Access数据库:SELECT iif(len(field)>2,left(field,2)+'...',field) FROM tablename;
ig M&yH,h8Cq4W0
bKBW]1o0Conn.Execute说明
Ix5B+s/[;[0Execute方法51Testing软件测试网+Q"a*An;R9FG
该方法用于执行SQL语句。根据SQL语句执行后是否返回记录集,该方法的使用格式分为以下两种:
t-[/cs}Q4I0 1.执行SQL查询语句时,将返回查询得到的记录集。用法为:51Testing软件测试网(Q9^x)Hg#w
Set对象变量名=连接对象.Execute("SQL查询语言")
!Y-j/z3{V&X1xT)y0 Execute方法调用后,会自动创建记录集对象,并将查询结果存储在该记录对象中,通过Set方法,将记录集赋给指定的对象保存,以后对象变量就代表了该记录集对象。51Testing软件测试网Q;ZEmN Ml
8\?;U8si'ic0 2.执行SQL的操作性语言时,没有记录集的返回。此时用法为:
fy;@3@_Y0P0 连接对象.Execute "SQL操作性语句" [, RecordAffected][, Option]51Testing软件测试网'v{+tMfd
·RecordAffected为可选项,此出可放置一个变量,SQL语句执行后,所生效的记录数会自动保存到该变量中。通过访问该变量,就可知道SQL语句队多少条记录进行了操作。
[5R:p:omU0 ·Option可选项,该参数的取值通常为adCMDText,它用于告诉ADO,应该将Execute方法之后的第一个字符解释为命令文本。通过指定该参数,可使执行更高效。51Testing软件测试网u8e6S"]"? YDQ H9Kf
R&N*T3P+W2\"]0·BeginTrans、RollbackTrans、CommitTrans方法
7B0g;q)^ vbdIZY0 这三个方法是连接对象提供的用于事务处理的方法。BeginTrans用于开始一个事物;RollbackTrans用于回滚事务;CommitTrans用于提交所有的事务处理结果,即确认事务的处理。
v?7p1jI-MY`0 事务处理可以将一组操作视为一个整体,只有全部语句都成功执行后,事务处理才算成功;若其中有一个语句执行失败,则整个处理就算失败,并恢复到处里前的状态。51Testing软件测试网 ?6m}*Dg:ug?
BeginTrans和CommitTrans用于标记事务的开始和结束,在这两个之间的语句,就是作为事务处理的语句。判断事务处理是否成功,可通过连接对象的Error集合来实现,若Error集合的成员个数不为0,则说明有错误发生,事务处理失败。Error集合中的每一个Error对象,代表一个错误信息。
{b*f8f$GX0SQL分类:
7}$y3zMdaf0DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
-^s*p8ck1wT0DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)51Testing软件测试网*qJ0d8L-i+q7G[
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
Y_JLd6r K%^051Testing软件测试网 t(vfIZ~?2l
首先,简要介绍基础语句:51Testing软件测试网5w Z.nTj oT
1、说明:创建数据库
2tk6w`9n8_w;[0CREATE DATABASE database-name51Testing软件测试网O1u+FnK
2、说明:删除数据库51Testing软件测试网 C:fgV ~A5S
drop database dbname51Testing软件测试网;GB(R6mp\/^M*o2v
3、说明:备份sql server51Testing软件测试网][C?4Yp
---创建备份数据的device51Testing软件测试网7\r8{1w m:Z
USE master51Testing软件测试网,X3K Vd/AA
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'51Testing软件测试网@ r/O1T0I'XHf%L
---开始备份51Testing软件测试网_(Uw](}
BACKUP DATABASE pubs TO testBack
| yYe in;r T*[2?l/R04、说明:创建新表51Testing软件测试网,tt?$Ce2Y0E6H
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)51Testing软件测试网^m C,D^ Pq\
根据已有的表创建新表:
_yY}:g};P g8c0A:create table tab_new like tab_old (使用旧表创建新表)
#C2`h5Y!p{0B:create table tab_new as select col1,col2… from tab_old definition only
5sB ["\IT'{M|x05、说明:删除新表drop table tabname
h+b,Q:F.E!o06、说明:增加一个列
J0TW']?c%O Vn'w0Alter table tabname add column col type51Testing软件测试网C$|dm)q`!F-f
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。51Testing软件测试网:c'uTs+{8{&|
7、说明:添加主键:Alter table tabname add primary key(col)51Testing软件测试网&lBBpDa
说明:删除主键:Alter table tabname drop primary key(col)51Testing软件测试网 u,i3A JnJ~6Af
8、说明:创建索引:create [unique] index idxname on tabname(col….)
] NQXJ)t N0删除索引:drop index idxname51Testing软件测试网*t3_7Q'L W gqH
注:索引是不可更改的,想更改必须删除重新建。
@#N;hGG$] [&c;R09、说明:创建视图:create view viewname as select statement
DE TS~pr1Jq7V0删除视图:drop view viewname
{aC1~0K{K010、说明:几个简单的基本的sql语句51Testing软件测试网{9v&[};h-E~
选择:select * from table1 where范围51Testing软件测试网Z1M&mSc0Snr
插入:insert into table1(field1,field2) values(value1,value2)51Testing软件测试网G&gr(t%E9Y$X
删除:delete from table1 where范围
Exp Eq0更新:update table1 set field1=value1 where范围51Testing软件测试网 dZC9h%a
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!51Testing软件测试网c/\z6{fWn
排序:select * from table1 order by field1,field2 [desc]
T;ZVT6ws@+jJF0总数:select count * as totalcount from table1
A&Ae Qlx/Q9}/TG0求和:select sum(field1) as sumvalue from table1
^u)_/ZK"wSN{_0平均:select avg(field1) as avgvalue from table151Testing软件测试网$n9r5Q _$N
最大:select max(field1) as maxvalue from table1
Yo(a p#UH!gG0最小:select min(field1) as minvalue from table1
!L*L*J,D{j011、说明:几个高级查询运算词
(M3HD|(C2lR X0A:UNION运算符51Testing软件测试网bb R`-W*r
UNION运算符通过组合其他两个结果表(例如TABLE1和TABLE2)并消去表中任何重复行而派生出一个结果表。当ALL随UNION一起使用时(即UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自TABLE1就是来自TABLE2。51Testing软件测试网 ]B/Ha9z o:Y?
B:EXCEPT运算符51Testing软件测试网5~RIS:l[(s
EXCEPT运算符通过包括所有在TABLE1中但不在TABLE2中的行并消除所有重复行而派生出一个结果表。当ALL随EXCEPT一起使用时(EXCEPT ALL),不消除重复行。
h9G,\\A|/fB0C:INTERSECT运算符
nq,wYK$vY%F0INTERSECT运算符通过只包括TABLE1和TABLE2中都有的行并消除所有重复行而派生出一个结果表。当ALL随INTERSECT一起使用时(INTERSECT ALL),不消除重复行。51Testing软件测试网4}Y |n$Bv"V2_:NG
注:使用运算词的几个查询结果行必须是一致的。
.I6j3WK"[012、说明:使用外连接
7OQj M3{8mvN1GW0A、left outer join:
/Fj(g7SY8di;U@0左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。51Testing软件测试网 vd_#g#@&\6T1P,q[
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c51Testing软件测试网"bw3KBd0x
B:right outer join:
a:w;s+p IE0右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
Tk h&h1`{xHb k+X0C:full outer join:51Testing软件测试网h*i'wI7LZPM&i#l&L
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
wbs7|1nFZ0
2| J ?"C9W0KN9L0其次,大家来看一些不错的sql语句
D;b6}%faI]Z D/u01、说明:复制表(只复制结构,源表名:a新表名:b) (Access可用)
V"zL VZZ1{i0法一:select * into b from a where 1<>151Testing软件测试网r0F4^ p/D?
法二:select top 0 * into b from a
&qc8M$s"a?3a0
)iNWD[J,mZ7y)I02、说明:拷贝表(拷贝数据,源表名:a目标表名:b) (Access可用)
r LD"}#]0insert into b(a, b, c) select d,e,f from b;
^MI.uv A/U7t,b4X051Testing软件测试网 ]v|*P q[ J7Mq w7u#al
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
'};rOw2hM7ER0insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where条件51Testing软件测试网re~Z!E/[Z
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
(S c&NA#rr2q,p+X0
^},JtoE*y0} F04、说明:子查询(表名1:a表名2:b)
D^.Li0oi F6x0select a,b,c from a where a IN (select d from b )或者: select a,b,c from a where a IN (1,2,3)51Testing软件测试网UK0Ds`JM
51Testing软件测试网R.mV ]6L'W$i
5、说明:显示文章、提交人和最后回复时间51Testing软件测试网(};TVUu,}&W5^
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
n9s,D*Z0rl051Testing软件测试网ibqZ3z
6、说明:外连接查询(表名1:a表名2:b)
|&Ny*yF^6eW)z*s0select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c51Testing软件测试网,n[py*zv
51Testing软件测试网`#P D,Q$Zc
7、说明:在线视图查询(表名1:a )
z yz-m6Tw0select * from (SELECT a,b,c FROM a) T where t.a > 1;51Testing软件测试网@&pg jsq
%]^Q&nj9v1k08、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括51Testing软件测试网B.p5GLeQ*v7v)K*tKT
select * from table1 where time between time1 and time251Testing软件测试网5Mm;MeRl,Trv
select a,b,c, from table1 where a not between数值1 and数值251Testing软件测试网zE$uO7~ PW
51Testing软件测试网uxlV1i L k/x.@
9、说明:in的使用方法
#{k?S F6c0select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)51Testing软件测试网,RD?X/T+i
51Testing软件测试网^i\$d:Uu&WU
10、说明:两张关联表,删除主表中已经在副表中没有的信息
:Y"L v1V0WA0d|0delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
D3T$e'UT`1f0
+IKx'Bbb"c/k011、说明:四表联查问题:
%LpHcI {t0select * 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 .....
2I'zh6Kv)Z q;Gl"Eli051Testing软件测试网/vojl _H:C
12、说明:日程安排提前五分钟提醒51Testing软件测试网%E1[3_ _tV
SQL: select * from日程安排where datediff('minute',f开始时间,getdate())>5
1l~:z1?,R SS051Testing软件测试网~5HC5B b.~5R
13、说明:一条sql语句搞定数据库分页51Testing软件测试网*t%}8hn.m P!g+h k!D
select top 10 b.* from (select top 20主键字段,排序字段from表名order by排序字段desc) a,表名b where b.主键字段= a.主键字段order by a.排序字段51Testing软件测试网A?z-f9b7O,Ks]Z
51Testing软件测试网aD'dD0wJ
14、说明:前10条记录
`.CI3N2C0select top 10 * form. table1 where范围51Testing软件测试网c}koI
On.[1K9LSM*`g015、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)51Testing软件测试网9}1L4A ?,c`9XW F
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
"i+H#mS8QS(`i051Testing软件测试网OD.d p;f]
16、说明:包括所有在TableA中但不在TableB和TableC中的行并消除所有重复行而派生出一个结果表
,Y mZ8LB-Lq0(select a from tableA ) except (select a from tableB) except (select a from tableC)
}lHm bV7u;Z0
6IkC v5P"@ | C017、说明:随机取出10条数据
G:c$UdTdf0select top 10 * from tablename order by newid()51Testing软件测试网W%tBT@~ @%K
51Testing软件测试网IG'rjvb0A4{
18、说明:随机选择记录
3E*v` A'A!\j0select newid()51Testing软件测试网_wr3Y9c(tc:h
51Testing软件测试网?#d)h.r!v(ns"F8Zvp
19、说明:删除重复记录51Testing软件测试网kyi+q*wc@"Fnc
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)51Testing软件测试网lm(OPVg:XA
51Testing软件测试网j.y.vgT _ aJy:f}7q
20、说明:列出数据库里所有的表名
.NJ8H^d In0select name from sysobjects where type='U'51Testing软件测试网)f6@ mER$Z$Eg
51Testing软件测试网,cx"s$O7N L%~hTc
21、说明:列出表里的所有的
7_{zL3GTGptY W#i0select name from syscolumns where id=object_id('TableName')
8K9[I"Y&F\051Testing软件测试网lk_7y5ss)G;c`4R
22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select中的case。51Testing软件测试网/M-nOtjo
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
;B-H,}WH0显示结果:51Testing软件测试网}rY!jX*cL9jCi
type vender pcs51Testing软件测试网0w9V,n;K$X%_
电脑A 151Testing软件测试网%M } c.IE0ko
电脑A 1
0K"iy9M:o'G\g0光盘B 2
x$M(O]x(r$[0光盘A 2
L h{ Q,EO0手机B 351Testing软件测试网 GX5T-?\0V3n8mt
手机C 3
?YKy#J{ p051Testing软件测试网^MG"\\
23、说明:初始化表table151Testing软件测试网KtOVT#j
TRUNCATE TABLE table1
'J#\I%|i.l~0DP0
"U f"Osnfe/m024、说明:选择从10到15的记录51Testing软件测试网G_:Tt.mVj5N3~|
select top 5 * from (select top 15 * from table order by id asc) table_别名order by id desc51Testing软件测试网J+W w)[}"w(z
51Testing软件测试网;d*l6~Nn F(Z2h
随机选择数据库记录的方法(使用Randomize函数,通过SQL语句实现)51Testing软件测试网vq?4~L#W!}?
对存储在数据库中的数据来说,随机数特性能给出上面的效果,但它们可能太慢了些。你不能要求ASP“找个随机数”然后打印出来。实际上常见的解决方案是建立如下所示的循环:
:I h| { g%Kpl$v0Randomize
;X#uf Rz1V0RNumber = Int(Rnd*499) +1
2f1I3MW @&| B?m%m*T0 51Testing软件测试网^^ o'p+T'G]
While Not objRec.EOF51Testing软件测试网1{k'Py7w
If objRec("ID") = RNumber THEN
#H!nk k6jYG~L!A C0...这里是执行脚本...
+h1Jn/Q/^0end if
5i$f%G-i,]R~ O JA$Dy0objRec.MoveNext51Testing软件测试网 PZ&@n/W#`#f?
Wend
~)rn/@2\wb:^%c.R0 51Testing软件测试网-_b4w5E(d7P \:{8F
这很容易理解。首先,你取出1到500范围之内的一个随机数(假设500就是数据库内记录的总数)。然后,你遍历每一记录来测试ID的值、检查其是否匹配RNumber。满足条件的话就执行由THEN关键字开始的那一块代码。假如你的RNumber等于495,那么要循环一遍数据库花的时间可就长了。虽然500这个数字看起来大了些,但相比更为稳固的企业解决方案这还是个小型数据库了,后者通常在一个数据库内就包含了成千上万条记录。这时候不就死定了?
&{/P(z4jE0R:xdTO0 采用SQL,你就可以很快地找出准确的记录并且打开一个只包含该记录的recordset,如下所示:51Testing软件测试网 @4ZeyE R
Randomize
8y1kA{gN0J$t0RNumber = Int(Rnd*499) + 151Testing软件测试网!eYWZ"^Xm}fc
51Testing软件测试网.N0C7w c1K)e
SQL = "SELECT * FROM Customers WHERE ID = " & RNumber51Testing软件测试网:U:C+W5J~%d
51Testing软件测试网$J$?&XI%dT
set bjRec = ObjConn.Execute(SQL)
#O6Bt,FKN.aQ)s5ye0Response.WriteRNumber & " = " & objRec("ID") & " " & objRec("c_email")51Testing软件测试网-D4k*z _._.L8O o]
V+y%_1c#L0 不必写出RNumber和ID,你只需要检查匹配情况即可。只要你对以上代码的工作满意,你自可按需操作“随机”记录。Recordset没有包含其他内容,因此你很快就能找到你需要的记录这样就大大降低了处理时间。51Testing软件测试网a*f*H(`Kqo7v\
再谈随机数51Testing软件测试网0Wx-bU(I
现在你下定决心要榨干Random函数的最后一滴油,那么你可能会一次取出多条随机记录或者想采用一定随机范围内的记录。把上面的标准Random示例扩展一下就可以用SQL应对上面两种情况了。51Testing软件测试网7N$g E8HQg'_ h
为了取出几条随机选择的记录并存放在同一recordset内,你可以存储三个随机数,然后查询数据库获得匹配这些数字的记录:51Testing软件测试网UaN$x!A%L:g
SQL = "SELECT * FROM Customers WHERE ID = " & RNumber & " OR ID = " & RNumber2 & " OR ID = " & RNumber3
6U-u$A L.BO1OH]0
c,q3P6|!?D(p"f/E0 假如你想选出10条记录(也许是每次页面装载时的10条链接的列表),你可以用BETWEEN或者数学等式选出第一条记录和适当数量的递增记录。这一操作可以通过好几种方式来完成,但是SELECT语句只显示一种可能(这里的ID是自动生成的号码):
~xJ A9Y2J0SQL = "SELECT * FROM Customers WHERE ID BETWEEN " & RNumber & " AND " & RNumber & "+ 9"
#m#SkuA"U_051Testing软件测试网W,R%K-E5A;V{'qi%fc
注意:以上代码的执行目的不是检查数据库内是否有9条并发记录。51Testing软件测试网G%a:X+Ei0N
$qe v4^8g8j u0
d b*[9R"SDr1jb0随机读取若干条记录,测试过
N2dJ%Bv o| |VL0Access语法:SELECT top 10 * From表名ORDER BY Rnd(id)51Testing软件测试网R at)\;~p:]6d
Sql server:select top n * from表名order by newid()51Testing软件测试网5z C@1^/kL1wSE
mysql elect * From表名Order By rand() Limit n
&D/?7A4{OF0Access左连接语法(最近开发要用左连接,Access帮助什么都没有,网上没有Access的SQL说明,只有自己测试,现在记下以备后查)
%A oo'OBO0语法elect table1.fd1,table1,fd2,table2.fd2 From table1 left join table2 on table1.fd1,table2.fd1 where ...51Testing软件测试网F2[y3R0N:r,x k
使用SQL语句用...代替过长的字符串显示
4tnZzxf?3E-h1q0语法:
(n$BCB|J$RN&LI0SQL数据库:select case when len(field)>10 then left(field,10)+'...' else field end as news_name,news_id from tablename51Testing软件测试网{$Du j*YL
Access数据库:SELECT iif(len(field)>2,left(field,2)+'...',field) FROM tablename;
ig M&yH,h8Cq4W0
bKBW]1o0Conn.Execute说明
Ix5B+s/[;[0Execute方法51Testing软件测试网+Q"a*An;R9FG
该方法用于执行SQL语句。根据SQL语句执行后是否返回记录集,该方法的使用格式分为以下两种:
t-[/cs}Q4I0 1.执行SQL查询语句时,将返回查询得到的记录集。用法为:51Testing软件测试网(Q9^x)Hg#w
Set对象变量名=连接对象.Execute("SQL查询语言")
!Y-j/z3{V&X1xT)y0 Execute方法调用后,会自动创建记录集对象,并将查询结果存储在该记录对象中,通过Set方法,将记录集赋给指定的对象保存,以后对象变量就代表了该记录集对象。51Testing软件测试网Q;ZEmN Ml
8\?;U8si'ic0 2.执行SQL的操作性语言时,没有记录集的返回。此时用法为:
fy;@3@_Y0P0 连接对象.Execute "SQL操作性语句" [, RecordAffected][, Option]51Testing软件测试网'v{+tMfd
·RecordAffected为可选项,此出可放置一个变量,SQL语句执行后,所生效的记录数会自动保存到该变量中。通过访问该变量,就可知道SQL语句队多少条记录进行了操作。
[5R:p:omU0 ·Option可选项,该参数的取值通常为adCMDText,它用于告诉ADO,应该将Execute方法之后的第一个字符解释为命令文本。通过指定该参数,可使执行更高效。51Testing软件测试网u8e6S"]"? YDQ H9Kf
R&N*T3P+W2\"]0·BeginTrans、RollbackTrans、CommitTrans方法
7B0g;q)^ vbdIZY0 这三个方法是连接对象提供的用于事务处理的方法。BeginTrans用于开始一个事物;RollbackTrans用于回滚事务;CommitTrans用于提交所有的事务处理结果,即确认事务的处理。
v?7p1jI-MY`0 事务处理可以将一组操作视为一个整体,只有全部语句都成功执行后,事务处理才算成功;若其中有一个语句执行失败,则整个处理就算失败,并恢复到处里前的状态。51Testing软件测试网 ?6m}*Dg:ug?
BeginTrans和CommitTrans用于标记事务的开始和结束,在这两个之间的语句,就是作为事务处理的语句。判断事务处理是否成功,可通过连接对象的Error集合来实现,若Error集合的成员个数不为0,则说明有错误发生,事务处理失败。Error集合中的每一个Error对象,代表一个错误信息。
相关阅读:
- Oracle中查询主键、外键、sequence、表基本信息等 (smile665, 2010-5-03)
TAG: 主键
生活在互联网领域,从事WEB测试工作.
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 1693274
- 日志数: 556
- 图片数: 1
- 文件数: 3
- 书签数: 8
- 建立时间: 2007-06-08
- 更新时间: 2023-01-07