Oracle:在对象关系数据库

时间:2016-06-06 04:46:23

标签: database oracle object relational

我有一个关于在oracle中的对象关系数据库中使用主键和外键的问题。下面是我的sql代码和insert语句。我正在尝试与学生,课程,教师和部门一起创建一个简单的数据库。但每当我运行下面的select语句时,我都看不到我输入的DepartmentID和TeacherID。相反,我将[HR.DEPARTMENT_OBJTYP]作为departmentID的值,同样也作为teacherID。这是因为我引用了整个部门而不仅仅是课程表中的departmentID?我不知道如何使用外键并在对象关系数据库中正确查询它们,并且无法在其上找到任何有用的易于理解的信息。有人可以告诉我哪里出错了吗?

例如在Course_objtyp我做... TeacherID REF Teacher_objtyp。这是正确的引用方式,我不确定,因为我只想引用teacherID而不是整个Teacher_objtyp?

CREATE TYPE Student_objtyp AS OBJECT (
  FName         VARCHAR2(20),
  LName         VARCHAR2(20),
  StudentID     NUMBER
  ); 
/

CREATE TABLE Student_objtab OF Student_objtyp (StudentID PRIMARY KEY)
  OBJECT IDENTIFIER IS PRIMARY KEY;

CREATE TYPE Department_objtyp AS OBJECT (
  DeptID NUMBER,
  DeptName VARCHAR2(20)
  );
/  

CREATE TABLE Department_objtab OF Department_objtyp (DeptID PRIMARY KEY)
  OBJECT IDENTIFIER IS PRIMARY KEY;

CREATE TYPE Teacher_objtyp AS OBJECT (
  TeacherID NUMBER,
  FName VARCHAR2(20),
  LName VARCHAR2(20)
  );
/

CREATE TABLE Teacher_objtab OF Teacher_objtyp (TeacherID PRIMARY KEY)
  OBJECT IDENTIFIER IS PRIMARY KEY;

CREATE TYPE Course_objtyp AS OBJECT (
  CourseName VARCHAR(20),
  CourseID NUMBER,
  DeptID REF Department_objtyp,
  TeacherID REF Teacher_objtyp
  );
/

CREATE TABLE Course_objtab OF Course_objtyp (
  PRIMARY KEY (CourseID),
  FOREIGN KEY (DeptID) REFERENCES Department_objtab,
  FOREIGN KEY (TeacherID) REFERENCES Teacher_objtab)
/

INSERT INTO Student_objtab VALUES('bill','smitts',1);
INSERT INTO Student_objtab VALUES('bob','jo',2);

INSERT INTO Teacher_objtab VALUES(1,'Mr','Higgins');

INSERT INTO Department_objtab VALUES(1111,'Science');

INSERT INTO Course_objtab
SELECT 'Chem101',001,
REF(D),
REF(T)
FROM Department_objtab D, Teacher_objtab T
WHERE D.DeptID = 1111 and T.TeacherID = 1;

我想要运行的非常简单的select语句:

select * from Course_objtab;

1 个答案:

答案 0 :(得分:1)

以下工作正常,但我不确定您是否正在寻找。

CREATE TYPE Course_objtyp AS OBJECT (
  CourseName VARCHAR(20),
  CourseID NUMBER,
  DeptID Number,
  TeacherID Number
  );

CREATE TABLE Course_objtab OF Course_objtyp (
  PRIMARY KEY (CourseID),
  FOREIGN KEY (DeptID) REFERENCES Department_objtab(DeptID),
  FOREIGN KEY (TeacherID) REFERENCES Teacher_objtab(TeacherID));

INSERT INTO Course_objtab
 SELECT 'Chem101',001, D.DeptID, T.TeacherID
 FROM Department_objtab D, Teacher_objtab T
 WHERE D.DeptID = 1111 and T.TeacherID = 1;