Can I make a table of objects that have nested tables as attributes? - oracle

Here is a snippet of my OR schema:
CREATE TYPE artist_table_type AS TABLE OF REF artist_type;
/
CREATE TYPE track_type AS OBJECT (
title VARCHAR(1000),
duration INT,
release_date DATE,
producers artist_table_type,
MEMBER FUNCTION getProducers RETURN artist_table_type,
MEMBER FUNCTION getRemixers RETURN artist_table_type
);
/
CREATE TABLE track_obj_table OF track_type;
When I attempt to run this, I get the error:
CREATE TABLE track_obj_table OF track_type
*
ERROR at line 1:
ORA-22913: must specify table name for nested table column or attribute
I suspect that this is because of the table type in the track_type object?

It just means you have to provide the storage clause for the nested table:
SQL> CREATE TABLE track_obj_table OF track_type;
CREATE TABLE track_obj_table OF track_type
*
ERROR at line 1:
ORA-22913: must specify table name for nested table column or attribute
SQL> CREATE TABLE track_obj_table OF track_type
2 NESTED TABLE producers STORE AS producers_nt
3 /
Table created.
SQL> desc track_obj_table
Name Null? Type
----------------------------------------- -------- -------------------------
TITLE VARCHAR2(1000)
DURATION NUMBER(38)
RELEASE_DATE DATE
PRODUCERS ARTIST_TABLE_TYPE
SQL>

Related

Unable to create a table with a column of object type (and the same object type contains an attribute of nested table type)

I am trying to create a table with a column of object type and tricky nested table hierarchy. Getting ORA-22913 error. In CREATE table statement the column "theCol" is of type object (i.e. MainObj). MainObj contains an attribute of type nested table i.e. ChildTab.
I think am supposed to use NESTED TABLE clause in CREATE TABLE statement. But not sure how to use it here because "theCol" is NOT of nested table type.
DROP TYPE MainObj;
DROP TYPE ChildTab;
DROP TYPE ChildObj;
CREATE TYPE ChildObj AS OBJECT (
naame varchar2(20)
, kaam varchar2(20)
);
/
CREATE TYPE ChildTab AS TABLE OF ChildObj;
/
Create TYPE MainObj as OBJECT (
KEEY VARCHAR2(5),
ChildList ChildTab
);
/
CREATE TABLE TestTableDesi (
theCol MainObj
);
/
Type dropped.
Type dropped.
Type dropped.
Type created.
Type created.
Type created.
ORA-22913: must specify table name for nested table column or attribute
Just found the solution here. How do I create an Oracle table of objects containing nested tables?
The create table statement should be as follows.
CREATE TABLE TestTableDesi (
theCol MainObj
) nested table theCol.ChildList store as ChildList_tab ;
/

SQL3 ORA-22913: must specify table name for nested table column or attribute

I am trying to define a table in SQL3 of a type that contains a nested table in its type declaration. I do not understand why I am always getting the same error despite having tried several solutions. Here is the piece of code:
create type Composite;
/
create type L_PieceComposite as table of ref Composite;
/
create type Piece as object(
name VARCHAR(20),
containedInto L_PieceComposite
)
not final not instantiable;
/
create type PieceQuantity as Object (
quantity NUMBER,
pieceref ref Piece
);
/
create type L_PieceQuantity as table of PieceQuantity;
/
create type Composite UNDER Piece(
cost NUMBER,
contains L_PieceQuantity
);
/
In another file I use:
create table thePieces of Piece;
CREATE TABLE theComposites of Composite NESTED TABLE contains store as tab7;
But get the following error:
ORA-22913: must specify table name for nested table column or attribute
Could anyone help?
Thanks
SOLVED..conclusion: we must beware of inherited tables.
create table lesComposites of Composite nested table containedInto store as tab5 nested table contains store as tab6;

Error in creating nested table involving inheritence

