关于数据库的一些小知识

             关于数据库的一些小知识       

1.sql语句执行顺序

   having是在聚合函数之前执行的
   select fclassid,count(*) as ' 班级人数',
  avg(innull(fage,0))  as '平均年龄' from newstudent
   group by fclassid
   having count(*)>3
  比如这个语句having 后就不能用count(*)的别名。即不能用“班级人数”。 
 
 
2.insert 增
 语法:insert into 表名(列名1,列名2.....)
           value(值1,值....  )
 
 如果要对表中除自动增长列外的所有列进行赋值,那么表名后的(列名)
 可以省略。
 
特殊情况下,非要对自动增长列进行手动插入值可以进行以下设置
SET IDENTITY_INSERT 表名 ON
 
批量插入数据第一种的方法:
语法:
insert into 表名(列名1,列名2......)
select  结果集
 
第二种批量插入的方法:
select * from 表名。
在列名后加上 into 新表名。
也可以在后面加where条件。 
 
 
3.update 改
   语法:
    update 表名 set 要更新的列名=新值[,要更新的列名=新值]
    后面也可以加where条件。
 
    update newstudent set classid=1       (更新一列)
    update newstudent set classid=1,stuisdel=2         (更新多列)
 
   部分更新,就加where条件
 
 
 
4.delete 删
 语法:
 delete from 表名
  delete from student(where sage>20)
 
truncate table student 的作用与delete from student一样。
都是删除student表中的全部数据。
不过,truncate语句非常高效。
truncate语句会把表中的自动编号重置为默认值。
truncate语句不触发delete触发器。
 
 
 
 
 
5.select 查
select * from  geqk                                          查询表中所有的数据
select 列名1,列名2... from 表名                      查询表中部分列
 
重命名列名
第一个方法:用as
select stuid as [学号],stuname,stuaddress from student
第二个方法:空格后直接写别名
select stuid 学号,stuname 姓名 from student
第三个方法:列名=别名
select stuid=学号,stuname=姓名 from student
 
添加一列,列可以进行计算
 
 
 
 
 
6.唯一约束
  语法:
  alter table 表名 add constraint 约束名 约束的关键字(列名)
   alter table tblcalss add constraint
   UQ_tabclass_tclassdesc
   unique (tclassdesc)
 
  查看:右键。设计。索引/键。
 
 
 
 
7.默认值约束
 语法:
 alter table 表名 add constraint 约束名 约束的关键字(默认值)
 for (列名)
alger table tblclass add constraint DF_tblclass_tclassdesc default 
(N'开班')for tclassdesc
 
8.check约束
  语法:
alter table 表名 add constraint 约束名 约束的关键字(判断的内容)
 
添加一个检查tclassname字段中的数据必须大于等于3的约束
alter table tblcalss add constraint CK_tblcalss_tcalssname
check(len(tclassname>3))
  
9.日期函数
  getdate():取得当前时间
    
  返回日期类型
     dateadd(datepart,number,date),计算增加以后的日期
   select dateadd(day,3,getdate())           当前时间下三天后的日期 
 
返回int类型
select datediff(month, 2013-05-06,2011-1-2)
select *,datediff(year,fbirthday,getdate()) 
as '真实年龄' from mystudent                       求真实年龄。。。。
 
datepart():返回一个日期的特定部分
month()、year()、day()
select distinct year(fbirthday) as [year] 
from mystudent
order by [year]
 
1991年出生学生的个数
select * from mystudent 
where year(fbirthday)=1996
 
 
 
 
10. column,constrain
添加列语法:
alter table 表名 add 列名 类型
注意:不要忘了类型
 
修改列语法:
alter table 表名 alter  column 列名 类型
注意:修改时不要忘了column
 
添加主键约束
alter 表名 add constrain 约束名 primary key (主键列)
注意:不可忘了最后的(主键列)
 
删除主键约束
alter table 表名 drop constraint 约束名
 
 
 
11.删表,删库
  drop table
  drop database
  
12.union
     合并结果集
要求两个结果集的列数必须相同
且两个结果集对应的数据类型必须相同
 
 
13.在聚合函数中,为NULL的记录不参与计算。
     MAX    MIN    AVG    SUM   
      COUNT(*)表示记录的数量,包括对空值行、重复行的统计。
 
 
 
14.模糊查询
   用like关键字
   %表示0个或多个任意字符
    select  * from myfridens
    where fname like'杜'
 
 
 
15.空值处理
  在数据库中null不是表示无或者没有的意思,而是不知道
  NULL与其他值计算还是NULL.
 
在数据库中null用is null或is not null判断
 语法:
 isnull(字段,如果为空返回的值)
 
列出1班所有学生的姓名和语文成绩
select fname,isnull(fchinese,0) from student
where fclassid=1
 
 
 
16.order by排序
 
    可以有多个排序依据,用逗号分开即可。
    asc(升,默认的)
    desc(降序,非默认)
 
     查询班级中数学成绩最高分数
     select top 1 fmath from mystudent
     order by fmath desc
    注意:order by 子句一般放到SQL的最后边
 
 
 
 
 
17group by 分组
   分组之后,select后只能显示分组依据和聚合函数。
  如果一个SQL语句中有group by和聚合,那么执行顺序是先分组,然后                      在第一组中执行聚合函数。
   group by句子是放到where句子之后 的。
  没有出现在GROUP BY子句中的列是不能放到SELET语句后的列名列表中的,当然聚合函数是除外的。
 
所有学生中,男女生的人数是多少?
select fgender,count(*) as '人数' from student
group by fgender
 
 求数学平均成绩在90分以上的班级
 select  fclassid,avg(isull(fmath,0)) as '数学平均成绩'
 from mystudent
group by fclassid
having avg(isnull(null,0))>90
 
 
 

select sum(case when OpenTime is null or OpenTime='' then 0 else 1 end ) as 'ConsultNum',
sum(case when EnterFirstSessionTime is null or EnterFirstSessionTime='' then 0 else 1 end ) as 'UserRequestNum',
sum(case when EnterProcessTime is null or EnterProcessTime='' then 0 else 1 end ) as 'EnterProcessNum',
sum(case when OpenTime is null or OpenTime='' then 0 else 1 end )-sum(case when EnterProcessTime is null or EnterProcessTime='' then 0 else 1 end ) as 'IVRNum',
COUNT(distinct(UserNick)) as 'CustomerServiceNum',
MAX(LineUpNumbers) as 'MaxLineUpNum',
datediff(s,'00:00:00',MAX(EnterProcessTime-EnterFirstSessionTime))as 'MaxLineUpTime'
,SUM(datediff(s,EnterFirstSessionTime,EnterProcessTime))/sum(case when EnterFirstSessionTime is null or EnterFirstSessionTime='' then 0 else 1 end ) as 'AvgLineUpSecond'
,SUM(case when ResopneTime!='' then datediff(s,EnterProcessTime,LastReplyTime) else 0 end )/sum(case when EnterProcessTime is null or EnterProcessTime='' then 0 else 1 end ) as 'AvgChatSecond'
,SUM(case when EnterProcessTime!='' then datediff(s,EnterProcessTime,EndTime) else 0 end )/sum(case when EnterProcessTime is null or EnterProcessTime='' then 0 else 1 end ) as 'AvgBackSecond'
from CustomerServiceData where opentime >='2016-06-19 00:12:14.000' and opentime<='2016-06-29 23:59:59.163'

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2013-06-22 22:18  公子若不胖天下谁胖  阅读(351)  评论(0编辑  收藏  举报