SQL中的3级嵌套表

时间:2015-04-16 18:56:02

标签: sql database oracle object exception

我想使用Oracle对象编程方面创建以下3级结构:
- 老板们    - - 经理
       - - - 员工

当我想在最后一行发生以下异常时运行创建表 tab_univ_bosses 的脚本时:

Error at Command Line:31 Column:3
Error report:
SQL Error: ORA-22913: "must specify table name for nested table column or attribute"
*Cause:    The storage clause is not specified for a nested table column
           or attribute.
*Action:   Specify the nested table storage clause for the nested table
           column or attribute.

Error starting at line 33 in command:
NESTED TABLE managers STORE AS tab_univ_managers
Error report:
Unknown Command

这是我的代码:

-- TYPE <EMPLOYEE>
create or replace 
TYPE type_Employee AS OBJECT (
  name                VARCHAR2(30), 
  department_name     VARCHAR2(30),
  second_name         VARCHAR2(30),
  manager             REF type_Employee 
);

CREATE or replace TYPE type_ListEmployees 
AS TABLE OF type_Employee;

-- TYPE <MANAGER>
create or replace 
TYPE type_Manager AS OBJECT (
  userData type_Employee,
  manager REF type_Employee, 
  listEmployees type_ListEmployees
);
NESTED TABLE listEmployees STORE AS tab_univ_employees 

CREATE or replace TYPE type_ListManagers 
AS TABLE OF type_ListEmployees;

-- BASE TABLE
CREATE TABLE tab_univ_bosses (
  id        NUMBER PRIMARY KEY,
  userData  type_Employee,
  managers  type_ListManagers
);
NESTED TABLE managers STORE AS tab_univ_managers <--- There is a problem...

1 个答案:

答案 0 :(得分:2)

您可以使用单个表格执行此操作:

create or replace 
TYPE type_Employee AS OBJECT (
  name                VARCHAR2(30), 
  department_name     VARCHAR2(30),
  second_name         VARCHAR2(30),
  manager             REF type_Employee 
);
/

CREATE TABLE Employees OF type_Employee (
  manager SCOPE IS Employees
)
/

INSERT INTO Employees VALUES ( 'Big', 'Admin', 'Boss', NULL );
INSERT INTO Employees VALUES ( 'Middle', 'Accounts', 'Manager',    ( SELECT REF(e) FROM Employees e WHERE name = 'Big' AND second_name = 'Boss' ) );
INSERT INTO Employees VALUES ( 'Other', 'Sales', 'Manager',        ( SELECT REF(e) FROM Employees e WHERE name = 'Big' AND second_name = 'Boss' ) );
INSERT INTO Employees VALUES ( 'Junior', 'Accounts', 'Accountant', ( SELECT REF(e) FROM Employees e WHERE name = 'Middle' AND second_name = 'Manager' ) );
INSERT INTO Employees VALUES ( 'Junior', 'Sales', 'Salesman',      ( SELECT REF(e) FROM Employees e WHERE name = 'Other' AND second_name = 'Manager' ) );

-- Get everyone in hierarchical order.
SELECT name, second_name, department_name, LEVEL
FROM   Employees e
START WITH manager IS NULL
CONNECT BY PRIOR name = DEREF( e.MANAGER ).name
AND PRIOR second_name = DEREF( e.MANAGER ).second_name
ORDER SIBLINGS BY name, second_name;

-- Get the employees which have the manager: 'Middle', 'Manager'
SELECT name, second_name, department_name
FROM  Employees e
WHERE DEREF( e.Manager ).Name = 'Middle'
AND DEREF( e.Manager ).Second_Name = 'Manager';

但是如果你真的想要使用那个结构:

create or replace 
TYPE type_Employee AS OBJECT (
  name                VARCHAR2(30), 
  department_name     VARCHAR2(30),
  second_name         VARCHAR2(30),
  manager             REF type_Employee 
);
/

CREATE or replace TYPE type_ListEmployees 
AS TABLE OF type_Employee;
/

create or replace 
TYPE type_Manager AS OBJECT (
  userData type_Employee,
  manager REF type_Employee, 
  listEmployees type_ListEmployees
);
/

CREATE or replace TYPE type_ListManagers 
AS TABLE OF type_Manager;
/

CREATE TABLE tab_univ_bosses (
  id        NUMBER PRIMARY KEY,
  userData  type_Employee,
  managers  type_ListManagers
)
NESTED TABLE managers STORE AS tab_univ_managers (
  NESTED TABLE listEmployees STORE AS tab_univ_employees
);
/