sql常用语句,帮我归纳出来,谢谢

如题所述

SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作。
一、基础
1、说明:创建数据库
CREATE DATABASE database-name
2、说明:删除数据库
drop database dbname
3、说明:备份sql server
--- 创建 备份数据的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 开始 备份
BACKUP DATABASE pubs TO testBack
4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表:
A:create table tab_new like tab_old (使用旧表创建新表)
B:create table tab_new as select col1,col2... from tab_old definition only
5、说明:删除新表
drop table tabname
6、说明:增加一个列
Alter table tabname add column col type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
7、说明:添加主键: Alter table tabname add primary key(col)
说明:删除主键: Alter table tabname drop primary key(col)
8、说明:创建索引:create [unique] index idxname on tabname(col....)
删除索引:drop index idxname
注:索引是不可更改的,想更改必须删除重新建。
9、说明:创建视图: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
11、说明:几个高级查询运算词
A: UNION 运算符
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
B: EXCEPT 运算符
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
C: INTERSECT 运算符
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
12、说明:使用外连接
A、left outer join:
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
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
B:right outer join:
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
C:full outer join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
二、提升
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1
法二:select top 0 * into b from a
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
insert into b(a, b, c) select d,e,f from b in '具体数据库' where 条件
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
4、说明:子查询(表名1:a 表名2: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)
5、说明:显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
6、说明:外连接查询(表名1:a 表名2: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
7、说明:在线视图查询(表名1:a )
select * from (SELECT a,b,c FROM a) T where t.a > 1;
8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2
9、说明:in 的使用方法
select * from table1 where a [not] in ('值1','值2','值4','值6')
10、说明:两张关联表,删除主表中已经在副表中没有的信息
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
11、说明:四表联查问题:
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 .....
12、说明:日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
13、说明:一条sql 语句搞定数据库分页
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段
14、说明:前10条记录
select top 10 * form table1 where 范围
15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)
17、说明:随机取出10条数据
select top 10 * from tablename order by newid()
18、说明:随机选择记录
select newid()
19、说明:删除重复记录
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
20、说明:列出数据库里所有的表名
select name from sysobjects where type='U'
21、说明:列出表里的所有的
select name from syscolumns where id=object_id('TableName')
22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
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
显示结果:
type vender pcs
电脑 A 1
电脑 A 1
光盘 B 2
光盘 A 2
手机 B 3
手机 C 3
23、说明:初始化表table1
TRUNCATE TABLE table1
24、说明:选择从10到15的记录
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
三、技巧
1、1=1,1=2的使用,在SQL语句组合时用的较多
"where 1=1" 是表示选择全部 "where 1=2"全部不选,
如:
if @strWhere !=''
begin
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where ' + @strWhere
end
else
begin
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'
end
我们可以直接写成
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere
2、收缩数据库
--重建索引
DBCC REINDEX
DBCC INDEXDEFRAG
--收缩数据和日志
DBCC SHRINKDB
DBCC SHRINKFILE
3、压缩数据库
dbcc shrinkdatabase(dbname)
4、转移数据库给新用户以已存在用户权限
exec sp_change_users_login 'update_one','newname','oldname'
go
5、检查备份集
RESTORE VERIFYONLY from disk='E:\dvbbs.bak'
6、修复数据库
ALTER DATABASE [dvbbs] SET SINGLE_USER
GO
DBCC CHECKDB('dvbbs',repair_allow_data_loss) WITH TABLOCK
GO
ALTER DATABASE [dvbbs] SET MULTI_USER
GO
7、日志清除
SET NOCOUNT ON
DECLARE @LogicalFileName sysname,
@MaxMinutes INT,
@NewSize INT
USE tablename -- 要操作的数据库名
SELECT @LogicalFileName = 'tablename_log', -- 日志文件名
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
@NewSize = 1 -- 你想设定的日志文件的大小(M)
-- Setup / initialize
DECLARE @OriginalSize int
SELECT @OriginalSize = size
FROM sysfiles
WHERE name = @LogicalFileName
SELECT 'Original Size of ' + db_name() + ' LOG is ' +
CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' +
CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'
FROM sysfiles
WHERE name = @LogicalFileName
CREATE TABLE DummyTrans
(DummyColumn char (8000) not null)
DECLARE @Counter INT,
@StartTime DATETIME,
@TruncLog VARCHAR(255)
SELECT @StartTime = GETDATE(),
@TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY'
DBCC SHRINKFILE (@LogicalFileName, @NewSize)
EXEC (@TruncLog)
-- Wrap the log if necessary.
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired
AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)
AND (@OriginalSize * 8 /1024) > @NewSize
BEGIN -- Outer loop.
SELECT @Counter = 0
WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))
BEGIN -- update
INSERT DummyTrans VALUES ('Fill Log')
DELETE DummyTrans
SELECT @Counter = @Counter + 1
END
EXEC (@TruncLog)
END
SELECT 'Final Size of ' + db_name() + ' LOG is ' +
CONVERT(VARCHAR(30),size) + ' 8K pages or ' +
CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'
FROM sysfiles
WHERE name = @LogicalFileName
DROP TABLE DummyTrans
SET NOCOUNT OFF
8、说明:更改某个表
exec sp_changeobjectowner 'tablename','dbo'
9、存储更改全部表
CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVARCHAR(128),
@NewOwner as NVARCHAR(128)
AS
DECLARE @Name as NVARCHAR(128)
DECLARE @Owner as NVARCHAR(128)
DECLARE @OwnerName as NVARCHAR(128)
DECLARE curObject CURSOR FOR
select 'Name' = name,
'Owner' = user_name(uid)
from sysobjects
where user_name(uid)=@OldOwner
order by name
OPEN curObject
FETCH NEXT FROM curObject INTO @Name, @Owner
WHILE(@@FETCH_STATUS=0)
BEGIN
if @Owner=@OldOwner
begin
set @OwnerName = @OldOwner + '.' + rtrim(@Name)
exec sp_changeobjectowner @OwnerName, @NewOwner
end
-- select @name,@NewOwner,@OldOwner
FETCH NEXT FROM curObject INTO @Name, @Owner
END
close curObject
deallocate curObject
GO
10、SQL SERVER中直接循环写入数据
declare @i int
set @i=1
while @i<30
begin
insert into test (userid) values(@i)
set @i=@i+1
end
小记存储过程中经常用到的本周,本月,本年函数
Dateadd(wk,datediff(wk,0,getdate()),-1)
Dateadd(wk,datediff(wk,0,getdate()),6)
Dateadd(mm,datediff(mm,0,getdate()),0)
Dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))
Dateadd(yy,datediff(yy,0,getdate()),0)
Dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))
上面的SQL代码只是一个时间段
Dateadd(wk,datediff(wk,0,getdate()),-1)
Dateadd(wk,datediff(wk,0,getdate()),6)
就是表示本周时间段.
下面的SQL的条件部分,就是查询时间段在本周范围内的:
Where Time BETWEEN Dateadd(wk,datediff(wk,0,getdate()),-1) AND Dateadd(wk,datediff(wk,0,getdate()),6)
而在存储过程中
select @begintime = Dateadd(wk,datediff(wk,0,getdate()),-1)
select @endtime = Dateadd(wk,datediff(wk,0,getdate()),6)
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-18
这是调试通过的
<%
'这里假定你的数据库名为db.mdb放在Datebase目录下,用户表名为users,其中有id(自动编号),userName,PassWord,ip,email几个字段
dim conn,connstr,strdb,rs,action,username,password,newpassword,ip,mail
strdb="Datebase/db.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(""&strdb&"")
conn.Open connstr

