第七天课(5月12号)
SQL Server 常见版本
* 企业版(Enterprise Edition)
* 标准版(Standard Edition)
* 个人版(Personal Edition)
* 开发者版(Developer Edition)
一、表
– 由行和列组成
– 每列又称为一个字段,每列的标题称为字段名
– 一行数据称为一个或一条记录,它表达有一定意义的信息组合
– 一个数据库表由一条或多条记录组成,没有记录的表称为空表。
– 每个表中通常都有一个主关键字(也叫主键),用于惟一地确定一条记录。主键不允许空值。不能存在具有相同的主键值的两个行
二、索引
索引是根据指定的数据库表列建立起来的顺序。它提供了快速访问数 据的途径,并且可监督表的数据,使其索引所指向的列中的数据不重复。
三、视图
是一个虚拟的表,在数据库中并不实际存。 视图是由查询数据库表产生的,视图可以用来控制用户对数据的访问,并能简化数据的显示,即通过视图只显示那些需要的数据信息。
四、事务
1、存储过程:事务的集合,存放在服务器中,只需在客户端发送一条调用存储过程的指令,即可以执行多条SQL命令
2、触发器:自动执行一组SQL命令,SQL内置触发器,级联更新,级联删除。
五、
如果存在分组语句,则在SELECT中必须显示的查找,对应的分组字段或用聚合函数进行汇总运算。
必须有group by存在才能使用Having;
Having语句对分组后的数据进行条件筛选;
Having中可以有聚合函数;
数据库的备份恢复,表的导入导出
基本语句练习
*select * from student where sname='张三'
select * from student where sname like '张%'
select * from student where sname like '张%' and shome='北京'
select * from student where shome in('北京','上海','天津')
select sno as '学号',cno as '课程号' from sc where grade between 60 and 70
select cno,avg(grade)
from sc
group by cno
--统计共有多少学生信息
select count(sno)
from student
--统计年龄大于20岁的学生有多少个
select count(sno)
from student
where sage>20
--统计出生日期在1980年至1982年的学生人数
select count(sno)
from student
where sage between 19 and 20
--统计学生的平均成绩
select avg(grade)
from sc
--统计学号为001所有课程中的最高分数
select max(grade)
from sc
where sno='20031001'
--统计课程号为G001的最高分数
select min(grade)
from sc
where cno='1'
--统计课程号为G001的所有课程总分
select sum(grade)
from sc
where cno='1'
--统计每门课程的平均成绩
select cno, avg(grade)
from sc
group by cno
--统计每门课程的平均成绩并按照从大到小显示
select cno,avg(grade)
from sc
group by cno
order by avg(grade) desc
--显示有两门以上不及格的学生的学号
select sno
from sc
where grade<60
group by sno
having count(grade)>=2
--查询英语课的总成绩
select sum(grade)
from sc
where cno=(select cno from course where Cname='C语言')
--查询003班英语课的总成绩
select sum(grade)
from sc
where sno in(select sno from student where sclass='一班')
and cno=(select cno from course where Cname='C语言')
--查询003班张三的英语成绩
select grade
from sc
where sno=(select sno from student where sclass='一班'and sname='张三')
and cno=(select cno from course where Cname='C语言')
--查询003班的各门课程总成绩
select sum(grade),cno
from sc
where sno in(select sno from student where sclass='一班')
group by cno
--查询003班张三的平均成绩
select avg(grade)
from sc
where sno=(select sno from student where sclass='一班'and sname='张三')