Inconsistent datatypes: expected CHAR got REF - oracle

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

Related

Error While Creating Nested Table in 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

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.

invalid number error when inserting a row in oracle

I need to insert a row to a table in oracle.
insert into policy_tab values ('4325','29-APR-98','29-APR-2007',32424,(select ref(a) from agent_tab a where a.nic='242424v'),claim_ntty(
claim_t('25-APR-2005','25-JUN-2005'),
claim_t('26-APR-2005','26-JUN-2005')
));
But when I executed it will show this error. "ORA-01722: invalid number ORA-06512: at "SYS.DBMS_SQL", line 1721"
create type policy_ty as object(
pid char(5),
sDate date,
eDate date,
premium number(10,2),
agent ref agent_ty,
claims claim_ntty
);
create table policy_tab of policy_ty(
pid primary key,
agent SCOPE IS agent_tab
)
nested table claims store as claim_nttab;
create type claim_t AS OBJECT(
eDate date,
amount number(10,2)
);
create type claim_ntty as table of claim_t;
create type agent_ty as object(
nic char(10),
name varchar(50),
address varchar(50),
contactNo contactNo_vaty
) NOT FINAL;
create table agent_tab of agent_ty(
nic primary key
);
So how to resolve it?
From the first look you need to use like below. However the information provided is not sufficient to resolve the issue. Related Objects defintion as well needed.
This has to be changed - '29-APR-98' to '29-APR-1998'
INSERT INTO policy_tab
VALUES (
'4325',
'29-APR-1998',
'29-APR-2007',
32424,
(SELECT REF (a)
FROM agent_tab a
WHERE a.nic = '242424v'),
claim_ntty (claim_t ('25-APR-2005', '25-JUN-2005'),
claim_t ('26-APR-2005', '26-JUN-2005')));
Edit:
Second observation. You created the below object:
create type claim_t AS OBJECT(
eDate date,
amount number(10,2)
);
And using it like:
claim_ntty (claim_t ('25-APR-2005', '25-JUN-2005'),
claim_t ('26-APR-2005', '26-JUN-2005')));
Second Argument should be number not date.
So your insert should be:
INSERT INTO policy_tab
VALUES (
'4325',
'29-APR-1998',
'29-APR-2007',
32424,
(SELECT REF (a)
FROM agent_tab a
WHERE a.nic = '242424v'),
claim_ntty (claim_t ('25-APR-2005', 123), claim_t ('26-APR-2005', 456)));

Error query oracle db in toad

I've executed following query in toad:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
UNIQUE (id)
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL;
And got this ORA message:
ORA-00907 missing right parenthesis
Cause: A left parenthesis has been entered without a closing right parenthesis, or extra information was contained in the parentheses. All parentheses must be entered in pairs.
Action: Correct the syntax and retry the statement.
There are a couple of errors in your syntax. The NOT NULL has to occur after the IDENTITY clause. The range has to be specified without a , and the UNIQUE keyword has to appear at the column directly:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL UNIQUE,
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
)
LOGGING NOCOMPRESS NOCACHE NOPARALLEL;
You specified unique not right, to solve it you have several ways:
First, create unique key explicitly:
create table tab1(
id number(10),
CONSTRAINT id_uk UNIQUE (id)
)
Second, create unique key as an option of the column:
create table tab1(
id number(10) UNIQUE
)
Third, add the unique key constraint using alter table:
create table tab1(
id number(10)
);
alter table tab1 add constraint id_uk unique(id);
Forth, create unique key implicitly by creating unique index:
create table tab1(
id number(10)
);
CREATE UNIQUE INDEX id_uk ON tab1 (id);

Oracle Create TABLE

I am trying to create a table with foreign key using oracle. My syntax is as follows
CREATE TABLE product (
product_id INT(7) NOT NULL,
supplier_id INT(7) NOT NULL,
product_name VARCHAR2(30),
product_price DOUBLE(4),
product_category VARCHAR2(30),
product_brand VARCHAR2(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
)
I got a error, saying
Error at Command Line:2 Column:14 Error report: SQL Error: ORA-00907:
missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Please help!
Your foreign key should refference another column on another table.
Here is the documentation you need to fix your issue (how to write the query with the correct syntax for foreign key)
Also, change your data type for column product_price from DOULBE(4) to NUMBER(12,4).
You should not use limit for int type...oracle will take default length for int type .
Instead of int you can use Number type to make it run. And DOUBLE PRECISION is a data type in oracle but Double is not there. Also , syntax for foreign key is wrong.
so this query will work for sure :
CREATE TABLE product(
product_id number(7) NOT NULL,
supplier_id number(7) NOT NULL,
product_name VARCHAR2(30),
product_price DOUBLE PRECISION,
product_category VARCHAR2(30),
product_brand VARCHAR2(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
REFERENCES parent_table (supplier_id)
);
You create like foreign key references parent table that is the proper syntax for creating the foreign key
CREATE TABLE product(
product_id number(7) NOT NULL,
supplier_id number(7) NOT NULL,
product_name VARCHAR(30),
product_price DOUBLE PRECISION,
product_category VARCHAR(30),
product_brand VARCHAR(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
REFERENCES parent_table (supplier_id)
);

Resources