删除表数据,保留表结构: delete from employee 将删除所有的记录 delete from employee where lastname = 'May' 这条语句是从emplyee表中删除lastname为'May'的行 备份表: select * into table1_backup from table1 创建表: create table dbo.Category (CategoryID int identity (1,1) primary key, CategoryName nvarchar(15) not null, Picture image null) 创建主键: alter table 表名 add constraint 主键名 primary key (列1, 列2) 删除主键: alter table 表名 drop constraint 主键名 查看主键: select * from sysobjects where xtype='PK' 删除表数据和表结构: drop table table_name 根据已有的表创建新表: select * into newtable from oldtable where 1=0 增加列: alter table 表名 add column 列名 数据类型 [NOTNULL] [约束] 删除列: alter table 表名 drop column 列名 创建索引: create [索引类型] index 索引名称 on 表名(列名) 删除索引: drop index idxname 创建视图: create view viewname as select statement 删除视图: drop view viewname 插入: insert into 表名 values(value1,value2) 删除: delete from 表名 where 字段名='需要删除的数据' 更新: update 表名 set 字段=修改后的数据 where 字段='修改条件' 查找: select * from 表名 where 字段 like '%value1%' 排序: select * from 表名 order by field1[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 UNION 运算符: UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。 当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自TABLE1 就是来自 TABLE2。 EXCEPT 运算符: EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。 当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 INTERSECT 运算符: INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。 当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。 左外连接(左连接): 结果集几包括连接表的匹配行,也包括左连接表的所有行。 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 右外连接(右连接): 结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 全外连接: 不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。 子查询: select * from ( SELECT count(*) as a,* FROM [TRAPP].[dbo].[aaaaa] Group by [AcctNo] ,[TrnDate] ,[LegalNo] ,[DocNo] ,[Debit] ,[Credit] ,[TrnText] ,[Yr] ,[Mn] ) c where c.a>2