Error While Creating Nested Table in Oracle - oracle

Below is the Tables i have created.
CREATE TYPE ft_obj AS OBJECT (
ftid NUMBER(5),
ftlocation VARCHAR(30),
country VARCHAR(10)
);
/
CREATE TABLE ft_table OF ft_obj (
ftid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;
/
CREATE TYPE frod_obj AS OBJECT (
prodid NUMBER(6),
ft_ref ft_obj,
proddesc VARCHAR(50),
costperitem DECIMAL,
labcostperitem DECIMAL
);
/
CREATE TABLE frod_table OF frod_obj (
prodid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;
CREATE TYPE wf_obj AS OBJECT (
wfid NUMBER,
ft_ref ft_obj,
wfname VARCHAR(30),
taxcode INT,
yearlyincome DECIMAL,
yearlytax DECIMAL
);
/
CREATE TABLE wf_table OF wf_obj (
wfid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;
/
CREATE TYPE wfusage_obj AS OBJECT (
jobdate DATE,
jobhours INT,
jobhourlyrate DECIMAL,
jobposted CHAR,
wfid_ref REF wf_obj
);
/
CREATE TYPE wfusage_nesttabtyp AS
TABLE OF wfusage_obj;
/
CREATE TABLE wfusage_objtab OF wfusage_obj;
/
CREATE TYPE odetails_obj AS OBJECT (
mfid NUMBER,
prodid_ref REF frod_obj,
quantity INT,
itemprice DECIMAL,
wfusage_ntab wfusage_nesttabtyp
);
/
CREATE TYPE odetails_nesttabtyp AS
TABLE OF odetails_obj;
/
CREATE TYPE prod_obj AS OBJECT (
prodoid NUMBER,
odate DATE,
promisedate DATE,
completiondate DATE,
shipmentdate DATE,
status VARCHAR(20),
odetails_ntab odetails_nesttabtyp
);
/
CREATE TABLE prod_objtab OF prod_obj (
PRIMARY KEY ( prodoid )
) OBJECT IDENTIFIER IS PRIMARY KEY
NESTED TABLE odetails_ntab STORE AS oprod_ntab ( (
PRIMARY KEY ( nested_table_id,
mfid )
)
ORGANIZATION INDEX
COMPRESS ) RETURN AS LOCATOR
/
ALTER TABLE oprod_ntab ADD (
SCOPE FOR ( prodid_ref ) IS frod_table
);
/
Getting the below error while creating Nested Table.
ORA-02320: failure in creating storage table for nested table column
odetails_ntab ORA-25175: no PRIMARY KEY constraint found
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.
*Action: See the messages that follow for more details. If the situation
they describe can be corrected, do so; otherwise contact Oracle
Support.
INSERT INTO prod_objtab VALUES ( 45000,
'12-April-2019',
'01-MAy-2019',
'01-MAy-2019',
'01-MAy-2019',
'COMPLETED',
odetails_nesttabtyp()
);
INSERT INTO TABLE (SELECT pr.odetails_ntab FROM prod_objtab pr WHERE pr.prodorderid = 45000 )
values (45001,(SELECT REF(pt) FROM frod_table pt WHERE pt.prodid = 10001 ),100,500,
wfusage_nesttabtyp(wfusage_obj('12-April-2019',60,100,'AME',
(SELECT REF(wf) FROM wf_table wf WHERE wf.wfid = 240))));
getting the error in line 9
ORA-01401: inserted value too large for column

ORA-02320: failure in creating storage table for nested table column
odetails_ntab ORA-25175: no PRIMARY KEY constraint found 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. *Action: See the messages that follow
for more details. If the situation they describe can be corrected, do
so; otherwise contact Oracle Support.
Since you have done Multi-level nesting, while creating the table you need 2 levels of Storage as well for the Nested tables. See below how you can do it.
CREATE TABLE prod_objtab OF prod_obj (
PRIMARY KEY ( prodoid )
) OBJECT IDENTIFIER IS PRIMARY KEY
NESTED TABLE odetails_ntab STORE AS oprod_ntab ( ( PRIMARY KEY (NESTED_TABLE_ID, mfid ))
ORGANIZATION INDEX COMPRESS
NESTED TABLE wfusage_ntab STORE AS XX ) RETURN AS LOCATOR;
Read more at
https://docs.oracle.com/en/database/oracle/oracle-database/18/adobj/multilevel-collection-types.html#GUID-76D5A6B0-28AD-483D-942C-B7F3B90AC379

Related

Inconsistent datatypes: expected CHAR got REF

CREATE TYPE empresa_type AS OBJECT (
CNPJ INTEGER,
nome_fantasia VARCHAR2(30),
pais VARCHAR2(25),
fundacao DATE
)
CREATE TYPE funcionario_type AS OBJECT (
CPF INTEGER,
nome VARCHAR2(30),
sexo CHAR(1),
nasc DATE,
empresa REF empresa_type
)
CREATE TABLE empresa_tab OF empresa_type (PRIMARY KEY(CNPJ))
CREATE TABLE funcionario_tab OF funcionario_type (PRIMARY KEY(CPF), FOREIGN KEY(empresa) REFERENCES empresa_tab)
report error:
ORA-00932: inconsistent datatypes: expected CHAR got REF MY_WKSP.EMPRESA_TYPE
You want to create the table and then add a SCOPE to the reference (rather than trying to define it as a foreign key, because it is not):
CREATE TABLE funcionario_tab OF funcionario_type (
PRIMARY KEY(CPF)
);
ALTER TABLE funcionario_tab ADD SCOPE FOR ( empresa ) IS empresa_tab;
db<>fiddle here

ORA-00909 error when Insert object in object table

I am getting an error ORA-00909 in when inserting an object of a superType in an object table. These are the definitions for the objects:
CREATE OR REPLACE TYPE address AS OBJECT (
street VARCHAR(20),
country VARCHAR(20),
province VARCHAR(20),
city VARCHAR2(20),
zipcode VARCHAR(10)
) FINAL;
CREATE OR REPLACE TYPE company AS OBJECT (
CIF VARCHAR2(9),
code VARCHAR2(10),
name VARCHAR2(20),
signUpDate DATE,
email VARCHAR2(20),
adminAddress address
) NOT FINAL;
CREATE OR REPLACE TYPE inCourseCompany UNDER company (
postalAddress address,
numEmployees NUMBER
) FINAL;
And the object table:
CREATE TABLE companies_objtab OF company (PRIMARY KEY CIF) OBJECT IDENTIFIER IS PRIMARY KEY;
I try to insert an object with the following statement:
INSERT INTO companies_objtab VALUES (
company('J12345678','000001','Test Company',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''))
);
and I get error Error
SQL: ORA-00909: invalid number of arguments
00909. 00000 - "invalid number of arguments"
However, when I insert an object of the subtype inCourseCompany it is inserted correctly:
INSERT INTO companies_objtab VALUES (
inCourseCompany('G11111111','','',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''), address('','','','',''), 100)
);
Any hint about what causes the error?
I am using Oracle SQL Developer 4.0.2.15.21 and Oracle Database Express Edition 11g Release 2.
Thank you in advance.
i tried all your statements right as you posted them (copy-paste). Both on 12c and 11gR2. Everything worked, both inserts. The only thing I noticed is that your create table is a bit incorrect. This is the correct one (but this does not explain the error you get). HTH KR
CREATE TABLE companies_objtab OF company (CIF PRIMARY KEY) OBJECT IDENTIFIER IS PRIMARY KEY;
Maybe try dropping the table and creating it again.

Insertion in Nested Tables on Oracle DB

I am a new learner of PL/SQL databases,A kind of exercise given to apply database on apex.oracle.com with given sequence.Then I have created tables but when it comes to fill tables with the insertion code as follows,Application has given error,Would you mind if I need your assistance
Thanks in Advance,
CREATE TYPE TEMPORAL_VARCHAR AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART VARCHAR2(50)
);
CREATE TYPE TEMPORAL_NUMBER AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART NUMBER );
Time-related attributeshave defined with the code as follows;
CREATE TYPE NAME_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE ADDRESS_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE DEPARTMENT_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE MANAGER_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE SALARY_TYPE AS TABLE OF TEMPORAL_NUMBER;
CREATE TABLE EMPLOYEE (
SSN NUMBER primary key,
NAME NAME_TYPE,
ADDRESS ADDRESS_TYPE ,
BIRTH_DATE DATE,
MANAGER MANAGER_TYPE ,
DEPARTMENT DEPARTMENT_TYPE,
SALARY SALARY_TYPE
)
NESTED TABLE NAME STORE AS NAME_TABLE,
NESTED TABLE ADDRESS STORE AS ADDRESS_TABLE,
NESTED TABLE MANAGER STORE AS MANAGER_TABLE,
NESTED TABLE DEPARTMENT STORE AS DEPARTMENT_TABLE,
NESTED TABLE SALARY STORE AS SALARY_TABLE
;
And the insertion that I am inteded to do
INSERT INTO EMPLOYEE VALUES
(101,
NAME(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','James Brown')),
ADDRESS(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','BUCA, IZMIR')),
'23.10.1986',
MANAGER(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','Mike White')),
DEPARTMENT(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','DEPT_ID05')),
SALARY(TEMPORAL_NUMBER('23.11.2005',’12.31.9999’, 250000))
);
And the error message I recieved is :
ORA-00904: "SALARY": invalid identifier
There are spaces before _ here
CREATE TYPE MANAGER _TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE SALARY _TYPE AS TABLE OF TEMPORAL_NUMBER;
DEPARTMENT DEPARTMENT _TYPE
A comma is missing after DEPARTMENT _TYPE
try this solution:
'alter session set NLS_DATE_FORMAT='DD.MM.YYYY';'
----------------------------------------------------
INSERT INTO EMPLOYEE VALUES
(101,
NAME_TYPE(TEMPORAL_VARCHAR('23.10.1986','09.09.9999','James Brown')),
ADDRESS_TYPE(TEMPORAL_VARCHAR('15.12.2009','09.09.9999','BUCA')),
'23.10.1986',
MANAGER_TYPE(TEMPORAL_VARCHAR('24.05.2008','09.09.9999','Mike White')),
DEPARTMENT_TYPE(TEMPORAL_VARCHAR('03.01.2012','09.09.9999','DEPT_ID05')),
SALARY_TYPE(TEMPORAL_NUMBER('01.01.2003','09.09.9999', 3200))
);

how to add ref type as primary key in oracle?

Consider the following relational schema.
Words in bold color are primary keys.
branches(**bname**:char(15),city:varchar(12), phone:varchar(10))
customers(**custno**:integer,cname:char(15),gender:char(1), bdate:date)
accounts(**accno**:integer, branch:char(15),acctype:char(1),balance:real)
ac(**accont:integer,customer**:integr)
columns in bold color are primary keys.
in ac, account and customer references account_ty and customer_ty respectively.
My problem is in ac table (account,customer) is a composite primary key. Since they are ref types, how can i add primary key in ac table.
I want to create object tables. First I created types and then tables from them.Below are my code
create type branch_ty as object(
bname char(15),
city varchar(12),
phone varchar(12)
)
/
create type customer_ty as object(
custno integer,
cname char(15),
gender char(1),
bdate date
)
/
create type account_ty as object(
accno integer,
branch REF branch_ty,
acctype char(1),
balance real
)
/
create type ac_ty as object(
account REF account_ty,
customer REF customer_ty
)
/
create table branch of branch_ty(
constraint br_pk primary key(bname)
)
/
create table customers of customer_ty(
constraint cu_pk primary key(custno),
constraint cu_ch check (gender='F' or gender='M')
)
/
create table accounts of account_ty(
constraint acc_pk primary key(accno),
constraint acc_fk foreign key(branch) references branch,
constraint acc_ch check(acctype='I' or acctype='J')
)
/
I am OK up to this point. But when I try to execute the following code segment it gives this error message.ORA-02329: column of datatype REF cannot be unique or a primary key
create table ac of ac_ty(
constraint ac_fk1 foreign key(account) references accounts,
constraint ac_fk2 foreign key(customer) references customers,
primary key(account,customer)
)
/
Please help me. I know i can do this without using object tables.But I want to create it using object tables. Is this possible? Please help me........

oracle change a column to unique

Table has been created in system this way
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The new crete table statement is as below:
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The differnce is INSTANCEID has UNIQUE in it. How do i Alter the table? I used the below statement and it did not work for me.
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID);
It gave an error:
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID)
Error report:
SQL Error: ORA-02261: such unique or primary key already exists in the table
02261. 00000 - "such unique or primary key already exists in the table"
*Cause: Self-evident.
*Action: Remove the extra key.
Please help me to Alter the table as required above. Thanks!
Here is the output of SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES';
"CONSTRAINT_NAME","COLUMN_NAME","CONSTRAINT_TYPE"
"SYS_C0016531","DM","C"
"SYS_C0016532","INSTANCEID","C"
"SYS_C0016533","INSTANCENAME","C"
"SYS_C0016534","HOSTNAME","C"
"PK_INSTANCES","HOSTNAME","P"
"PK_INSTANCES","INSTANCEID","P"
"SYS_C0016536","INSTANCENAME","U"
You have already stated that INSTANCEID is supposed to be UNIQUE, so a constraint has been created.
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE, -- UNIQUE constraint
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
Edit: Ok, after reading your comment, try this:
SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES'
AND con.constraint_type = 'U'
;
It will list UNIQUE constraints and associated columns for INSTANCE table. Please check if there is a unique constraint on the INSTANCEID column (and if that constraint has no other associated columns).
Example at SQLFiddle: http://sqlfiddle.com/#!4/43b43/6
Edit #2: creating named constraints, all options:
-- CREATE TABLE - "In Line" Constraints
CREATE TABLE ports (
ID NUMBER CONSTRAINT PORT_ID_PK PRIMARY KEY,
NAME VARCHAR2(20)
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_NN NOT NULL
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_UQ UNIQUE
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER CONSTRAINT SHIP_PORT_FK
REFERENCES PORTS (ID)
);
-- CREATE TABLE - "Out of Line" Constraints
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT PORT_ID_PK PRIMARY KEY (ID)
);
-- NOT NULL constraints can not be created "Out of Line"!
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT NAME_UQ UNIQUE (NAME)
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER,
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER,
CONSTRAINT SHIP_PORT_FK FOREIGN KEY
(HOME_PORT_ID) REFERENCES PORTS (ID)
);
-- ALTER TABLE - "In Line" Constraints
ALTER TABLE PORTS MODIFY ID
CONSTRAINT PORT_ID_PK PRIMARY KEY;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_NN NOT NULL;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_UQ UNIQUE;
ALTER TABLE SHIPS MODIFY HOME_PORT_ID
CONSTRAINT SHIP_PORT_FK REFERENCES PORTS (ID);
-- ALTER TABLE - "Out of Line" Constraints
ALTER TABLE PORTS ADD CONSTRAINT
PORT_ID_PK PRIMARY KEY (ID);
-- NOT NULL constraints can not be created "Out of Line"!
ALTER TABLE PORTS ADD CONSTRAINT
NAME_UQ UNIQUE (NAME);
ALTER TABLE PORTS ADD
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5));
ALTER TABLE SHIPS ADD CONSTRAINT SHIP_PORT_FK
FOREIGN KEY (HOME_PORT_ID)
REFERENCES PORTS (ID);
NOT NULL constraints cannot be create of out line.

Resources