使用对象关系数据库进行 REF 查询

REF querying with object-relational database

所以目前我的数据库如下:

//branch table
create type branch_Type as object(bID number(6), branch_id ref branch_Type, b_street varchar2(20), b_city varchar2(20), b_p_code varchar2(8), b_bPhone number(14));

create table branch of branch_Type;

INSERT INTO branch VALUES(branch_Type('901', NULL, 'Market', 'Edinburgh', 'EH5 1AB', '01311235560'));

INSERT INTO branch VALUES(branch_Type('908', NULL, 'Bridge', 'Glasgow', 'G18 1QQ', '01413214556'));

insert into branch SELECT branch_Type('901', ref(e), b_street, b_city, b_p_code, b_bPhone) from branch e where e.BID = '901';

insert into branch SELECT branch_Type('908', ref(e), b_street, b_city, b_p_code, b_bPhone) from branch e where e.BID = '908';

//employee table
create type employee_Type as object(e_bid ref branch_Type, empID number(8), e_street varchar2(20), e_city varchar2(20), e_p_code varchar2(8), e_title varchar2(4), e_firstname_surname varchar2(20), emphomephone number(14), emp_mobile_1_2 number(22), supervisorID number(6), e_position varchar2(20), salary number(5), e_ninum varchar2(8), joindate date);

create table employee of employee_Type;

insert into employee select ref(e), '101', 'Dart', 'Edinburgh', 'EH1 05T', 'Mrs', 'Alison Smith', '01312125555', '0770562344307907812345', NULL, 'Head', '50000', 'NI001', '01-FEB-06'
from branch e where e.bid = '901';

insert into employee select ref(e), '105', 'New', 'Edinburgh', 'EH2 4AB', 'Mr', 'John William', 01312031990, 0790231455107701234567, '101', 'Manager', '40000', 'NI010', '04-MAR-07'
from branch e where e.bid = '901';

insert into employee select ref(e), '108', 'Old', 'Edinburgh', 'EH9 4BB', 'Mr', 'Mark Slack', 01312102211, NULL, '105', 'accountant', '30000', 'NI120', '01-Feb-09'
from branch e where e.bid = 901;

insert into employee select ref(e), '804', 'Adam', 'Edinburgh', 'EH1 6EA', 'Mr', 'Jack Smith', 01311112223, 0781209890, '801', 'Leader', '35000', 'NI810', '05-Feb-08'
from branch e where e.bid = 908;

select e_bid from EMPLOYEE where e_bid = '901'; //query I'm using

我正在尝试向员工 table 查询引用分支机构 table 的出价,但是当我 运行 工作表时,脚本输出告诉我没有已选择行。

e_bid 是对分支对象的引用,并不引用对象中包含的 bID 列。你想要:

SELECT e_bid
FROM   EMPLOYEE
WHERE  DEREF(e_bid).bID = 901;

我看不出为什么你的 BRANCH_TYPE 需要包含一个自我引用(当你设置它时你正在创建分支的副本所以会有一个实例 branch_idNULL 和另一个它指的是自己)。

您正在跨表重复多个结构,更好的结构可能是:

DROP SEQUENCE EMPLOYEES__ID__SEQ;
DROP TABLE EMPLOYEES;
--DROP TYPE EMPLOYEE_TYPE;
--DROP TABLE BRANCHES;
--DROP TYPE BRANCH_TYPE;
--DROP TYPE PHONENUMBER_TABLE_TYPE;
--DROP TYPE PHONENUMBER_TYPE;
--DROP TYPE ADDRESS_TYPE;

CREATE TYPE ADDRESS_TYPE AS OBJECT(
  street varchar2(20),
  city   varchar2(20),
  p_code varchar2(8)
);
/

CREATE TYPE PHONENUMBER_TYPE AS OBJECT(
  international varchar2(4),
  area          varchar2(5),
  local         varchar2(8)
);
/

CREATE TYPE PHONENUMBER_TABLE_TYPE AS TABLE OF PHONENUMBER_TYPE;
/

CREATE TYPE BRANCH_TYPE AS OBJECT (
  ID      number(6),
  Address ADDRESS_TYPE,
  Phone   PHONENUMBER_TYPE
);
/

create table branches of branch_type( ID PRIMARY KEY );

INSERT INTO BRANCHES VALUES (
  901,
  ADDRESS_TYPE( 'Market', 'Edinburgh', 'EH5 1AB' ),
  PHONENUMBER_TYPE( '044', '1311', '235560' )
);

INSERT INTO BRANCHES VALUES (
  908,
  ADDRESS_TYPE( 'Bridge', 'Glasgow', 'G18 1QQ' ),
  PHONENUMBER_TYPE( '044', '1413', '214556' )
);

create type employee_Type as object(
  branch            ref branch_type,
  ID                number(8),
  address           ADDRESS_TYPE,
  title             varchar2(4),
  firstname_surname varchar2(20),
  homephone         PHONENUMBER_TYPE,
  mobiles           PHONENUMBER_TABLE_TYPE,
  supervisor        REF EMPLOYEE_TYPE, -- Change this to a REF
  position          varchar2(20),
  salary            number(5),
  ninum             varchar2(8),
  joindate          date
);
/

create table employees of employee_Type (
  ID PRIMARY KEY
) NESTED TABLE mobiles STORE AS employees_mobiles;

CREATE SEQUENCE employees__id__seq;

insert into employees
select ref(b),
       101, -- EMPLOYEES__ID__SEQ.NEXTVAL,
       ADDRESS_TYPE( 'Dart', 'Edinburgh', 'EH1 05T' ),
       'Mrs',
       'Alison Smith',
       PHONENUMBER_TYPE( '044', '1312', '125555' ),
       PHONENUMBER_TABLE_TYPE(
         PHONENUMBER_TYPE( '044', '7705', '623443' ),
         PHONENUMBER_TYPE( '044', '7907', '812345' )
       ),
       NULL,
       'Head',
       50000,
       'NI001',
       DATE '1906-02-01'
from   branches b
where b.id = 901;

insert into employees
select ref(b),
       102, -- EMPLOYEES__ID__SEQ.NEXTVAL,
       ADDRESS_TYPE('New', 'Edinburgh', 'EH2 4AB'),
       'Mr',
       'John William',
       PHONENUMBER_TYPE(NULL, '0131', '2031990'),
       PHONENUMBER_TABLE_TYPE (PHONENUMBER_TYPE('44', '07902', '314551'), PHONENUMBER_TYPE('44', '07701', '234567')),
       REF(s),
       'Manager',
       40000,
       'NI010',
       DATE '2007-03-04'
from   branches b,
       employees s
where  b.id = 901
and    s.id = 101;