How do I insert data into object tables that have refs to others?
I'm new in Oracle and I really don't have a clear idea how to do this.
The database is this one...
CREATE OR REPLACE TYPE personUdt4 AS OBJECT(
pid varchar(11),
firstName varchar(20),
lastName varchar(20),
dob date)
NOT FINAL;
/
CREATE OR REPLACE TYPE locationUdt4 AS OBJECT(
street varchar(30),
bldg varchar(5),
room varchar(5))
NOT FINAL;
/
CREATE TYPE departmentUdt4;
/
CREATE TYPE studentUdt4;
/
CREATE TYPE facultyUdt4;
/
CREATE OR REPLACE TYPE campusClubUdt4 AS OBJECT(
cId number,
n开发者_开发知识库ame varchar(50),
location locationUdt4,
phone varchar(12),
advisor REF facultyUdt4,
members REF studentUdt4)
NOT FINAL;
/
CREATE OR REPLACE TYPE facultyUdt4 UNDER personUdt4(
rank varchar(10),
advisorOf REF campusClubUdt4,
worksIn REF departmentUdt4,
chairOf REF departmentUdt4)
NOT FINAL;
/
CREATE OR REPLACE TYPE studentUdt4 UNDER personUdt4(
status varchar(10),
memberOf REF campusClubUdt4,
major REF departmentUdt4)
NOT FINAL;
/
CREATE OR REPLACE TYPE studentUdtList4 AS VARRAY(1000) of studentUdt4;
/
CREATE OR REPLACE TYPE facultyUdtList4 AS VARRAY(50) of facultyUdt4;
/
CREATE OR REPLACE TYPE departmentUdt4 AS OBJECT(
code varchar(3),
name varchar(40),
deptChair REF facultyUdt4,
MEMBER FUNCTION getStudents RETURN studentUdtList4,
MEMBER FUNCTION getFaculty RETURN facultyUdtList4)
NOT FINAL;
/
CREATE TYPE BODY departmentUdt4 as
member function getStudents return studentUdtList4
end func;
member function getFaculty return facultyUdtList4
end func;
end;
/
CREATE TABLE person4 OF personUdt4(
primary key (pid));
/
CREATE TABLE faculty4 OF facultyUdt4;
/
CREATE TABLE student4 OF studentUdt4;
/
CREATE TABLE department4 OF departmentUdt4(
primary key (code));
/
CREATE TABLE campusClub4 OF campusClubUdt4(
primary key (cid)
);
INSERT INTO student4
(pid, firstname, lastname, dob, status, memberOf, major)
VALUES
('10','alex','smith','31-may-98','FRESH', '10', '11');
COMMIT;
It'll be great if someone can help me D:
(This is all based on my very limited knowledge of Oracle's object-relational technology. Some of it may be wrong, and there's likely a simpler way to do this.)
As I understand it, a REF must point to an actual row. You must create some data before you can create some data. Since there are so many circular references you'll have to go back later and update everything. But hopefully this is enough to at least get you started.
--Create some rows to reference.
insert into campusClub4(cid) values(1);
insert into department4(code) values('A');
--Insert regular columns first.
insert into student4(pid, firstName, lastName, dob, status)
values('10', 'alex', 'smith', to_date('31-MAY-1998', 'DD-MON-YYYY'), 'FRESH');
--Add references with an update.
update student4
set memberOf = (select ref(campusClub) from campusClub4 campusClub where cid = 1)
,major = (select ref(department) from department4 department where code = 'A')
where pid = '10';
--Verify data
select pid, firstname, lastname, dob, status, deref(memberOf), deref(major) from student4;
--This would be a simpler method, but it doesn't work and I don't understand why.
insert into student4(pid, firstName, lastName, dob, status)
values('10', 'alex', 'smith', to_date('31-MAY-1998', 'DD-MON-YYYY'), 'FRESH'
,(select ref(campusClub) from campusClub4 campusClub where cid = 1)
,(select ref(department) from department4 department where code = 'A')
);
But I strongly recommend that you NEVER DO THIS. Inserting data into a table should not be this difficult. Object Relational databases are probably a bad idea. And Oracle's implementation of it sucks. You'll get ORA-600 errors and "invalid" tables all over the place with this stuff (e.g. I just got an ORA-600 from cross joining campusClub4 and department4 with only one row in each). And nobody will know how to use your data.
精彩评论