声明:此文档只作为学习参考,不得用作它途!实验一了解ORACLE环境,使用ORACLE数据库实用工具1.目的要求:了解ORACLE数据库的各个常用工具软件2.实验内容:在ORACEL数据库下使用SQL*PLUS ,SQL*PLUS Worksheet,PL/SQL Developer 工具,企业管理器等实用工具与Oracle交互。
并在企业管理器中观察ORACLE的底层存储原理。
在PL/SQL Developer中书写简单的SQL语言。
3.主要仪器设备及软件1)PC2)ORACLE数据库实验二熟悉SQL语言1.目的要求在SQL*PLUS或PL/SQL Developer工具中编写SQL语句2.实验内容在ORACLE 数据库中定义用户,给用户赋权限,创建,修改和删除表格,视图等数据库对象,并向表格中插入,修改和删除数据。
体会SQL语言中ORACLE的“方言”。
对自己建立的表做查询:包括单表查询,多表查询,嵌套查询,分组查询,相关查询3.主要仪器设备及软件1)PC2)ORACLE数据库自定义用户:create user taozi identified by taozi;给用户赋DBA权限:grant dba to taozi;创建表格 student,sc,course:Create table student(sno char(10) primary key,sname varchar(20) not null,sage smallint,ssex char(2),sdept varchar(20));Create table course(cno char(10) primary key,cname varchar(50) not null,credit smallint);Create table sc(sno char(10),cno char(10),grade smallint,primary key(sno,cno));创建视图:create view oracle as (select sno,sname,sage from student);删除视图:delete oracle;为student 表增加一列 jiguan: alter table student add jiguan varchar(10);删除jiguan 列:alter table student drop column jiguan;删除student 表结构:drop table student;插入数据:insert into student values('004','AA','21','f','MA');insert into student values('005','BB','20','m','CS');insert into student values('006','CC','20','m','E');修改数据:update student set sname='DD' where sno='006';删除数据:delete student where sno='004' or sno='005' or sno='006';单表查询select * from student where sno='009';多表查询:查询001号学生选修课成绩大于或等于85分的课程名及成绩select sname,cname,grade from student,sc,course where student.sno='001' and grade>=85;嵌套查询:查询选修了软件工程学生的学号select sc.sno from sc,course where o=o ando=(select cno from course where cname='软件工程');分组查询:查询所有学生的姓名、所选课程的课程名,课程号和成绩,并按成绩降序和课程号升序排序。
select sname,cname, o,grade from student sc,course student where sc.sno=s.sno and o=o order by grade desc,cno asc;相关查询:查询所有学生都选修了的课程名及课程号select cno from sc group by cno having count(*)>=(select count(*) from student );实验三实现简单的PL/SQL程序1.目的要求编写简单的PL/SQL程序,熟悉PL/SQL编程环境2.实验内容在SQL*PLUS或PL/SQL Developer工具中编写PL/SQL的简单程序,熟悉PL/SQL 的编程环境和代码结构。
实现与Oracle数据库交互,并捕获和处理常见系统异常和用户自定义异常。
3.主要仪器设备及软件1)PC2)ORACLE数据库预定义异常处理查询课程“java”的课程号,若未成功,则提示异常v_cno varchar(40);beginselect cno into v_cno from course where cname='java';dbms_output.put_line('java对应的课程号是:'||v_cno);exception when no_data_found thendbms_output.put_line('该课程不存在。
');end;查询既选修了001又选修了002课程的学生的学号,未成功则提示:该学生不存在。
declarev_sno varchar(40);beginselect sno into v_sno from sc where cno='001'and sno=(select sno from sc where cno='002');dbms_output.put_line('既选修了001又选修了002课程的学生:'||v_sno);exception when no_data_found thendbms_output.put_line('ERROR:该学生不存在!');end;查询“周源”的学号,若未成功,则插入‘周源’信息:学号59,性别F,学院CS 查询既选修了001又选修了002课程的学生的学号,未成功则提示:该学生不存在。
declarev_sno varchar(40);beginbeginselect sno into v_sno from student where sname='DD';dbms_output.put_line('DD的学号是:'||v_sno);exception when no_data_found thendbms_output.put_line('error:该学生不存在,插入该学生信息');insert into student(sno,sname,ssex,sdept) values('59','DD','F','CS');commit;end;select sno into v_sno from sc where cno='001'and sno=(select sno from sc where cno='002');dbms_output.put_line('既选修了001又选修了002课程的学生:'||v_sno);exception when no_data_found thendbms_output.put_line('ERROR:该学生不存在');end;exception when no_data_found thendbms_output.put_line(sqlerrm);end;实验四在PL/SQL中使用游标1.目的要求在PL/SQL中使用无参数的游标和带参数的游标处理结果集2.实验内容在PL/SQL程序中使用游标来处理结果集,分别使用fetch, while和For循环来遍历查询结果集中的每一条记录,并对这些记录进行判断和处理,将处理的结果格式化打印出来。
使用带参数的游标来传递查询条件,使程序更加灵活实用。
实现下面功能:使用游标实现:将某门课程高于平均分的学生的姓名,课程名,成绩格式化输出。
3.主要仪器设备及软件1)PC2)ORACLE数据库--将某门课程高于平均分的学生的姓名,课程名,成绩格式化输出。
declarecursor c1 isselect s.sno,sname,o,cname,grade from student s,course c,scwhere s.sno=sc.sno and o=o and s.sno in(select sno from sc where grade>(select avg(grade) from sc wherecno=o)) order by s.sno asc;T c1%rowtype;a_sno varchar2(20):='null';beginopen c1;dbms_output.put_line(rpad(' ',15,' ')||rpad('=',30,'=')||rpad(' ',15,' '));dbms_output.put_line(rpad(' ',22,' ')||rpad('考试成绩信息',20,' '));dbms_output.put_line(rpad(' ',15,' ')||rpad('=',30,'=')||rpad(' ',15,' '));loopfetch c1 into T;exit when c1%notfound;if a_sno!=T.sno thendbms_output.put_line(rpad(' ',50,' '));dbms_output.put_line(rpad('--',50,'--'));dbms_output.put_line(rpad(' ',50,' '));dbms_output.put_line(rpad(' ',12,' ')||'学生姓名:'||T.sname);dbms_output.put_line(rpad(' ',5,' ')||rpad('=',30,'='));dbms_output.put_line(rpad('课程号',20,' ')||rpad('课程名',20,' ')||rpad('成绩',20,' '));dbms_output.put_line(rpad('=',10,'=')||rpad(' ',8,' ')||rpad('=',10,'=')||rpad(' ',9,' ')||rpad('=',10,'='));a_sno:=T.sno;end if;dbms_output.put_line(o||ame||T.grade);end loop;close c1;end;实验五实现过程,包,函数的编写1.目的要求使用PL/SQL语言编写过程,包和函数2.实验内容创建存储过程,包和函数,并能通过参数将结果传递出去。