当前位置:文档之家› 菜单树的设计与实现实验报告

菜单树的设计与实现实验报告

一、问题描述创建table保存具有层次结构的菜单树信息,记录菜单的ID,名称,描述,父菜单(可为空)和可用性(enable/disable),菜单的深度无限制。

二、实验内容及步骤1.首先以sysdba身份登录数据库,命令为:conn / as sysdba2.创建用户表空间menu,数据文件为:d:\menu.dbf,大小20M,相关命令如下:create tablespace menudatafile 'd:\menu.dbf' size 20M;3.创建新用户yjmin,默认表空间为新创建的menu表空间,相关命令如下:create user yjminidentified by yjmindefault tablespace menu;4.为新创建用户yjmin授权connect和resource角色;5.以新创建的用户yjmin登录数据库,命令为:conn yjmin/yjmin;6.创建序列,自动生成菜单ID,相关命令如下:create sequence id_seqstart with 1increment by 1;7.创建表menu_tab保存菜单信息:create table menu_tab(id number primary key,name varchar2(50) not null,description varchar2(200),parent_menu varchar2(50),available varchar2(10) not null);8.向表menu_tab中插入菜单数据,以示例数据插入,并提交插入的数据。

9.打开PL/SQL输出信息,命令为:set serveroutput on;10.创建函数获取菜单ID:create or replace function get_menu_id(v_name varchar2)return numberas专业计算机科学与技术(数据库方向)年级班级课程名称大型数据库实验课程实验项目菜单树的设计与实现实验类型□验证□设计□综合实验时间 2012年5月11日实验指导老师实验评分result_id number;beginselect id into result_id from menu_tab where name=v_name;return result_id;exceptionwhen no_data_found thenreturn 0;when too_many_rows thenreturn -1;end get_menu_id;/11.根据输入的菜单名称,输出菜单ID:declarev_input menu_%type;v_menu menu_%type;beginv_input := '&菜单名称';select name into v_menu from menu_tab where name=v_input;dbms_output.put_line(chr(10)||'该菜单名称的ID如下:');dbms_output.put_line(chr(10)||'菜单名称:'||v_menu||' '||'ID:'||get_menu_id(v_menu));exceptionwhen no_data_found thendbms_output.put_line(chr(10)||'该菜单名称不存在!');when others thendbms_output.put_line(chr(10)||'程序出现异常,已结束!');end;/该语句块的运行结果如下:专业计算机科学与技术(数据库方向)年级班级课程名称大型数据库实验课程实验项目菜单树的设计与实现实验类型□验证□设计□综合实验时间 2012年5月11日实验指导老师实验评分12.根据输入的菜单名称,输出祖先菜单结构:declaretype indextable is table of menu_tab.parent_menu%type index by binary_integer;v_parents indextable;v_input menu_tab.parent_menu%type;v_temp menu_tab.parent_menu%type;v_counter binary_integer :=1;beginv_input := '&菜单名称';v_parents(v_counter) := v_input;v_temp := v_input;while v_temp is not null loopselect parent_menu into v_temp from menu_tab where name=v_parents(v_counter);v_counter := v_counter+1;v_parents(v_counter) := v_temp;end loop;v_counter := v_counter-1;dbms_output.put_line(chr(10)||'该菜单名称的祖先菜单结构如下:');dbms_output.put(chr(10));loopif v_counter != 1 thendbms_output.put(v_parents(v_counter)||' - ');elsedbms_output.put_line(v_parents(v_counter));end if;v_counter := v_counter-1;exit when v_counter = 0;end loop;exceptionwhen no_data_found thendbms_output.put_line(chr(10)||'该菜单名称不存在!');when others thendbms_output.put_line(chr(10)||'程序出现异常,已结束!');end;/该语句块的运行结果如下:专业计算机科学与技术(数据库方向)年级班级课程名称大型数据库实验课程实验项目菜单树的设计与实现实验类型□验证□设计□综合实验时间 2012年5月11日实验指导老师实验评分13.创建递归存储过程获取子菜单:create or replace procedure print_child_menu(v_parent in varchar2,depth in number)asv_name menu_%type;cursor c_menu isselect name from menu_tab where parent_menu=v_parent;beginopen c_menu;loopfetch c_menu into v_name;exit when c_menu%notfound;for i in 1..depth loopdbms_output.put(chr(9));end loop;dbms_output.put_line(v_name);print_child_menu(v_name,depth+1);end loop;close c_menu;end print_child_menu;/14.根据输入的菜单名称,输出所有子菜单:declarev_input menu_%type;v_menu menu_%type;beginv_input := '&菜单名称';select name into v_menu from menu_tab where name=v_input;dbms_output.put_line(chr(10)||'该菜单名称的所有子菜单如下:');dbms_output.put_line(chr(10)||v_menu);专业计算机科学与技术(数据库方向)年级班级课程名称大型数据库实验课程实验项目菜单树的设计与实现实验类型□验证□设计□综合实验时间 2012年5月11日实验指导老师实验评分print_child_menu(v_menu,1);exceptionwhen no_data_found thendbms_output.put_line(chr(10)||'该菜单名称不存在!');when others thendbms_output.put_line(chr(10)||'程序出现异常,已结束!');end;/该语句块的运行结果如下:15.关闭PL/SQL输出信息,命令为:set serveroutput off;16.以sysdba身份登录数据库17.删除用户yjmin,命令为:drop user yjmin cascade;18.删除表空间menu,包括数据文件,相关命令如下:drop tablespace menu including contents and datafiles;三、实验小结通过本次实验,掌握了游标、存储过程以及函数的用法,并对一些Oracle数据库概念加强了理解及应用。

相关主题