实验五 Oracle数据库对象

Oracle数据库对象

一、实验目的
Oracle 数据库包含许多数据库对象,例如表、视图、索引、序列、存储过程、触发器等。表、视图、索引的操作在前面的实验中已经做了相应的练习,本实验将介绍如何使用序列、触发器和存储过程。同学们可以通过本实验掌握如何定义、使用删除这些数据库对象。
二、使用仪器、材料
Oracle 11g,windows10;
三、实验内容

1.	序列
a)	创建序列
b)	查看创建的序列对象
c)	使用序列
d)	修改序列
e)	删除序列
2.	存储过程
a)	创建三个数据表
b)	插入数据创建存储过程
c)	创建存储过程,更新表中的数据
d)	执行存储过程,并比较存储过程执行前后的数据变化情况
e)	删除存储过程
f)	创建存储过程
g)	运行存储过程
3.	触发器
a)	创建触发器
b)	创建触发器credit_id
c)	查看刚创建的触发器对象
d)	激活刚创建的触发器
1.以SYSTEM连接数据库ORCL,执行以下语句查看对象:
select object_name,owner from all_objects where owner = 'SYSTEM';

2.创建新的用户并授权:
create user cc identified by ccpassword ;

3.以用户CC的身份建立连接,并在此连接下执行序列的操作:
select object_name,owner from all_objects where owner = 'SYSTEM';
显示有多少行?
select object_name,owner from all_objects where owner = 'CC';
显示有多少行?
create sequence my_seq_01 increment by 1 start with 1 nomaxvalue nocycle;
create sequence my_seq_02 increment by 2 start with 1;

select object_name,owner from all_objects where owner = 'CC';
显示有多少行?
select object_name,Object_Type, owner from all_objects where owner = 'SYSTEM' and OBJECT_TYPE='SEQUENCE';
显示有多少行?
Select Object_Name, Object_Type, Owner From All_Objects Where Owner = 'CC' and OBJECT_TYPE='SEQUENCE';
显示有多少行?
select my_seq_01.nextval from dual;
重复执行上面的这条语句,得到什么序列?
alter sequence my_seq_01 increment by 10 ;
select my_seq_01.nextval from dual;
重复执行上面的这条语句,得到什么序列?
select my_seq_02.nextval from dual;
重复执行上面的这条语句,得到什么序列?
drop sequence my_seq_02;
select my_seq_02.nextval from dual; 
什么结果?
create sequence my_seq_02 increment by 3 start with 100;
select my_seq_02.nextval from dual;
重复执行上面的这条语句,得到什么序列?

4.在CC的连接中,执行存储过程的操作;
Declare
tmp integer default 0;
Begin
select count(*) into tmp from user_tables where table_name='SC';
if(tmp>0) then
execute immediate 'drop table SC ';
end if;
select count(*) into tmp from user_tables where table_name='STUDENT';
if(tmp>0) then
execute immediate 'drop table STUDENT ';
end if;
select count(*) into tmp from user_tables where table_name='COURSE';
if(tmp>0) then
execute immediate 'drop table COURSE ';
end if;
Select count(*) into tmp From All_Objects Where OBJECT_NAME='SC_INS' and OBJECT_TYPE='PROCEDURE';
if(tmp>0) then
execute immediate 'drop PROCEDURE SC_INS ';
end if;
Select count(*) into tmp From All_Objects Where OBJECT_NAME='STUDENT_NO' and OBJECT_TYPE='SEQUENCE';
if(tmp>0) then
execute immediate 'drop SEQUENCE STUDENT_NO ';
end if;
end;
然后重新创建数据表:

create table student(sno int primary key, sname varchar(8));
create table course(cno int primary key, cname varchar(10));
create table sc(sno int, cno int, grade int, 
      primary key(sno,cno), 
      foreign key (sno) references student(sno), foreign key (cno) references course(cno));

成功了吗?上面的三个表的创建顺序能否任意调整?
select object_name, Object_Type, owner from all_objects where owner = 'CC';
显示有多少行?索引是怎么产生的?
create sequence student_no increment by 1 start with 2012001;

