Microsoft SQL Server数据库知识点总结(一)

Microsoft SQL Server数据库知识点总结(一)

前言

对于数据库的知识,大部分的公司都是很重视的,

常见SQL分类

DDL — 数据定义语言(CREATE、ALTER、DROP、DECLARE)
DML — 数据操作语言(SELECT、INSERT、DELETE、UPDATE)
DCL — 数据控制语言(GRNAT、REVOKE、COMMIT、ROLLBACK)

常用基础语句

1、创建数据库

 CREATE DATABASE database_name

2、删除数据库

DROP DATABASE database_name

3、备份数据库

--创建备份数据的device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssqlbackup\MyDb_1.data'
--开始备份
BACKUP DATABASE mydb TO testBack

4、创建新表

create table tablename(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 tablename

6、增加删除列

--增加列
alter table tablename add column col type
--删除列
alter table tablename drop column col

7、添加删除主键

--添加主键
alter table tablename add primary key(col)
--删除主键
alter table tablename drop primary key(col)

8、创建删除索引

--创建索引
create [unique] index idxname on tablename(col,...)
--删除索引
drop index idxname

注:索引是不可更改的,想更改必须删除重新建

9、创建删除视图

--创建视图
create view viewname as select statement
--删除视图
drop view viewname

10、几个简单的SQL语句

--选择
select * from tablename where condition
--插入
insert into tablename(field1,field2,...) values(value1,value2,...)
--删除
delete from tablename where condition
--更新
update tablename set field1 = value1, field2 = value2, ... where condition
--查找
select * from tablename where field1 like '%value1%'		--like的语法很精妙,查资料!
--排序
select * from tablename order by field1,field2, ... [desc]
--总数
select count(*) as totalcount from tablename
--求和
select sum(field1) as sumvalue from tablename
--平均
select avg(field1) as avgvalue from tablename
--最大
select max(field1) as maxvalue from tablename
--最小
select min(field1) as minvalue from tablename

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
左外联接(左联接):结果集既包括联接表的匹配行,也包括左联接表的所有行。

select A.a,A.b,A.c,B.d,B.e,B.f from A LEFT OUTER JOIN B ON A.a = B.d

B:RIGHT OUTER JOIN
右外联接(右联接):结果集既包括联接表的匹配行,也包括右联接表的所有行。
C:FULL OUTER JOIN
全外联接:不仅包括符合联接表的匹配行,还包括两个联接表中的所有记录。

经典SQL语句汇总

1、复制表(只复制结构,源表名:a 新表名:b)

--方法一
select * into b from a where 1<>1
--方法二
select top 0 * into b from a

2、拷贝表(拷贝数据,原表名:a 目标表名:b)

insert into b(col1,col2,col3)  select colA,colB,colC from a

3、跨数据库之间表的拷贝(具体数据使用对决路径)

insert into b(col1,col2,col3) select colA,colB,colC from a in '具体数据库' where condition

4、子查询

select col1,col2,col3 from a where a in(select colA from b)
--或者
select col1,col2,col3 from a where a in(1,2,3)

5、外联接查询

select a.col1,a.col2,a.col3,b.colA,b.colB,b.colC from a left outer join b on a.col1 = b.colA

6、在线视图查询

select * from (select col1,col2,col3 from a) T where T.col1 > 1

7、between的用法,between限制查询数据范围时包括了边界值,not between不包括

select * from table1 where time between time1 and time2
select col1,col2,col3 from table1 where col1 not between value1 and 

8、in的使用方法

select * from table1 where a [not] in(value1,value2,value3,...)

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

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

10、四表联查问题

select * from a left inner join b on a.col1 = b.col2 right inner join c on a.col1 = c.col3 inner join d on a.col1 = d.col4 where ...

11、日程安排提前五分钟提醒

select * from 日程安排 where datediff('minute',f开始时间,getdate()) > 5

12、一条SQL语句搞定数据库分页

select top 10 b.* from (select top 20 主键字段,排序字段 from tablename order by 排序字段 desc) a,tablename b where b.主键字段 = a.主键字段 order by a.排序字段

13、取前10条记录

select top 10 * from tablename where condition

14、选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品伏牛溪,按科目成绩排名,等等)

select a,b,c from tablename ta where a = (select max(a) from tableename tb wheere tb.b = ta.b)

15、包括所有在TableA中但不在TableB和TableC中的行并消除所有重复行而派生出一个结果表

(select a from tableA) except (select a from tableB) except (select a from tableC)

16、随机取出10条数据

select top 10 * from tablenam order by newid()

17、随机选择记录

select newid()

18、删除重复记录

delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

19、列出数据库里所有的表名

select nam from sysobjects where type = 'U'

20、列出表里所有的字段

select name from syscolumns where id = object_id('TableName')

21、列出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 typee

显示结果:

扫描二维码关注公众号,回复: 9304428 查看本文章
type vender pcs
电脑 A 1
电脑 A 1
光盘 B 2
光盘 A 2
手机 B 3
手机 C 3

22、截断(初始化)表table1

truncate table table1

23、选择从10到15的记录

select top 5 * from (select top 15 * from tableename order by id asc) table1 order by id desc
发布了107 篇原创文章 · 获赞 291 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/zlbdmm/article/details/103695057