实验目的
Oracle 数据库包含许多数据库对象,例如表、视图、索引、序列、存储过程、触发器等。表、视图、索引的操作在前面的实验中已经做了相应的练习,本实验将介绍如何使用序列、触发器和存储过程。同学们可以通过本实验掌握如何定义、使用删除这些数据库对象。
实验内容
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';
显示有多少行?
464行。
2、 创建新的用户并授权:
create user cc identified by ccpassword ;
grant resource, connect, DBA to c##cc;
3、 以用户CC
的身份建立连接,并在此连接下执行序列的操作:
select object_name,owner from all_objects where owner = 'SYSTEM';
显示有多少行?
453行
select object_name,owner from all_objects where owner = 'c##CC';
显示有多少行?
0行。
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 = 'c##CC';
显示有多少行?
两行
select object_name,Object_Type, owner from all_objects where owner = 'SYSTEM' and OBJECT_TYPE='SEQUENCE';
显示有多少行?
7行。
Select Object_Name, Object_Type, Owner From All_Objects Where Owner = 'c##CC' and OBJECT_TYPE='SEQUENCE';
显示有多少行?
两行。
select my_seq_01.nextval from dual;
重复执行上面的这条语句,得到什么序列?
1,2,3,4,……
dual是一个虚拟表,用来构成select
的语法规则
alter sequence my_seq_01 increment by 10 ;
select my_seq_01.nextval from dual;
重复执行上面的这条语句,得到什么序列?
21,31,41,51,……从上一个值继续
select my_seq_02.nextval from dual;
重复执行上面的这条语句,得到什么序列?
1,3,5,7,……
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;
重复执行上面的这条语句,得到什么序列?
100,103,106,109,……与上被删除的同名序列没有关系了。
4、 在CC
的连接中,执行存储过程的操作;
Declare
tmp integer default 0;
Begin
select