数据库上机指导

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

char(20) not null)
a.利用 insert 语句向其中插入一条新的记录:
(‘0007’,‘张三’,‘男’,‘1982-3-21’,‘团员’,’计
算机系’)
insert into pupil
values(‘0007’,‘张三’,‘男’,‘1982-3-21’,‘团员’,
‘计算机系’)
b.利用 update 语句将编号为‘0004’的学生 polity 改为
23
4、 通过 Transact_SQL 语句创建一个视图,计算各个班级的 各门课程的平均分。 Create view v_avggrade As Select substring(pupil.sno,1,len(pupil.sno)-2) as 班级, Cname as 课程名称, Avg(grade) as 平均分 From pupil,sc,grade Where pupil.sno=sc.sno and o=o Group by substring(pupil.sno,1,len(pupil.sno)-2), Cname
4
5
b.
c.保存并命名为 pupil 即可。 4.将 pupil 表的 sno 设置为主键。
5、 为 sname 字段设置惟一性约束。 步骤:
6
7
6.为 ssex 和 birthday 设置检查性约束,要求 ssex 只能为‘男’或‘女’,birthday 应该大于
‘1987-1-1’。 步骤:
And cname=’ 高等数学’ 6.通过 Transact_SQL 语句创建一个视图,查询的数据为 99521 班学生的考试成绩。 Create view v_grade99521 As
24
Select pupil.sno,sname,cname,grade From pupil,sc,course Where pupil.sno=sc.sno and o=o and
《数据库原理与技术》上机实验安排
实验 1 SQL server 2000 的安装和常用工具的使用
【实验内容】 1.练习安装 SQL SERVER 2000 及卸载 SQL SERVER 2000。(注意 WINDOWS 登录模式和
混合登录模式的区别) 2. 在企业管理器中创建一个新的 SQL Server 服务器组,作为 Microsoft SQL Servers 的子对 象,名称为 “NewGroup” 3. 在计算机上再安装一个 SQL Server 命名实例,实例名为 “people”。 使用企业管理器在 NewGroup 服务器组下创建一个新的 SQL Server 注册,使它连接 people 实例.
18
select @personcount=(select count(distinct sno) from sc) from sc where grade<=@maxline real and grade>=@minline return @personcount end
19
3.如何通过企业管理器和 Transact_SQL 语句对视图进行创建、 修改和删除?
17
【实验内容】
实验 4 Transact—SQL 程序设计
1. 编写一个用户自定义函数 fun_avggrade,要求根据输入
的班级号和课程号,求此班级此课程的平均分。
Create function fun_avggrade
(@classnumber as char(10),@coursenumber as char(10))
1
实验 2 数据库及表的创建和管理
【实验内容】 1、使用企业管理器创建名为 student 的数据库,并设置数据库主文件名为 syudent_data,大小 为 10MB;日志文件为 student_log,大小为 2MB.其他参数取默认值。 步骤:a.
b.
2
c.
3
d.
2. 使用 Transact—SQL 语言创建一个 student1 数据库,主文件逻辑名为 student1_data,物理 文件名为 student1.mdf,为 10MB,增长速度为 10%;数据库的日志文件逻辑名为 syudent1_log, 物理文件名为 student1.l件存 放路径为 c:\data 文件夹下。 Create database student1 On primry (name=student1_data, filename=c:\data\student1.mdf, size=10, maxsize=unlimited, filegrowth=10%) log on (name=student1_log, filename=c:\data\student1.ldf, size=1, maxsize=5, filegrowth=1)
28
birthday as 出生日期, polity as 政治面貌 from pupil where sno=@stunum end 3. 创建一个存储过程 stu_age,根据输入的学生姓名,计算该学生 的年龄。 Create procedure stu_age @stuname char(10) as if not exists(select * from pupil where sname=@stuname) print ‘查无此人!!!!!’ else begin declare @age int select @age=floor(datediff(day,birthday,getdate())/365) from pupil where sname=@stuname end
f. 查询‘刘’姓学生的信息 select * from pupil where sname like ‘刘%’
g. 查询 polity 为’团员’或’党员’的学生信息 select * from pupil where polity in(‘团员’,’ 党员’)
h. 查询各门课程的选课人数 select cno ,count(sno) from sc group by cno
a. 查询所有学生的基本信息,并按出生日期升序排列
select * from pupil order by polity
15
b. 查询女同学的信息和女同学的人数 select * from pupil where ssex=’女’ compute count(sno)
c. 查询所有男同学的年龄 select year(getdate())-year(birthday) as 年龄 from pupil
returns real
begin
declare @avgresult
select @avgresult=avg(grade)
from sc
where substring(sno,1,len(sno)-2)= @classnumber and
cno=@coursenumber
return @avgresult
查询某学生的基本信息。
26
27
代码如下: create procedure stu_info @stunum char(10) as if not exists(select * from pupil where sno=@stunum)
print ‘查无此人!!!!!’ else
begin select sno as 编号, sname as 姓名, ssex as 性别,
left(pupil.sno,5)=’99521’
25
【实验内容】
实验 5 存贮过程
1. 创建一个存储过程 stugradeinfo,查询班级、学号、姓名、性别、
课程名称、分数
create procedure stugradeinfo
as
select 班级=substring(pupil.sno,1,len(pupil.sno)-2),
通过企业管理器: 创建步骤:
20
21
最后保存该视图即可。 修改步骤:
进入该界面重新修改该视图即可。
22
删除步骤:
通过 Transact_SQL 语句: 创建:create view 视图名
[with encryption] as select_statement [with check option] 修改:利用 alter view 视图名 删除:利用 drop view 视图名
d. 所有选课学生的姓名、选修课程名及成绩 select sname,cname,grade from pupil,sc,course where pupil.sno=sc.sno and o=o
e. 不及格学生的姓名 select distinct sname from pupil,sc,course where pupil.sno=sc.sno and o=o and grade<60
i. 查询缺少成绩的学生的学号及课程号 select sno,cno from sc where grade is null
j. 查询与‘刘成’同一个系的学生情况
16
select * from pupil where sdept in(select sdept from pupil where sname=’ 刘成’)) k. 查询选修了课程名为‘MIS’的学生的学号和姓名 select sno,sname from pupil where sno in(select sno from sc where cno in(select cno from course wnere cname=’MIS’))
‘党员’:
update pupil
set polity=’党员’
where sno=’0004’
a. 利用 delete 语句将‘186-1-1’以后出生的女同学记录删除。
Delete from pupil
Where polity>1986-1-1 and ssex=’女’
2. 针对 pupil、sc、course 三张表完成以下查询:
end
2. 编写一个用户自定义函数,完成以下功能:根据两个
输入参数(成绩上限和成绩下限),求 sc 数据表中满足输
入条件的学生人数。
Create function fun_sumren
(@maxline real,@minline real)
returns int
begin
declare @personcount as int
10
11
12
13
最后保存该关系图即可。
14
【实验内容】
实验 3 数据查询及维护
1. 已知一个名为 pupil 的表:
(sno char(6) not null,sname char(10) not null,ssex char(2)
not null,birthday datetime not null, polity char(20),sdept
3. 在 student 数据库中创建一个名为 pupil 的表,要求:
步骤:
(sno char(6) not null,sname char(10) not null,ssex char(2) not null,birthday datetime not null, polity char(20))
pupil.sno as 学号,
sname as 姓名,
ssex as 性别,
cname as 课程名称,
grade as 分数
from pupil,sc,course
where pupil.sno=sc.sno and o=o
2. 利用企业管理器创建一个存储过程 stu_info,根据传入的编号,
8
9
7. 。 为 polity 字段设置默认约束,值为‘群众’
8 再创建一个学生选课表 sc(sno char(6) not null,cno char(10) not null,grade real). 为 sc 表创建外键约束,把 sc 表的 sno 和 pupil 表的 sno 关联起来,在这两个表之间 创建一种制约关系。 步骤:
5. 通过 Transact_SQL 语句创建一个视图,显示‘高等数学’未 过的学生的信息。 Create view v_gradenotpass As Select pupil.sno,sname,cname,grade From pupil,sc,course Where pupil.sno=sc.sno and o=o and grade<60
相关文档
最新文档