[MS SQL]常用SQL语句大全1-基本操作

来源:互联网 发布:淘宝网哈伦裤女裤 编辑:程序博客网 时间:2024/05/17 21:43

一、基础

1、数据库操作

   1.1 创建数据库

 CREATE DATABASE databasename

   1.2 删除数据库

 drop database dbname

   1.3 修改数据库的名称:

 sp_renamedb 'old_name''new_name' 

2、说明:备份sql server 
      2.1 创建 备份数据的 device

 USE master
 EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' 

      2.2 开始备份

 BACKUP DATABASE pubs TO testBack

4、表操作
      4.1 新建表

 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

       4.2 根据已有的表创建新表(即复制表结构,不复制旧表数据):

 select * into table_new from table_old (使用旧表创建新表)

      4.3 根据已有的表创建新表(复制表结构和数据):

 insert into table_new(column1,column2,column3) select (column4,column5,column5) from table_old  
     4.4 删除表
 drop table tablename
     4.5 增加一个列,删除一个列
 alter table tabname add column colname coltype
 alter table tabname drop column colname

注:DB2DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

可参看:对某个DB中所有的表删除指定字段 和 对某个DB中所有的表加上一个字段

    4.6 添加主键:

 Alter table tabname add primary key(col)

    4.7 删除主键:

 Alter table tabname drop primary key(col)

    4.8 创建索引:

 create [unique] index idxname on tabname(col….)

    4.9 删除索引:

 drop index idxname

注:索引是不可更改的,想更改必须删除重新建。
5. 创建视图:

 create view viewname as select statement 

   删除视图:

 drop view viewname

  在线视图查询:

 select * from (select column1, column2, column3 FROM table1) T where T.column1 > 1 

6、SELECT语句

 1 随机选择记录

 select newid() 

 2 随机取出10条记录

 select top 10 * from tablename order by newid() 

 3 选择从10到15的记录

 select top 5 * from (select top 15 * from tablename order by id asc) order by id desc

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

两张关联表,删除主表中已经在副表中没有的信息

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

详细用法见另一篇博文:[SQL][双语]入門之AVG/COUNT/MAX/MIN/SUM

11、几个高级查询运算词
A: UNION 运算符
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。

详细用法见另一篇博文:[SQL][双语]進階之UNION/UNION ALL
B: EXCEPT 运算符
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行

实例:所有在TableA中,但不在TableB和TableC中的行,并消除所有重复行

 (select a from tableA) except (select a from tableB) except ( select a from tableC) 
C: INTERSECT 运算符
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。

12、使用外连接
A、
left (outer) join:
左外连接(左连接):结果集既包括连接表的匹配行,也包括左连接表的所有行。

 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/cross (outer) join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
详细用法见另一篇博文:[SQL][双语]入門之表格連接Join

13、分组:Group by:
一张表,一旦分组完成后,查询后只能得到组相关的信息。
组相关的信息:(统计信息) count,sum,max,min,avg 分组的标准)
在SQLServer中分组时:不能以text,ntext,image类型的字段作为分组依据
在selecte统计函数中的字段,不能和普通的字段放在一起;
详细用法见另一篇博文:[SQL][双语]入門之Group by/Having

15.查詢Trigger

 use [mydb]
 go 
 Select name as 触发器名称,object_name(parent_obj) as 所在表名
 from sysobjects where xtype= 'TR ' order by Crdate desc 

16. 计算数量

 select count(distinct columnname) FROM TestTable

17. 在存储过程中查找是否存在某个字符串

 SELECT Distinct SO.Name
 FROM sysobjects SO (NOLOCK)
 INNER JOIN syscomments SC (NOLOCK) on SO.Id = SC.ID
 AND SO.Type = 'P' 
 AND SC.Text LIKE '%arperiod%' 
 ORDER BY SO.Name
0 0