action=request("action")
username=request("username")
password=request("password")
newpassword=request("newpassword")
mail=request("mail")
if request("ip") ="" then
ip=Request.ServerVariables("REMOTE_ADDR")
end if

if action="reg" then
if username="" or password="" or mail="" then
response.write("注册失败,账号密码邮箱不能为空")
response.end
end if
if isUser(username) then
response.write "注册失败,账号已经存在"
else
'在这里执行SQL语句,添加新的项目,将 username 和 password 和 mail添加进去
sql="insert into users ([userName],[PassWord],ip,email) values ('"&username&"','"&password&"','"&ip&"','"&mail&"')"
conn.execute(sql)
response.write("注册成功")
end if
response.end
end if

if action="edit" then
if username="" or password="" or newpassword="" then
response.write "改密失败,账号密码新密码不能为空"
response.end
end if
if not isLegalUser(username,password) then
response.write "改密失败,账号不存在或旧密码错误"
else
'更改密码
sql="update users set [PassWord]='"&newpassword&"' where [userName]='"&username&"'"
conn.execute(sql)
response.write "改密成功"
end if
response.end
end if

if action="find" then
if username="" or mail="" then
response.write "找回失败,账号邮箱不能为空"
response.end
end if
if not isUser(username) then
response.write "找回失败,账号不存在"
else
'取得用户的密码加入到password 变量中,接着直接输出密码
set rs=server.CreateObject("adodb.recordset")
sql="select PassWord from users where [UserName]='"&username&"' and [email]='"&mail&"'"
rs.open sql,conn,1,1
if rs.eof then
response.write("找回失败,账号或邮箱地址不正确")
else
password=rs("PassWord")
response.write("找回成功,你的密码是:" & password )
end if
rs.close
set rs=nothing
end if
response.end
end if

