实验案例一:创建数据库并使用索引查询学员考试成绩
实验环境
在SQL Server 2016的服务器上,bdqn数据库保存了学员及考试信息.包含的表及结构如图5.1所示。
需求描述
1,利用索引查询考试成绩为80一90分的所有记录。
2,要求输出:学员姓名.课程名.考试曰期和学员成绩。
推荐步骤
1,在bdqn 的chengji表中学员成绩字段上.创建非聚集索引。
2,使用所创建的索引查询成绩为8O~90 分的所有记录。
创建 xueyuan 表 并插入数据
create table xueyuan
(
学员ID int primary key,
学员姓名 nvarchar(50) not null,
电话 varchar(11) not null,
出生日期 datetime not null,
)
select * from xueyuan
insert into xueyuan
select 1,'老大','110','2000/01/01' union
select 2,'老二','120','2002/01/01' union
select 3,'老三','130','2003/01/01' union
select 4,'老四','140','2004/01/01' union
select 5,'老五','150','2005/01/01' union
select 6,'老六','160','2006/01/01' union
select 7,'老七','170','2007/01/01' union
select 8,'老八','180','2008/01/01'
创建 chengji 表并插入数据
create table chengji
(
学员ID int not null,
课程ID int not null,
学员成绩 int not null,
考试日期 datetime not null,
)
select * from chengji
insert into chengji
select 1,1,60,'2000.01/01' union
select 2,2,70,'2002.01/01' union
select 3,3,80,'2003.01/01' union
select 4,4,90,'2004.01/01' union
select 5,5,30,'2005.01/01' union
select 6,6,40,'2006.01/01' union
select 7,7,50,'2007.01/01' union
select 8,8,60,'2008.01/01' union
select 9,9,70,'2009.01/0
创建 kecheng 表并插入数据
create table kecheng
(
课程ID int not null,
课程名 nvarchar(50) not null,
)
select *from kecheng
insert into kecheng
select 1,'java' union
select 2,'HTML'union
select 3,'Python'
1,利用索引查询考试成绩为80一90分的所有记录。
进入索引
select * from chengji
with (index=IX_chengji)
where 学员成绩>=80 and 学员成绩<=90
2,要求输出:学员姓名.课程名.考试曰期和学员成绩。
select xueyuan.学员姓名,kecheng.课程名,chengji.考试日期,chengji.学员成绩
from xueyuan,kecheng,chengji
where xueyuan.学员ID=chengji.学员ID