对数据库操作常用的sql语句,语法,很全面:
asc 按升序排列
desc 按降序排列
下列语句部分是Mssql语句,不可以在access中使用。
SQL分类:
DDL—数据定义语言(Create,Alter,Drop,DECLARE)
DML—数据操纵语言(Select,Delete,Update,Insert)
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
首先,简要介绍基础语句:
1、说明:创建数据库
Create DATABASE database-name
drop database dbname
--- 创建 备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'--- 开始 备份BACKUP DATABASE pubs TO testBack
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
create table tab_new like tab_old
create table tab_new as select col1,col2… from tab_old definition only
drop table tabname
Alter table tabname add column col type
Alter table tabname add primary key(col)
Alter table tabname drop primary key(col)
create [unique] index idxname on tabname(col….)
drop index idxname
create view viewname as select statement
删除视图:
drop view viewname
10、说明:几个简单的基本的sql语句
选择:
select * from table1 where 范围
insert into table1(field1,field2) values(value1,value2)
delete from table1 where 范围
update table1 set field1=value1 where 范围
select * from table1 where field1 like '%value1%' ---like的语法很精妙,查资料!
select * from table1 order by field1,field2 [desc]
select count as totalcount from table1
select sum(field1) as sumvalue from table1
select avg(field1) as avgvalue from table1
select max(field1) as maxvalue from table1
select min(field1) as minvalue from table1
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
select * into b from a where 1<>1
select top 0 * into b from a
insert into b(a, b, c) select d,e,f from b;
insert into b(a, b, c) select d,e,f from b in '具体数据库' where 条件
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)
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
select * from (Select a,b,c FROM a) T where t.a > 1;
select * from table1 where time between time1 and time2select a,b,c, from table1 where a not between 数值1 and 数值2
select * from table1 where a [not] in (‘值1','值2','值4','值6')
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
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 .....
sql: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
select top 10 * form table1 where 范围
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
(select a from tableA ) except (select a from tableB) except (select a from tableC)
select top 10 * from tablename order by newid()
select newid()
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
select name from sysobjects where type='U'
select name from syscolumns where id=object_id('TableName')
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
TRUNCATE TABLE table1
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
RandomizeRNumber = Int(Rnd*499) +1While Not objRec.EOFIf objRec("ID") = RNumber THEN... 这里是执行脚本 ...end ifobjRec.MoveNextWend
RandomizeRNumber = Int(Rnd*499) + 1sql = "Select * FROM Customers Where ID = " & RNumberset objRec = ObjConn.Execute(SQL)Response.WriteRNumber & " = " & objRec("ID") & " " & objRec("c_email")
sql = "Select * FROM Customers Where ID = " & RNumber & " or ID = " & RNumber2 & " or ID = " & RNumber3
sql = "Select * FROM Customers Where ID BETWEEN " & RNumber & " AND " & RNumber & "+ 9"
Select top 10 * From 表名 orDER BY Rnd(id)
select top n * from 表名 order by newid()
select * From 表名 order By rand() Limit n
elect table1.fd1,table1,fd2,table2.fd2 From table1 left join table2 on table1.fd1,table2.fd1 where ...
select case when len(field)>10 then left(field,10)+'...' else field end as news_name,news_id from tablename
Select iif(len(field)>2,left(field,2)+'...',field) FROM tablename;