数据库系统实验解答.docx
《数据库系统实验解答.docx》由会员分享,可在线阅读,更多相关《数据库系统实验解答.docx(19页珍藏版)》请在三一办公上搜索。
1、数据库系统实验解答实验一 用SQL语句完成实验内容2-5的各种操作。 * 实验内容2: 1)CREATE DATABASE students 具体格式: CREATE DATABASE students ON PRIMARY ( NAME=students_Data, FILENAME=E: students_Data.MDF, SIZE=10, MAXSIZE=300, FILEGROWTH=5) LOG ON ( NAME=students_Log, FILENAME=E: students_Log.MDF, SIZE=10, MAXSIZE=200, FILEGROWTH=2) ) 2)
2、DROP DATABASE Students 3)CREATE DATABASE bookdb 4) sp_helpdb bookdb 项目 内容 数据库所有者 8C62E96C274F451Administrator 数据库大小 1.24 MB 可用空间 0.51MB 数据文件名 Bookdb_data 数据文件物理存放位置 C:Program FilesMicrosoft SQL ServerMSSQLdatabookdb_data.mdf 日志文件名 Bookdb_log 日志文件物理存放位置 C:Program FilesMicrosoft SQL ServerMSSQLdataboo
3、kdb_log.LDF * 实验内容3: 1) USE bookdb create table book (Book_id int primary key,Book_name char (50) not null,Price float not null,Publisher char (26) not null,City char (20) not null) 2) create table Orderform (Order_id int primary key,Book_id int not null,Book_number int not null,Order_date datetime
4、not null,Client_id int not null) 3) create table Authors (Author_id int primary key,Author_name char (8) not null,Address char (50) ,Telphone char (15) ) 4) create table Clients (Client_id int primary key,Client_name char (8) not null,Address char (50) not null) * 实验内容4: 1)alter table book add autho
5、r_id int not null default 2) alter table book drop city 3) alter table book add constraint unqBkname UNIQUE(Book_name), constraint chkPrice check 添加约束,要求Book_name 取值不能重复,Price的取值在0100之间。 * 实验内容5: create index ix_authors on authors(author_name) Create index ix_Orderform on Orderform (Order_id asc,Ord
6、er_date desc) with (FILLFACTOR=10) 创建所有学生的基本信息和选课信息的视图S_C_all。 CREATE VIEW S_C_all AS SELECT student.*, o,sc.score FROM student,sc WHERE student.sno=sc.sno 基于上述视图S_C_all查询选修C2课程的学生姓名及其所在系。 select sno,sdept from S_C_all where cno=C2 创建一个视图CS,包含选课成绩及格的学生编号、所选课程号和该课程成绩。 create view CS as select sno,cno
7、,score from sc where score=60 创建一个基于视图的视图S_CS,基于中建立的视图,定义一个包CS含学生编号、学生所选课程数目和平均成绩的视图。 create view S_CS (sno,cnt,avg_sc) as select sno,count(*),avg(score) from CS group by sno 创建带表达式的视图,由学生姓名、所选课程名、所有课程成绩再多5分这几个属性组成。 create view BT_SC(sname,cname,newscore) as select student.sname, ame,sc.score+5 from
8、 student,course,sc where student.sno=sc.sno and o=o 插入元组到视图CS中。 insert into CS values 删除视图CS。 drop view CS 实验二 1、student表中插入元组(99006, 郭启芳, 女, 15, CS) ,给出操作结果,分析原因并进行修改。 INSERT Student(sno,sname,ssex,sage,sdept) VALUES(99006, 郭启芳, 女, 15, CS) 显示: 消息2627,级别14,状态1,第1 行 违反了PRIMARY KEY 约束PK_STUDENT。不能在对象d
9、bo.STUDENT 中插入重复键。 语句已终止。 修改:将学号改成与已有学号不会重复的值。 2、求学生的选课人数和学生平均成绩,并把结果存入数据表tot_score。使用SELECT INTO和INSERT INTO两种方法实现。 SELECT INTO语句 SELECT cno,count(*) as cnt_sc,avg(score) as avg_sc INTO tot_score FROM SC GROUP BY cno INSERT INTO语句 create table tot_score (cno char(2),cnt_sc int,avg_sc float) INSERT
10、INTO tot_score SELECT cno,count(*),avg(score) FROM SC GROUP BY cno 3、Management Studio:右击SC表,在菜单中选择“打开表”,在列出的数据中直接修改 用T-SQL语句: UPDATE SC SET score=score+3 WHERE sno=99002 AND cno=C2 4、 1)查询IS系所有学生的姓名,并按学号升序排列。 select sname from student where sdept=IS order by sno 2)查询学分为2或3,且课程名称以”数”开头的课程编号。 select
11、cno from course where cname like 数%and (credit=2 or credit=3); 3)查询所有学生的编号及其所选课程的平均成绩,按平均成绩降序排列。 select sno,avg(score) from sc group by sno order by avg(score) desc 4)查询至少选修了三门课程的学生编号。 select sno from sc group by sno having count(*)=3 5)查询所有选了“信息系统”的学生编号。 select sno from sc,course where o=o and ame=
12、 信息系统 6)求出选择了同一课程的学生对。 select distinct x.sno,y.sno from sc x, sc y where o=o and x.snoy.sno 7)查询选修了学号99002的学生所选的某门课程的学生编号。 select distinct y.sno from sc x, sc y where o=o and x.sno=99002 and y.sno99002 8)分别用等值连接和谓词IN两种方式查询姓名为张立的学生所选课程的编号和成绩。 等值连接: select o,sc.score from student, sc where sname=张立 an
13、d sc.sno=student.sno 谓词IN: select cno,score from sc where sno in ( select sno from student where sname=张立) 9)查询选修“数据库”课程的成绩比姓名为“张立”学生高的所有学生的编号和姓名。 select sno,sname from student where sno in ( select c1.sno from sc as c1,sc as c2 where c1.scorec2.score and o=o and c2.sno=(select sno from student wher
14、e sname=张立) and o=(select cno from course where cname=数据库) ) 10)查询学分比课程“数学”多的课程的名称。 select ame from course as c1, course as c2 where c1.creditc2.credit and ame=数学 5、用T-SQL语句完成下面的要求: 用WHILE语句实现计算10000减1、减2、减3,一直减到50的结果,并显示最终结果。 DECLARE x int,y int SELECT x=10000,y=1 WHILE (y=0且=0 AND val= select * fr
15、om course where credit ( select credit from course where o=o) 3)查询没有学生选的课程名称。 select cname from course where cno not in ( select cno from sc ) 4)找出选修课程“数据库”的学生选修的所有课程名称。 select cname from course where cno in ( select cno from sc where sno in ( select sno from course,sc where cname=数据库 o=o) ) 5)查询选修了
16、99002学生选修的所有课程的学生编号。 select distinct sname from student where not exists ( select * from sc as c1 where c1.sno= 99002 and not exists ( and select * from sc as c2 where c2.sno=student.sno and o=o and student.snoc1.sno) ) 6)实现集合交运算,查询既选修课程“数学”又选修课程“信息系统”的学生编号。 select x.sno from sc as x,sc as y where (
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 系统 实验 解答

链接地址:https://www.31ppt.com/p-3559956.html