接上————数据字典
数据字典是关系数据库系统内部的一组系统表,记录了数据库所有的基本信息包括关系模式定义,视图定义,基本索引定义,完整性约束定义,各类用户对数据库的操作权限、统计信息等。
注:插入数据时,由于未创建基本模式,将将基本表建立在初始数据库的master下面
导致选中表时,输入基本表名“student”时显示文件名无效,需要家加上前缀
按照课本P79的内容创建基本表并插入数据
插入数据
insert into
master.dbo.student
values( '201215128','陈冬','男',18,'IS',null); --单独插入数据
insert into
master.dbo.student
values --同时插入多行数据
( '201215121','李勇','男',20,'CS',null),
( '201215122','刘晨','女',19,'CS',null),
( '201215123','王敏','女',18,'MA',null),
( '201215125','张立','男',19,'IS',null);
insert into
master.dbo.course
values
( '1','数据库','5',4),
( '2','数学',null,2),
( '3','信息系统','1',4),
( '4','操作系统','6',3),
( '5','数据结构','7',4),
( '6','数据处理',null,2),
( '7','PASCAL语言','6',4);
insert into
master.dbo.SC
values
( '201215121','1',92),
( '201215121','2',85),
( '201215121','3',88),
( '201215122','2',90),
( '201215122','3',80);
对插入的数据进行检查
--插入数据之后对数据进行检查
select *
from
master.dbo.student;
select *
from
master.dbo.course;
select *
from
master.dbo.SC;
数据查询
--单表查询
--查询学生的学号和姓名
select sno,sname
from
student;
--查询学生的姓名、学号、所在系
select sname,sno,sdept
from
student ;
--查询全部列
select
*
from
student;
/*
查询经过计算的值
可以是表中的属性列,也可以是表达式
*/
select
sname,2014-sage as birthyear --as语句取别名
from
student ;
--不仅可以是算算术表达式,也可以是字符串常量、函数
--查询姓名、出生年、所在院系,并且小写字母表示系名
select
sname,'year of birth:',2014-sage,LOWER(sdept)
from
student;
--指定别名改变标题
select
sname,'year of birth:' 'year of birth',2014-sage '2014-sage',
LOWER(sdept) 'LOWER(sdept)'
from
student;
--选择表中的若干元组
--DISTINCT 取消重复的行
select
sno
from
student;
--去重后
select
distinct sno
from
student;
--好吧,没啥不同|如果没有指定distinct 关键字,默认all
select
all sno
from
student;
--=====================================
--(查询满足条件的元组)
--查询计算机科学系全体学生的名单
select sname from student
where
sdept='cs';
--查询年龄小于20的姓名及年龄
select sno, sname from student
where
sage<20 ;
--查询考试成绩不合格的学生学号
select sno from SC where
grade<60;
--between --and--和 not between and 可以查询指定范围的元组
select
sname ,sdept,sage
from
student
where
sage between 20 and 30;
--不在这个范围
select
sname ,sdept,sage
from
student
where
sage not between 20 and 30;
--谓词IN 用来查找指定范围的元组
--查询CS\MA\IS、的姓名和性别
select
sname,ssex
from
student
where
sdept in ('CS','MA','IS');
--NOT IN
select
sname,ssex
from
student
where
sdept not in ('CS','MA','IS');
like \\in
--字符匹配LIKE
--%表示任意长度的字符串’_'表示任意字符
--查询学号为201215121的详细情况
select
*
from
student
where sno like '201215121';
--LIKE与‘=’等价
select sname ,sno, ssex
from
student
where
sname like '刘%';
select sname ,sno, ssex
from
student
where
sname like '欧阳_';
select sname ,sno, ssex
from
student
where
sname like '_阳%';
select sname ,sno, ssex
from
student
where
sname not like '_阳%';
select Cno ,Ccredit
from
course
where
Cname like 'DB\_Design' escape '\';
--'\'表示换码字符使’_'不再是通配符,而是文件本身的数据名
--涉及空值的查询
select sno,Cno
from
SC
where Grade is null; --这里的null不能用“='代替
select sno,Cno
from
SC
where Grade is not null;
/*
运算符and和or可以用来链接查询多个条件
and>or
可以用括号改变优先级
*/
select sname
from student
where sdept ='CS' and sage <20;
--谓词in等同于多个谓词or 的缩写
select sname,ssex
from
student
where sdept = 'CS' or sdept = 'MA' or sdept = 'IS';
特殊字句
--特殊字句
select sno,grade
from SC
where Cno = '3'
--order by Grade desc; --降序
order by Grade asc; --默认升序
--多个元素进行序列安排
select *
from student
order by sdept,sage desc;
聚集函数
--聚集函数
--查询学生总人数
select COUNT(*)
from student ;
--查询选修了课程的人数
select COUNT(distinct sno)
from SC;
--计算系=选修一号课程平均成绩
select AVG(grade)
from SC
where Cno = '1';
--查询选修1号课程的学生的最高分数
select MAX(grade)
from SC
where Cno = '1';
--查询某学生选修课程的总学分数
select SUM(Ccredit)
from SC ,course
where sno = '201215012' and SC.Cno = course.Cno ;