insert into student values(student_no.nextval, 'aaaaaa');
insert into student values(student_no.nextval,'bbbbbbb');
insert into student values(student_no.nextval,'ccccccc');
insert into student values(student_no.nextval,'ddddddd');
commit;
select * from student;
查看student表的内容
insert into course values (105,'程序设计');
insert into course values (908,'大学英语');
insert into course values (433,'数据结构');
commit;
select * from course;
查看course表的内容
create procedure sc_ins(ino int,cno int,grade int) is
begin
  if(grade>=0) then insert into sc values (ino,cno,grade);
  else insert into sc values (ino,cno,null);
end if;
end;
成功了吗? 显示什么内容?
Select Object_Name, Object_Type, Owner From All_Objects Where Owner = 'CC' and OBJECT_TYPE='PROCEDURE';
显示有多少行? 
exec sc_ins (2012001,105,60);
exec sc_ins (2012001,908,0);
exec sc_ins (2012001,433,98);
exec sc_ins (2012002, 105,75);
exec sc_ins (2012002, 433,-1);
exec sc_ins (2012003, 105,64);
exec sc_ins (2012003, 908,90);
exec sc_ins (2012003, 433,-100);
成功了吗? 
select student.sno,sname,cname,grade 
  from student,course,sc 
  where student.sno=sc.sno and course.cno=sc.cno;
显示结果说明,成绩为负数的话,在数据表里是什么内容?存储过程有什么好处?
分数为0,和分数为负数,有区别吗?

5.在CC的连接中,执行触发器的操作;
一、触发器只影响当前表:
alter table sc add (gradelevel char(1));

update sc set gradelevel='A' where grade>=85;
update sc set gradelevel='B' where grade>=75 AND grade<85;
update sc set gradelevel='C' where grade>=60 AND grade<75;
update sc set gradelevel='D' where grade<60;

select student.sno,sname,cname,grade,gradelevel 
  from student,course,sc 
  where student.sno=sc.sno and course.cno=sc.cno;
看到和上一步的显示结果有何区别?
create or replace trigger sc_ins before insert or update on sc
for each row
begin
  if :new.grade>=85 then :new.gradelevel:='A';
  else if :new.grade>=75 then :new.gradelevel:='B';
      else if :new.grade>=60 then :new.gradelevel:='C';
           else if :new.grade>=60 then :new.gradelevel:='D';
 end if;
  end if;
   end if;
    end if;
 end;
执行成功了吗?
select * from sc where sno=2012002;
insert into sc(sno,cno,grade) values (2012002,908,80);
select * from sc where sno=2012002;
刚刚插入的行,gradelevel列的值哪里来的?

二、触发器影响其他表:
alter table course add (maxgrade int);

update course set maxgrade=0;
select * from course;
表的结构改变成功了吗?修改成功没有?
create or replace trigger course_ins before insert or update on sc
for each row
declare oldg int;
begin
 select maxgrade into oldg from course where cno=:new.cno;
 if oldg<:new.grade then update course set maxgrade=:new.grade where cno=:new.cno;
 end if;
 end course_ins;
触发器生成成功了吗?当前触发器的目录下有多少项?
select * from course;
记下当前各科的最高分数
insert into sc(sno,cno,grade) values (2012004,908,99);
insert into sc(sno,cno,grade) values (2012004,433,88);
insert into sc(sno,cno,grade) values (2012004,105,59);
select * from sc;
select * from course;
2012004号同学的三科分数插入成功了吗?
当前各科的最高分有没有变化?
select * from sc  where sno=2012003 and cno=105;
2012003号同学的105号课程的分数是多少?级别是什么?

update sc set grade=100 where sno=2012003 and cno=105;
select * from sc where sno=2012003 and cno=105;
select * from course;

2012003号同学的105号课程的分数修改成功了吗?级别(sc.gradelevel)有没有相应变化?当前各科的最高分(course.maxgrade)有没有变化?
一个修改语句,可以同时触发两个表的数据改变吗?

四、实验结果及分析
本实验代码在实验中已给出,不给予重复,实验结果所需截图过多,不进行一一上传,有需要实验结果截图或者其他实验报告的同学可以点击https://download.csdn.net/download/weixin_43981315/12721378获取!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值