sqloracle-databaseobject-relational-model

REF querying with object-relational database


So currently my database stands as follows:

//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

I am trying to query the employee table for the bid's that are referenced to branch table, but when I run the worksheet the script output tells me that I have no rows selected.


Solution

  • e_bid is a reference to the branch object and does not refer to the bID column contained in the object. You want:

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

    I see no reason why your BRANCH_TYPE needs to contain a self reference (and when you set it you are creating a duplicate of the branch so there will be one instance where branch_id is NULL and another where it refers to itself).

    You are repeating several structures across the tables and a better structure might be:

    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;