if action="update" then
if username="" or password="" then
response.write "更新失败,账号密码不能为空"
response.end
end if
if not isUser(username) then
response.write "更新失败,账号不存在"
response.end
else
'更新到用户名的IP列中
sql="update users set ip='"&ip&"' where [userName]='"&username&"'"
conn.execute(sql)
response.write ("更新成功,最新IP:" & ip )
end if
response.end
end if

if action="showip" then
if username="" then
response.write "输出失败,账号不能为空"
response.end
end if
'在这里执行SQL语句,将用户的IP地址取得后加入到变量IP中
set rs=server.CreateObject("adodb.recordset")
sql="select ip from users where [UserName]='"&username&"'"
rs.open sql,conn,1,1
if rs.eof then
response.write("输出失败,账号不存在")
else
ip=rs("ip")
response.write("你的IP是:" & ip )
end if
rs.close
set rs=nothing
response.end
end if
'======================
'检查账号是否存在
'======================
function isUser(userStr)
set rs=server.CreateObject("adodb.recordset")
sql="select * from users where [UserName]='"&userStr&"'"
rs.open sql,conn,1,1
if rs.eof then
isUser=false
else
isUser=true
end if
rs.close
set rs=nothing
end function
'======================
'检查用户名和密码是否都正确
'======================
function isLegalUser(userStr,pwsStr)
set rs=server.CreateObject("adodb.recordset")
sql="select * from users where [UserName]='"&userStr&"' and [PassWord]='"&pwsStr&"'"
rs.open sql,conn,1,1
if rs.eof then
isLegalUser=false
else
isLegalUser=true
end if
rs.close
set rs=nothing
end function

%>

如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!

