一、实验目的
1.熟悉SQL Server数据库中的数据查询、统计、分组、排序等操作。
2.掌握使用查询分析器对数据进行简单查询。
二、实验工具
计算机(Pentium4,128M内存,Intel兼容机、Windows XP环境)、SQLServer2005软件, U盘(学生自备)。
三、实验原理
启动数据库服务软件SQL Server 2005的查询分析器,用SELECT语句对表进行简单查询操作,整个查询过程只涉及到一个表,是最基本的查询语句。
四、 实验内容与步骤
打开数据库SQL Server 2005的查询分析器,利用以前建立的xs,kc,xs_kc进行查询。
(1)利用企业管理器创建数据库xscj
(2)打开数据库SQL Server 2005的查询分析器, 分别创建三张表xs,kc,xs_kc
创建表xs:
use xscj
go
create table xs
(sno char(7) not null primary key,
sname char(8) not null,
ssex char(2) null,
sbirth smallint null,
sdept varchar(20) null,
scredit tinyint null
)
创建表kc:
use xscj
go
create table kc
(cno char(5) not null primary key,
cname varchar(20) not null,
ctime tinyint null,
ccredit int null
)
创建表xs_kc:
use xscj
go
create table xs_kc
(sno char(7) not null,
cno char(5) not null,
grade int null,
primary key(sno,cno)
)
(3)用INSERT语句分别向表xs,kc,xs_kc中插入记录
xs:
use xscj
go
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001101','刘致朋','男',18,'IS',8)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001102','李宏','男',19,'CS',6)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001103','黄方方','女',18,'CS',7)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001104','田莉莉','女',17,'CS',4)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001105','王毅','男',20,'IS',5)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001106','李建国','男',19,'MA',9)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001107','刘涛','男',18,'MA',8)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001108','郝露','女',17,'CS',7)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('001109','吴杭','男',18,'IS',5)
INSERT INTO xs(sno,sname,ssex,sbirth,sdept,scredit)
VALUES('0011010','李进','男',19,'CS',6)
kc:
use xscj
go
INSERT INTO kc(cno,cname,ctime,ccredit)
VALUES('101','计算机基础',4,4)
INSERT INTO kc
VALUES('102','离散数学',3,6)
INSERT INTO kc
VALUES('103','数据结构',6,6)
INSERT INTO kc
VALUES('104','数据库技术与应用',5,5)
INSERT INTO kc
VALUES('105','c语言程序设计',6,6)
xs_kc:
use xscj
go
INSERT INTO xs_kc(sno,cno,grade)
VALUES('001101','101',85)
INSERT INTO xs_kc
VALUES('001102','101',72)
INSERT INTO xs_kc
VALUES('001102','102',83)
INSERT INTO xs_kc
VALUES('001103','102',65)
INSERT INTO xs_kc
VALUES('001103','104',58)
INSERT INTO xs_kc
VALUES('001104','105',66)
INSERT INTO xs_kc
VALUES('001105','103',89)
INSERT INTO xs_kc
VALUES('001106','104',78)
INSERT INTO xs_kc
VALUES('001107','103',48)
INSERT INTO xs_kc
VALUES('001107','104',96)
INSERT INTO xs_kc
VALUES('001107','105',47)
INSERT INTO xs_kc
VALUES('001109','103',77)
INSERT INTO xs_kc
VALUES('001109','105',85)
2.简单查询
(1)查询全体学生的详细记录;
select * from xs
(2)查询所有选修过课的学生的学号;
select distinct sno from xs_kc
(3)查询考试成绩不及格的学生的学号;
select distinct sno from xs_kc where grade<60
(4)查所有姓王的学生的姓名、学号和性别;
select sno,sname,ssex from xs where sname like '王%'
(5)查 “计算机基础” 课程的课程号和学分;
select cno,ccredit from kc where cname = '计算机基础'
(6)查询选修了101号课程的学生的学号及其成绩,查询结果按分数降序排列;
select sno,grade from xs_kc where cno = '101' order by(grade) desc
(7)查询选修了102号课程的学生最高分数;
select MAX(grade) from xs_kc where cno = '102'
(8)查询xs表中姓名、系别和总学分,只返回结果集的前5行;
select top 5 sname,sdept,scredit from xs