对于数据库的知识,大部分的公司都是很重视的,
DDL — 数据定义语言(CREATE、ALTER、DROP、DECLARE)
DML — 数据操作语言(SELECT、INSERT、DELETE、UPDATE)
DCL — 数据控制语言(GRNAT、REVOKE、COMMIT、ROLLBACK)
CREATE DATABASE database_name
DROP DATABASE database_name
--创建备份数据的device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssqlbackup\MyDb_1.data'
--开始备份
BACKUP DATABASE mydb TO testBack
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
drop table tablename
--增加列
alter table tablename add column col type
--删除列
alter table tablename drop column col
--添加主键
alter table tablename add primary key(col)
--删除主键
alter table tablename drop primary key(col)
--创建索引
create [unique] index idxname on tablename(col,...)
--删除索引
drop index idxname
注:索引是不可更改的,想更改必须删除重新建
--创建视图
create view viewname as select statement
--删除视图
drop view viewname
--选择
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
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),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
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
全外联接:不仅包括符合联接表的匹配行,还包括两个联接表中的所有记录。
--方法一
select * into b from a where 1<>1
--方法二
select top 0 * into b from a
insert into b(col1,col2,col3) select colA,colB,colC from a
insert into b(col1,col2,col3) select colA,colB,colC from a in '具体数据库' where condition
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)
select a.col1,a.col2,a.col3,b.colA,b.colB,b.colC from a left outer join b on a.col1 = b.colA
select * from (select col1,col2,col3 from a) T where T.col1 > 1
select * from table1 where time between time1 and time2
select col1,col2,col3 from table1 where col1 not between value1 and
select * from table1 where a [not] in(value1,value2,value3,...)
delete from table1 where not exists(select * from table2 where table1.field1 = table2.field1)
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 ...
select * from 日程安排 where datediff('minute',f开始时间,getdate()) > 5
select top 10 b.* from (select top 20 主键字段,排序字段 from tablename order by 排序字段 desc) a,tablename b where b.主键字段 = a.主键字段 order by a.排序字段
select top 10 * from tablename where condition
select a,b,c from tablename ta where a = (select max(a) from tableename tb wheere tb.b = ta.b)
(select a from tableA) except (select a from tableB) except (select a from tableC)
select top 10 * from tablenam order by newid()
select newid()
delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
select nam 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 typee
显示结果:
type | vender | pcs |
---|---|---|
电脑 | A | 1 |
电脑 | A | 1 |
光盘 | B | 2 |
光盘 | A | 2 |
手机 | B | 3 |
手机 | C | 3 |
truncate table table1
select top 5 * from (select top 15 * from tableename order by id asc) table1 order by id desc