数据库实验报告Word文档格式.docx
- 文档编号:17713215
- 上传时间:2022-12-08
- 格式:DOCX
- 页数:60
- 大小:1.17MB
数据库实验报告Word文档格式.docx
《数据库实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据库实验报告Word文档格式.docx(60页珍藏版)》请在冰豆网上搜索。
ctimenumber(3,0)
createtableteach
(tnamevarchar2(8),
tsexvarchar2
(2),
cnovarchar2(3),
tdatedate,
tdeptvarchar2
(2),
constraints_pkprimarykey(tname,cno,tdept),
constraintc_fkforeignkey(cno)referencescourse(cno)
createtablescore
(snovarchar2(5),
scorenumber(5,2),
constraintsc_pkprimarykey(sno,cno),
constraintss_fkforeignkey(sno)referencesstudent(sno),
constraintcs_fkforeignkey(cno)referencescourse(cno)
例1-2:
(修改数据库表)在Student表中增加SEX(C,2)字段。
altertablestudentaddsexvarchar2
(2);
例1-3:
(修改列名)将Student表中列名SEX修改为SSEX。
altertablestudentrenamecolumnsextossex;
例1-4:
(修改数据库表)将Student表中把Sname字段修改为Sname(C,10)且为非空。
altertablestudentmodify(snamevarchar2(10)notnull);
例1-5:
(建立索引)为Score表按课程号升序、分数降序建立索引,索引名为SC_GRADE。
createindexsc_gradeonscore(cnoasc,scoredesc);
例1-6:
(删除索引)删除索引SC_GRADE。
dropindexsc_grade;
例1-7:
(建立数据库表)建立数据库表S1(SNO,SNAME,SD,SA),其字段类型定义与Student表中的相应字段(SNO,SNAME,SDEPT,SAGE)的数据类型定义相同。
createtables1(
snovarchar2(5)primarykey,
snamevarchar2(10)notnull,
例1-8:
(修改数据库表)删除成绩表Score的参照完整性约束关系。
altertablescoredropconstraintsc_pk;
例1-9:
(修改数据库表)添加成绩表Score的参照完整性约束关系。
altertablescoreaddconstraintsc_pkprimarykey(sno,cno);
例1-10:
(修改数据库表名)将数据库表S1改名为Student_Temp。
renames1toStudent_Temp;
[实验要求]
①熟悉Oracle上机环境,掌握Oracle客户端的配置;
②建立数据库表,修改数据库表结构,建立、删除索引;
[实验方法]
①按照附录中的操作步骤进行客户端的配置;
②将实验需求用SQL语句表示;
③执行SQL语句;
④查看执行结果,如果结果不正确,进行修改,直到正确为止。
[实验总结]
①SQL语句以及执行结果;
②对重点实验结果进行分析;
③实验中的问题和提高;
④收获与体会。
实验二:
数据操纵语言
在实验一的基础上,熟练掌握和使用DML语言,对数据进行增加、修改和删除操作。
2.SQL数据操纵语句:
例2-1:
(插入数据)按前面各表中的数据分别插入到教学数据库的四个数据库表中。
学生表:
insertintostudent(sno,sname,sdept,sclass,ssex,sage)values('
96001'
'
马小燕'
CS'
01'
女'
21);
96002'
黎明'
男'
18);
96003'
刘东明'
MA'
96004'
赵志勇'
IS'
02'
20);
97001'
马蓉'
19);
97002'
李成功'
97003'
03'
97004'
李丽'
96005'
司马志明'
20001'
赵薇'
课程表:
insertintocourse(cno,cname,ctime)values('
001'
数学分析'
144);
002'
普通物理'
003'
微机原理'
72);
004'
数据结构'
005'
操作系统'
64);
006'
数据库原理'
007'
DB_Design'
48);
008'
程序设计'
56);
教师表:
insertintoteach(tname,tsex,cno,tdate,tdept)values('
王成刚'
to_date('
1999-9-5'
yyyy-mm-dd'
),'
李正科'
严敏'
赵高'
2000-2-23'
刘玉兰'
马悦'
2000-9-6'
成绩表:
insertintoscore(sno,cno,score)values('
77.5'
89'
86'
82'
88'
92.5'
90'
92'
76'
69'
96'
95'
87'
91'
'
'
85'
75'
59'
58'
例2-2:
(多行插入)将表Student中在计算机系(‘CS’)的学生数据插入到表Student_Temp中。
(以上操作中,注意用COMMIT提交数据,将数据保存到数据库服务器)
insertintoStudent_Temp(sno,sname,sdept,sage)
selectsno,sname,sdept,sagefromstudent
wheresdept='
;
例2-3:
(利用查询来实现表的定义与数据插入)求每一个学生的平均成绩,把结果存入数据库表Student_Gr中。
createtablestudent_gr
(snochar(5),
avg_scoeint
insertintostudent_gr
selectstudent.sno,avg(score)
fromstudent,score
wherestudent.sno=score.sno
groupbystudent.sno;
例2-4:
(修改数据)将Student_Temp表中所有学生的年龄加2。
updateStudent_Tempsetsage=sage+2;
例2-5:
(修改数据)将Course表中‘程序设计’课时数修改成与‘数据结构’的课时数相同。
updatecoursesetctime=(selectctimefromcoursewherecname='
)
wherecname='
例2-6:
(插入数据)向Score表中插入数据(‘98001’,‘001’,95),根据返回信息解释其原因。
insertintoscorevalues('
98001'
95);
例2-7:
(插入数据)向Score表中插入数据(‘97001’,‘010’,80),根据返回信息解释其原因。
010'
80);
例2-8:
(删除数据)删除Student表中学号为‘96001’的学生信息,根据返回信息解释其原因。
deletefromstudentwheresno='
例2-9:
(删除数据)删除Course表中课程号为‘003’的课程信息,根据返回信息解释其原因。
deletefromcoursewherecno='
例2-10:
(删除数据)删除学生表Student_Temp中学号以96打头的学生信息。
(此操作后,注意用ROLLBACK回退可能更新的数据)
deletefromstudent_tempwheresnolike'
96%'
例2-11:
(删除数据)删除数据库表Student_Temp中所有学生的数据。
deletefromstudent_temp
例2-12:
(删除表)删除数据库表Student_Temp和Student_Gr。
droptablestudent_temp;
droptablestudent_gr;
对数据库表进行插入、修改和删除数据的操作。
实验三:
数据查询语言
体会SQL语言数据查询功能的丰富和复杂。
3.SQL数据查询语句:
例3-1:
(选择表中的若干列)求全体学生的学号、姓名、性别和年龄。
selectsno,sname,ssex,sagefromstudent;
例3-2:
(不选择重复行)求选修了课程的学生学号。
selectdistinctsnofromscore;
例3-3:
(选择表中的所有列)求全体学生的详细信息。
select*fromstudent;
例3-4:
(使用表达式)求全体学生的学号、姓名和出生年份。
selectsno,sname,2015-sageas出生年份fromstudent;
例3-5:
(使用列的别名)求学生的学号和出生年份,显示时使用别名“学号”和“出生年份”。
selectsnoas学号,2015-sageas出生年份fromstudent;
例3-6:
(比较大小条件)求年龄大于19岁的学生的姓名和年龄。
selectsname,sagefromstudentwheresage>
19;
例3-7:
(比较大小条件)求计算机系或信息系年龄大于18岁的学生的姓名、系和年龄。
selectsname,sdept,sagefromstudentwheresage>
18andsdeptin('
例3-8:
(确定范围条件)求年龄在19岁与22岁(含20岁和22岁)之间的学生的学号和年龄。
selectsno,sagefromstudentwheresagebetween20and22;
例3-9:
(确定范围条件)求年龄不在19岁与22岁之间的学生的学号和年龄。
selectsno,sagefromstudentwheresagenotbetween20and22;
例3-10:
(确定集合条件)求在下列各系的学生信息:
数学系、计算机系。
select*fromstudentwheresdeptin('
例3-11:
(确定集合条件)求不是数学系、计算机系的学生信息。
select*fromstudentwheresdeptnotin('
例3-12:
(匹配查询)求姓名是以“李”打头的学生。
select*fromstudentwheresnamelike'
李%'
例3-13:
(匹配查询)求姓名中含有“志”的学生。
%志%'
例3-14:
(匹配查询)求姓名长度至少是三个汉字且倒数第三个汉字必须是“马”的学生。
%马__'
例3-15:
(匹配查询)求选修课程001或003,成绩在80至90之间,学号为96xxx的学生的学号、课程号和成绩。
selectsno,cno,score
fromscore
wheresnolike'
96___'
andscorebetween80and90
andcno='
orcno='
例3-16:
(匹配查询)求课程名中包含’_’字符的课程号、课程名和学时数。
selectcno,cname,ctimefromcourse
wherecnamelike'
%\_%'
escape'
\'
例3-17:
(涉及空值查询)求缺少学习成绩的学生的学号和课程号。
selectsno,cnofromscore
wherescoreisnull;
例3-18:
(控制行的显示顺序)求选修003课程或004课程的学生的学号、课程号和分数,要求按课程号升序、分数降序的顺序显示结果。
selectsno,cno,scorefromscore
wherecnoin('
orderbycnoasc,scoredesc;
例3-19:
(组函数)求学生总人数。
selectcount(sno)as学生总人数fromstudent;
例3-20:
(组函数)求选修了课程的学生人数。
selectcount(distinctsno)fromscore;
例3-21:
(组函数)求
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据库 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)