vaela
第2个回答  2010-10-18
下面都是开发必须要的语法 好好保存 做开发都经常用到的
(我总结了几年,其他的语法经典知识有很多C#,java,js,jsp,你想要可以给我留言)
select 列名(*代表全部列) from 表明
创建表
create table 表名

字段名1 类型 约束/是否为空/主键。。,
字段名2 类型 约束/是否为空


插入数据
insert into 表名 values(值1,值2,值3....)
insert into 表名(字段1,字段2,字段3....)values (值1,值2,值3...)

更改表以添加新列-------------------------------
注意:ALTER TABLE 只允许添加可包含空值或指定了 DEFAULT 定义的列。---
语法:
alter table 表名 add 新列名 类型 约束/是否为空/主键。。

更改表以除去列
语法:
alter table 表名 drop column 列名

修改表的列的类型
语法:
alter table 表名 alter column 列名 新类型

一次插入多行数据
insert into 新表 select * from 旧表
insert into 新表(字段1,字段2,字段3.....)select * from 旧表(字段1,字段2,字段3....)

select * into 新表 from 旧表
select (字段1,字段2,字段3。。。) into 新表 from 旧表
----select into拷贝用法需要注意--------------------------
语法:将一个表的结构拷贝到另一个表中(不拷贝数据)
select * into 新表 from 旧表 where 1=0
语法:将一个表拷贝到自动新建的表中
select * into 新表 from 旧表 where 1=1 或者 select * into aa from student

insert 表名(字段1,字段2,字段3)
select 值,值,值 union
select 值,值,值 union
select 值,值,值

修改纪录
修改满足条件下的一条纪录的语法如下:
update 表名 set 字段名1=新值,字段2=新值 where 字段名3=值
不加条件就意味着修改所有语法如下:
update 表名 set 字段名=新值

like的用法
select * from 表名1 where 字段1 like '%荆%'

is null 用法
select * from 表名1 where 字段1 is null 区别于下面 select * from students where stuAddress =''

is not null 用法
select * from 表名1 where 字段1 is not null

between用法
select * from 表名1 where 字段1 between 60 and 80

in 用法
select * from 表名1 where 字段1 in (值1,值2,值3.....)

聚合函数
select sum(字段1) as 别名 from 表名 where 字段=值
select max(字段1) as 别名 from 表名 where 字段=值
select min(字段1) as 别名 from 表名 where 字段=值
select count(*) as 别名 from 表名

group by用法 //注意结合上课所讲的
select 字段,avg(字段) as 别名 from 表名 group by 字段

inner join ... on 注意:将表1和表2中的公共的数据 给列在拼接表中
select * from 表1 inner join 表2 on 表1.字段名=表2.字段

left join...on --注意:左连接是把 两个表中的公共的数据给列出来在拼接表中,并且把 左表即left join 左边的表students中有的但是在 scores中没有的也列出来
select * from 主表 left join 子表 on 主表.字段=子表.字段
注意:
select * from 子表 left join 主表 on 主表.字段=子表.字段 相当于内连接 如下
select * from 表1 inner join 表2 on 表1.字段名=表2.字段

right join ...on用法
select * from 主表 right join 子表 on 主表.字段=子表.字段

--------------创建一个数据库 -----------
create database 库名
on primary ---主要数据文件
(
name=库名_data,---主要数据文件逻辑文件名,可任意命名,与日志文件逻辑文件名不能相同
filename='盘符:\路径\库名.mdf',---主要数据文件物理文件名
size=1mb,----最小空间
maxsize=5mb,----最大空间
filegrowth=10%----增涨率
)
-----日志文件-----
log on
(
name=库名_log,--日志文件逻辑文件名,可任意命名,与主要数据文件逻辑文件名不能相同
filename='盘符:\路径\库名.ldf',--日志文件物理文件名
size=1mb,----最小空间
maxsize=5mb,----最大空间
filegrowth=10%----增涨率
)

--------------维护数据库的结构-------------
----修改大小
ALTER DATABASE 库名
MODIFY FILE
(
NAME = '主要数据文件名或日志文件名',
SIZE = 新空间大小MB
)

----添加次要数据文件
ALTER DATABASE Sample
ADD FILE
(
NAME = 次要数据文件名,
FILENAME='盘符:\路径\库名.ndf',
SIZE=空间大小MB,
MAXSIZE=最大空间大小MB
)

--------------删除数据库-------------
drop database 库名1,库名2,……库名n

--------------创建一个数据表-------------------
CREATE TABLE 表名
(
列名1 类型1 约束,
列名2 类型2 约束,
………………
列名n 类型n 约束
)

----创建表时直接创建各种约束
CREATE TABLE 表名
(
列名1 类型1 primary key(列名1,列名2,……列名n),---主键约束
列名2 类型2 unique, ---唯一约束
列名3 类型3 identity(初始值,增量),---标识列约束
列名4 类型4 default 默认值[注意:字符和日期要用单引号]---默认值约束
列名5 类型5 check(列名及常量及运算符组成的表达式)---检查约束
………………
列名n 类型n,foreign key(列名1,列名2,……列名n) references 主键所在表名(列名a1,列名a2,……列名n)
)
-------------删除数据表--------------
drop table 表名1,表名2,……表名n

-------------维护数据表的结构-----------------
-----修改列
alter table 表名 alter column 列名 新类型(长度)
-----添加列
alter table 表名 add 列名 类型(长度)
-----删除列
alter table 表名 drop column 列名

-----添加主键约束
alter table 表名
add constraint 主键约束名 (外键列名)
primary key(列名1,列名2,……列名n)
-----删除主键约束
alter table 表名 drop constraint 主键约束名
-----添加唯一约束
alter table 表名 add constraint 唯一约束名 unique(列名)
-----删除唯一约束
alter table 表名 drop constraint 唯一约束名

-----添加外键约束
alter table 表名 add constraint 外键约束名 foreign key(列名1,列名2,……列名n) references 主键所在的表名(列名A1,列名A2,……列名An)
-----删除主键约束
alter table 表名 drop constraint 外键约束名
-----添加默认约束
alter table 表名 add constraint 默认约束名 default 默认值 for 列名
-----删除默认约束
alter table 表名 drop constraint 默认约束名
-----添加检查约束
alter table 表名 add constraint 检查约束名 check(列名及常量及运算符组成的表达式)
-----删除检查约束
alter table 表名 drop constraint 检查约束名

-------------------维护表的数据-------------------
-----插入数据
USE BOOKS (利用数据库)
GO
insert into 表名
values(列值1,列值2,……列值n)
insert into 表名1(列名1,列名2,……列名n)
select (列名a1,列名a2,……列名an) from 表名2

-----查询数据
select * from 表名 ---显示所有记录的所有列
select 列名1,列名2,……列名n from 表名 -----显示所有记录的某些列
select * from 表名
where 条件表达式 ---显示满足条件的所有记录的所有列
select 列名1,列名2,……列名n from 表名
where 条件表达式 -----显示满足条件的所有记录的某些列

-----删除数据
delete from 表名
where 条件表达式 -----删除满足条件的记录
truncate table 表名 -----删除所有的记录,只保留表的结构

汉化标题
select 别名1.列名1,……别名1.列名n,别名2.列名1,……别名2.列名n
from 表名1 as 别名1 inner join 表名2 as 别名2
on 别名1.列名=别名2.列名
where 条件表达式
---内联接的等值联接
select 别名1.列名1,……别名1.列名n,别名2.列名1,……别名2.列名n
from 表名1 as 别名1
inner join 表名2 as 别名2
on 别名1.列名>别名2.列名
where 条件表达式
---内联接的不等值联接
select 别名1.列名1,……别名1.列名n,别名2.列名1,……别名2.列名n
from 表名1 as 别名1
left outer join 表名2 as 别名2
on 别名1.列名=别名2.列名
where 条件表达式 ---内联接的左外联接
select 别名1.列名1,……别名1.列名n,别名2.列名1,……别名2.列名n
from 表名1 as 别名1
right outer join 表名2 as 别名2
on 别名1.列名=别名2.列名
where 条件表达式
---内联接的右外联接
select 别名1.列名1,……别名1.列名n,别名2.列名1,……别名2.列名n
from 表名1 as 别名1
full outer join 表名2 as 别名2
on 别名1.列名=别名2.列名 where 条件表达式
---内联接的完全外联接
select 列名1,列名2,……列名n
from 表名
where 条件表达式
order by 列名---按某列排序
[ASC为升序,DESC为降序。对于联接查询的排序只需将上述语法后加上order by子句即可]
select 列名1+'符号常量'+列名2+'符号常量'+……列名n
from 表名
where 条件表达式 -----使用特殊符号显示满足条件的所有记录的某些列
select 列名1 as 新列名1,列名2 as 新列名2,……列名n as 新列名n
from 表名
where 条件表达式 -----使用as子句显示满足条件的所有记录的某些列
select identity(类型,初值,增量)
as 新列名
into 新创建的表名
from 已有的表名----使用
identity从一个旧表创建一个新表,且只有一个标识列
select identity(类型,初值,增量)
into 新创建的表名 from 已有的表名----使用
identity从一个旧表创建一个新表,且只有一个标识列
[当新表中只有一个标识列时可不用as]
select 新列名=identity(类型,初值,增量)
into 新创建的表名 from 已有的表名----使用
identity从一个旧表创建一个新表,且只有一个标识列
select identity(类型,初值,增量) as 新列名1,旧表的列名1 as 新表的列名2,…… 旧表的列名n as 新表的列名n
into 新创建的表名
from 已有的表名----使用
identity从一个旧表创建一个新表,且有一个标识列和其它列
[当新表中有很多列时必用as]
select top n * from 表名
where 条件表达式---显示前n行记录
select top n percent * from 表名
where 条件表达式---显示前n%行记录
select 列名1,列名2,……列名n
from 表名
group by 列名1,列名2,……列名n
select 列名,聚合函数
[sum(列名)或avg(列名)或count(列名)或max(列名)或min(列名)] from 表名
group by 列名----使用聚合函数分组查询
[注意sum和avg只用于数字型的列,count用于数字型和字符型的列,max和min用于数字、字符和日期型的列,sum、avg和count支持distinct]
select 列名1,列名2,……列名n
from 表名 group by 列名1,列名2,……列名n
having 条件表达式----使用having子句查询
[注意where、group和having的顺序不能颠倒]
select * from 表名
where 通配符条件表达式
--- 列名 like '_A%'
--- 列名 not like '_A%'
--- 列名 in(值1,值2,……值n)
--- 列名 not in(值1,值2,……值n)
--- 列名 between 值1 and 值2
--- 列名 not between 值1 and 值2
--- 列名 is null
--- 列名 is not null

-----更新数据
update 表名 set 列名1=值1,列名2=值2,……列名n=值n
where 条件表达式----更新满足条件的记录的某些列值
update 表名 set 列名1=值1,列名2=值2,……列名n=值n
from 表名1 as 别名1
inner join 表名2 as 别名2 on 别名1.列名=别名2.列名
where 条件表达式 ---内联接的等值联接更新
update 表名 set 列名1=值1,列名2=值2,……列名n=值n
from 表名1 as 别名1
inner join 表名2 as 别名2 on 别名1.列名>别名2.列名
where 条件表达式
---内联接的不等值联接更新
update 表名 set 列名1=值1,列名2=值2,……列名n=值n
from 表名1 as 别名1
left outer join 表名2 as 别名2
on 别名1.列名=别名2.列名 where 条件表达式
---内联接的左外联接更新
update 表名 set 列名1=值1,列名2=值2,……列名n=值n
from 表名1 as 别名1
right outer join 表名2
as 别名2 on 别名1.列名=别名2.列名
where 条件表达式 ---内联接的右外联接更新
update 表名 set 列名1=值1,列名2=值2,……列名n=值n
from 表名1 as 别名1
full outer join 表名2 as 别名2 on 别名1.列名=别名2.列名 where 条件表达式 ---内联接的完全外联接更新
第3个回答  2010-10-18
sql常用的无非就是增、删、查、改
更新 update ...set语法:
update 表名 set 列名=更新值 Where=更新条件
(一般update都是有条件更新的)
查找语法:
Select 列名 From 表名 或者Select * From 表名(这是查询整张表)
删除语法:
删除数据:
Delete From 表名 Where Name='张三' (delete是足行删除,不可以单个删除)
Truncate Table 表名 (Truncate table 删除表中的所有行,不会删除表结构、列、约束、索引等)
删除数据库:
Drop database 数据库名
删除表:
Drop table 表名
增加语法:
单行增加:
insert 表名(列名) Values(增加的数据)
多行增加:
insert 表名 (列名)
Select 增加的数据 Union(最后一行数据后面不要Union)
这些都是常用的SQL语句
建议楼主去买《sql Server 必知必会》这本书,挺好的,希望能帮到你!
相似回答