I have a somewhat complex structure as follows :
create or replace type user_typ as object(
user_id number(19,0),
username nvarchar2(40 char)
);
I inherit an applicant_typ from this :
create or replace type applicant_typ under user_typ (
resume_text nclob
);
My design involves jobs to which applicants can apply. To this end, I create an application_typ as follows :
create or replace TYPE Application_typ AS OBJECT (
application_id NUMBER,
candidate applicant_typ,
time_of_app DATE
);
CREATE TYPE Application_tab IS TABLE OF Application_typ;
And now I want to create an object type called Job_typ, and a table containing those objects, wherein there will be a nested table for applications :
CREATE OR REPLACE TYPE Job_typ AS OBJECT (
job_ID NUMBER,
company_ID NUMBER,
description NVARCHAR2(1000),
name NVARCHAR2(200),
application Application_tab,
MAP MEMBER FUNCTION job_no RETURN NUMBER,
MEMBER PROCEDURE no_of_applicants
);
All of this works fine. The issue is when I try to create a table of type Job_typ :
CREATE TABLE Job_tab OF Job_typ
NESTED TABLE application STORE AS application_nt;
This doesn't work, giving the error :
SQL Error: ORA-02320: failure in creating storage table for nested table column APPLICATION
ORA-22913: must specify table name for nested table column or attribute
02320. 00000 - "failure in creating storage table for nested table column %s"
*Cause: An error occurred while creating the storage table for the
specified nested table column.
What am I doing wrong?
EDIT : I tried some different things. If I change application_typ as follows :
CREATE OR REPLACE TYPE Application_typ AS OBJECT (
application_id NUMBER,
candidate User_Typ, -- NOTE: This attribute is now of type User_typ instead of the inherited type
time_of_app DATE,
);
CREATE TYPE Application_tab IS TABLE OF Application_typ;
Then everything else works, and I am able to create the Job table. Why do I get the error on using the inherited type?
I tried the following in Oracle 11.2.0.1 and didn't get any error. I made a slight change though:
CREATE OR REPLACE TYPE user_typ AS OBJECT
(
user_id NUMBER (19, 0),
username NVARCHAR2 (40 CHAR)
) NOT FINAL; -- << Notice the NOT FINAL keyword
create or replace type applicant_typ under user_typ (
resume_text nclob
);
CREATE OR REPLACE TYPE Application_typ AS OBJECT
(
application_id NUMBER,
candidate applicant_typ,
time_of_app DATE
);
CREATE TYPE Application_tab IS TABLE OF Application_typ;
CREATE OR REPLACE TYPE Job_typ AS OBJECT
(
job_ID NUMBER,
company_ID NUMBER,
description NVARCHAR2 (1000),
name NVARCHAR2 (200),
application Application_tab,
MAP MEMBER FUNCTION job_no
RETURN NUMBER,
MEMBER PROCEDURE no_of_applicants
);
CREATE TABLE Job_tab OF Job_typ
NESTED TABLE application
STORE AS application_nt;
While trying to create all your types and keywords, I got the error:
[Error] PLS-00590 (10.1): PLS-00590: attempting to create a subtype UNDER a FINAL type
This is because Oracle doesn't allow creation of a subtype on a FINAL type. If you don't define any finalizing clause for the base type, the default is FINAL.
Read more on Oracle Docs.
If you are coding for the real world (read industry), I'd advise against using nested tables as column types. You end up spending your entire life trying to nest and un-nest these. I'd suggest you normalize your schema as much as you can or need and leave nested tables for operations in PL/SQL code blocks.

Nested table in oracle giving error

