SQLServer 基础(二)

数据的增删改

  1、插入(insert)(注意主键和外键)

  向T_Person表里插入Tom的信息

  正规写法:insert into T_Person(FName,FAge,FRemark)Values('Tom',18,'USA')

  简易写法:insert into T_Person Values('Tom',18,'USA')(此方法必须与表里的字段顺序和个数一致)

  2、更新(update)(注意主键和外键)

  更新T_Person的所有FRemark字段

  update T_Person set FRemark=‘SuperMan’

  更新一条或几条数据用Where,not和or关键字,字段FName=‘Tom’和‘Jim’

  update T_person set Fage=12 where FName=‘Tom’ or FName=‘Jim’

  3、删除(delete)(注意外键)

  简单的删除(所有数据,表的结构还在):Delete from T_person

  删除一条或几天用where,not 或or关键字

  delete from T_Person where FAge>20 or FRemark=‘Mars’

select基本用法

1、简单检索(表中所有信息):

  select * from T_Employee

2、指定列:

  select FName,FAge from T_Employee

  2.1、列别名:改变显示列的名称,关键字as

  select FName as name,FAge as age  from T_Employee

  2.2、条件过滤where ,not 和or关键字

  select * from T_Employee where FSalary <5000 or FAge>25

  2.3、数据汇总

  最大max,最小min关键字:

  年龄大于25岁员工的最高工资:select max(FSalary),min(FSalary) from T_Employee where FAge>25

  平均avg关键字:

  select avg(FAge) from T_Employee where FSalary >3800

  总额sum关键字:

  select sum(FSalary) from T_Employee

  个数count关键字:

  select count(*),count(FNamber) from T_Employee

  2.4、排序 order by

  order by放句末,升序asc(默认),降序desc

  按年龄升序排列:select * from T_Employee order by FAge asc

  按年龄降序,年龄相同的工资降序排列:select * from T_Employee order by FAge desc,FSalary desc

  where关键字(order by 放最后):select * from T_Employee where FAge>23 order by FAge desc,FSalary desc

3、高级数据过滤

  like关键字,单字符匹配“_”,多字符匹配“%”,集合匹配[],否定匹配符‘^’

  3.1、单字符匹配'_'

  搜索FName结尾为“erry”,像Jerry、Kerry:select * from T_Employee where FName like '_erry'

  3.2、多字符匹配‘%’

  搜索首字母为“T”的所有记录:select * from T_Employee where FName like 'T%'

  3.3、集合匹配‘[]’

  搜索首字母为“B”或“T”的记录:select * from T_Employee where FName like '[BT]%'

  3.4、否定匹配符‘^’

  搜索首字母不为“B”和“T”的记录:select * from T_Employee where FName like '[^BT]%'

4、空值检测

  is null关键字

  select * from T_Employee where FName is null

  select * from T_Employee where FName is not null

5、反义运算符

  !关键字,意为:不等于。NOT关键字,意为:取反。

  select * from T_Employee where FAge !=22 and FSalary !<2000

  select * from T_Employee where FAge !=22 and not(FSalary !<2000)

6、多值检测

  in关键字,between and 关键字,两数之间的值。

  select * from T_Employee where FAge in (23,22,28)

  23和27岁之间的员工:select * from T_Employee where FAge between  23 and 27

7、数据分组

  group by关键字,放在where语句之后,需要分组的所有列都必须位于group by 子句的列名列表中

  查看员工年龄段:select FAge from T_Employee group by FAge

  select FAge from T_Employee where FSubCompany = 'Beij' group by FAge

  7.1、数据分组与聚合函数

  每个年龄段的人数:select FAge ,count (*) as countofthisage from T_Employee group by FAge

   年龄段人数和排序:select FSubCompany,FAge,Count(*) as CountOfThisSubComAge from T_Employee group by FSubCompany,FAge order by FSubCompany

   sum、avg、max在分组中使用:select FSubCompany,sum(FSalary) as FSalarySum from T_Employee group by FSubCompany

  7.2、having语句

   因为where语句不能用在group by中,having语句代替了where。用于对部分分组过滤。having中不能包含未分组的列名。

  年龄段人数大于1的分组:select FAge,count(*) as CountOFAge from T_Employee gruop by FAge having count(*)>1

 

你可能感兴趣的:(sqlserver)