2017-04-01 35 views
0

我想问一些pl/sql oracle的帮助。我试图找出这个面向对象的数据库的东西,但迄今没有太大的成功。我进入编程,我也理解rdbms,已经管理了一些复杂的数据库,但这个面向对象超出了我的理解。 所以任务如下: 有老师和学生。需要制定一个有两种课程的时间表。一天和下午。 每天一班最多可有10名学生和下午5最大PL SQL关系和程序问题

以下内容我至今:

/*create student type and table*/ 
create type student_typ as object(
st_id char(3), 
st_name varchar(50) 
) 
create table student_tbl of student_typ(
st_id primary key not null, 
st_name not null 
) 

/*create teacher type and table*/ 
create type teacher_typ as object(
tr_id char(3), 
tr_name varchar(50) 
) 
create table teacher_tbl of teacher_typ(
tr_id primary key not null, 
tr_name not null 
) 

/*create array type to store the student id-s*/ 
create type students_arr as varray(20) of char(3); 

/*create table for timetable*/ 
create table timetable_tbl(
t_ID char(5), 
t_Date date, 
t_Time varchar(5), /*store time format as 12:00*/ 
t_Type char(1), 
teacher ref teacher_typ scope is teacher_tbl, 
students students_arr, 
constraint chk_type check(t_Type='D' or t_Type='N') 
) 

/*insert values into the tables*/ 
insert into teacher_tbl values ('t01','bob'); 
insert into teacher_tbl values ('t02','smith'); 
insert into student_tbl values ('s01','lara'); 
insert into student_tbl values ('s02','john'); 

/*insert values into timetable*/ 
insert into timetable_tbl(select 't01','01-Apr-17','09:00','D',ref(t),students_arr('s01','s02') from teacher_tbl t where t.tr_ID='t01'); 
insert into timetable_tbl(select 't02','01-Apr-17','19:00','N',ref(t),students_arr('s01') from teacher_tbl t where t.tr_ID='t02'); 

/*display timetable records*/ 
select t_ID, t_Date, t_Time, t_Type, deref(teacher).tr_ID,deref(teacher).tr_Name, students from timetable_tbl; 

/*create procedure to add new student*/ 
create or replace procedure Add_Student(
v_id in student_tbl.st_id%type, 
v_name in student_tbl.st_name%type, 
verror out varchar2) 
is begin 
insert into student_tbl (st_id,st_name) values (v_id,v_name); 
commit; 
exception when others then 
verror := sqlerrm; 
end; 

/*call add_student procedure*/ 
declare 
verror varchar2(500); 
begin 
Add_Student('s03','sam',verror); 
end; 

所以这是我到目前为止所。问题在下。

  • 这是正确的方法吗?

  • 我该如何引用varray中的学生?我已经试过参考老师参考,但没有结果呢? students student_arr ref student_typ范围是student_tbl;此代码不起作用。 students_arr数组只应接受来自学生表的现有记录

  • 如何创建时间表的过程?因为当t_Type是“D”时,主要的事情是,那么student_arr应该允许最多10个值 ,并且如果t_Type是“N”,那么student_arr应该只允许最多5个值。我应该做一些像add_record程序的时间表和使用if语句,但我不确定这将是一个好主意。 如果t_type = 'd',则如果 students.count < = 10 或 如果t_type = 'N' 然后 如果students.count < = 5

林种stucked现在。我看了很多关于pl sql的教程,但更加困惑。烦人的事情是,我管理与 rdbms(SQL服务器)和Java在相同的时间。但是这个语言和对象数据库现在让我很难受。

任何帮助将被占用!

+0

我没有看到具体的PL/SQL问题。这个问题似乎是在处理[SQL中的对象引用函数](https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions005.htm),这在学术上可能很有趣,但我非常自信自从8i推出以来,没有人在任何现实世界中使用过它。另外,不相关的一些字符串类型是'char'而不是'varchar2'。 'char'很少是一种有用的类型。 –

+0

我试过了,所有的代码工作。有趣的'deref'就像是一个隐含的FK查找 - 整齐。我似乎想起了一篇Steven Feuerstein的文章,建议它的实际用途,尽管我现在想不出任何。我认为一般的“正确的做法”是避免收集和参考列,除非你有特定的需求。 –

+0

正如我所提到的,我不太清楚这个面向对象的数据库的东西。我知道编程Java,C#和更多和RDBMS,但这东西我不明白。也许我只是想了解整个数据库概念。我需要帮助的是以正确的方式在plsql中创建场景。 – bontoo

回答

0

您可以使用table()功能拓展students阵列:

select t_id, t_date, t_time, t_type, deref(teacher).tr_id,deref(teacher).tr_name 
    , s.column_value as student 
from timetable_tbl cross join table(students) s; 

您还可以看到,联接写成

from timetable_tbl, table(students) s; 

看起来整洁在这种情况下,但我已经正式切换以ANSI连接语法。

由于students_arr是varchar2的通用varray,因此它没有属性名称,因此默认名称为column_value

至于程序中插入的时间表,如果有一个名为p_students一个students_arr VARRAY参数,是只检查p_students.countt_type值。 (不幸的是,您不能在检查约束中使用cardinality()函数,否则可能是声明式方法。)

关于“执行此操作的正确方法”,请使用普通的,规范化的关系表,除非您对对象/嵌套表的内容有很好的使用。

0

看来我并不清楚这些问题。所以这里又是一个简单的方法。

有一个人类,其中包含关于人的信息。还有一个测试课程,其中包含时间表等其他信息,但它包含一列中的人员。所以它是一个多值列,我可以使用varray或嵌套表类型进行管理。 enter image description here

问题是如何以这种方式使用ref。我可以使用ref作为单个对象,但不能用于集合。

/*create person type and table*/ 
create type person_ty as object(
pid varchar(3), 
pName varchar(50) 
); 
create table person_tbl of person_ty; 

/*create table which will be used as nested table*/ 
create type nest_ty as object(
tID varchar(3), 
person ref person_ty 
); 
create type nest_tbl as table of nest_ty; 

/*create table for test*/ 
create table test_tbl(
tID varchar(3), 
tDate date, 
persons nest_tbl 
)nested table persons store as persons; 
  • 所以我怎样才能将数据插入测试表?

我知道没有引用类型单一的嵌套表将是:

insert into test_tbl values ('123','12-dec-17', nest_tbl(
nest_ty('123','p01'), 
nest_ty('345','p06') 
)); 

,我知道有一个REF类型插入数据是这样的:

insert into test_tbl 
select '123', '12-dec-17', ref(a) from person_tbl a where a.pID='p01'; 

我再次oodb和plsql非常新,所以我的问题可能不清楚或没有意义。在那种情况下,我很抱歉。 仍然适合各种帮助。