I am not able to create a nested table. I executed the following commands:
create or replace type address_ty as object(
street nvarchar2(15),
city nvarchar2(15),
district nvarchar2(15));
create or replace type name_ty as object(
name nvarchar2(15),
address address_ty);
create or replace type dependent_ty as object(
relation nvarchar2(15),
name name_ty,
age number);
create or replace type dependent_list as table of dependent_ty;
create or replace type employee_info_ty as object(
emp_id nvarchar(15),
name name_ty,
salary nvarchar(15),
dept_id nvarchar(15),
dependents dependent_list);
When I create a table it gives an error:
create table employee_info of employee_info_ty OIDINDEX OID_EMPLOYEE_INFO
nested table dependent store as dependent_ty;
SQL Error: ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
Your last type declaration is invalid:
create or replace type employee_info_ty as object(
emp_id nvarchar(15),
name name_ty,
salary nvarchar(15),
dept_id nvarchar(15),
dependents dependent_list);
/
Warning: Type created with compilation errors.
show errors
Errors for TYPE EMPLOYEE_INFO_TY:
LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0 PL/SQL: Compilation unit analysis terminated
2/12 PLS-00201: identifier 'NVARCHAR' must be declared
If that's changed to use nvarchar2 then the invalid datatype error against employee_info_ty goes away but is replaced with another error:
create or replace type employee_info_ty as object(
emp_id nvarchar2(15),
name name_ty,
salary nvarchar2(15),
dept_id nvarchar2(15),
dependents dependent_list);
/
Type created.
create table employee_info of employee_info_ty OIDINDEX OID_EMPLOYEE_INFO
nested table dependents store as dependent_ty;
nested table dependent store as dependent_ty
*
ERROR at line 2:
ORA-00904: : invalid identifier
... which is because you called the nested table dependents in the type definition but dependent here. You also can't re-use dependent_ty for the store as clause as that's an object type you just created; you'd get an ORA-00955. This works:
create table employee_info of employee_info_ty OIDINDEX OID_EMPLOYEE_INFO
nested table dependents store as dependents_tab;
Table created.
From question in comments, if you populate a row like:
insert into employee_info values (
employee_info_ty('1', name_ty('Joe Bloggs',
address_ty('1 Main Street', 'Omaha', 'Nebraska')),
'20000', '2', dependent_list(dependent_ty('Daughter',
name_ty('Emily Bloggs',
address_ty('1 Main Street', 'Omaha', 'Nebraska')),
'14')
)
)
);
You can select immediate attributes like:
select e.emp_id, e.name.name
from employee_info e;
EMP_ID NAME.NAME
--------------- ---------------
1 Joe Bloggs
And nested attributes like:
select d.relation, d.name.name, d.age
from the(select e.dependents from employee_info e where emp_id = '1') d;
RELATION NAME.NAME AGE
--------------- --------------- ----------
Daughter Emily Bloggs 14
I imagine you're manipulating these in PL/SQL, but the principle is the same.
Not sure why you're storing emp_id and salary as strings though.

Subtype Supertype with Oracle Object Type Creation. Limit on the number of subtypes?

I have run into an issue when creating a object type in Oracle 10g that inherits from a supertype. We currently have many object types that inherit from this supertype and recently the compiler started throwing the following errors
ORA-30745: error occured while trying to add column "SYS_NC_ROWINFO$" in table "DATA_CACHE.CACHE_ENTRIES"
ORA-01792: maximum number of columns in a table or view is 1000
Is there a cap on the number of subtypes you can generate that inherit from a supertype?
When you create tables with columns based on user-defined types, Oracle creates additional "secret" columns for you under the covers. For example:
SQL> create type emp_data_t as object (empno number, ename varchar2(30));
2 /
Type created.
SQL> create table emp_data_table (id int, emp_data emp_data_t);
Table created.
This table appears to have 2 columns:
SQL> desc emp_data_table
Name Null? Type
-------------------------- -------- ------------------------
ID NUMBER(38)
EMP_DATA EMP_DATA_T
... but it really has four:
SQL> select name
2 from sys.col$
3 where obj# = (select object_id
4 from user_objects
5 where object_name='EMP_DATA_TABLE');
NAME
------------------------------
ID
EMP_DATA
SYS_NC00003$
SYS_NC00004$
As you have seen, Oracle has a limit of 1000 columns per table. This limit will include any of these hidden columns derived from types and supertypes. It looks like your table has exceeded this limit.
use command:
PURGE RECYCLEBIN;

Resources