Oracle:在对象关系数据库中使用外键
Oracle: using foreign keys in object relation database
我有一个关于在 oracle 的对象关系数据库中使用主键和外键的问题。下面是我的 sql 代码和插入语句。我正在尝试创建一个包含学生、课程、教师和部门的简单数据库。但是每当我 运行 下面的 select 语句时,我都看不到我输入的 DepartmentID 和 TeacherID。相反,我得到 [HR.DEPARTMENT_OBJTYP] 作为 departmentID 和 teacherID 的值。这是因为我在课程 table 中引用了整个部门而不仅仅是 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;
下面的工作正常,但我不确定你是否正在寻找它。
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;
我有一个关于在 oracle 的对象关系数据库中使用主键和外键的问题。下面是我的 sql 代码和插入语句。我正在尝试创建一个包含学生、课程、教师和部门的简单数据库。但是每当我 运行 下面的 select 语句时,我都看不到我输入的 DepartmentID 和 TeacherID。相反,我得到 [HR.DEPARTMENT_OBJTYP] 作为 departmentID 和 teacherID 的值。这是因为我在课程 table 中引用了整个部门而不仅仅是 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;
下面的工作正常,但我不确定你是否正在寻找它。
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;