Invalid Identifier error for scope - sql

I have created customer_ty object which includes a nested table called "deposits".
CREATE TYPE deposit_ty as object(
depNo number,
depCategory ref depcategory_ty,
amount number,
period number
)
/
This 'depCategory' reference depcategory_ty in another table called 'depcategory_tbl'.
CREATE TYPE deposit_ntty as table of depcategory_ty
/
CREATE TYPE address_ty as varray(3) of varchar2(20)
/
CREATE TYPE customer_ty as object(
custId varchar2(4),
custName varchar2(10),
address address_ty,
dob date,
deposits deposit_ntty
)
/
CREATE TABLE customer_tbl of customer_ty(
custId primary key)
nested table deposits store as cli_deposit_tbl
/
alter table cli_deposit_tbl
add scope for (depCategory) is depcategory_tbl
/
The problem appears when I try to add a scope for table. it says;
add scope for (depCategory) is depcategory_tbl
*
ERROR at line 2:
ORA-0094:"DEPCATEGORY":Inavalid Identifier
All the identifiers are correct. What is wrong with this?

It looks like you've defined deposit_ntty incorrectly, and meant it to be:
CREATE TYPE deposit_ntty as table of deposit_ty
/
With that change, your alter now gets:
alter table cli_deposit_tbl
add scope for (depCategory) is depcategory_tbl
/
ORA-22892: scoped table "DEPCATEGORY_TBL" does not exist in schema "PUBLIC"
You may already have that but it isn't shown; with it there the alter works:
CREATE TABLE depcategory_tbl of depcategory_ty;
Table DEPCATEGORY_TBL created.
alter table cli_deposit_tbl
add scope for (depCategory) is depcategory_tbl
/
Table CLI_DEPOSIT_TBL altered.

Related

ORACLE : How to add a nested table to an existing type [duplicate]

Why I get ORA-22913 when I want to alter table? ("must specify table name for nested table column or attribute")
DROP TYPE salaryRaise FORCE;
/
CREATE TYPE salaryRaise AS OBJECT(
newSalary NUMBER,
raiseDate DATE);
/
DROP TYPE salaryList FORCE;
/
CREATE TYPE salaryList IS TABLE OF salaryRaise;
/
ALTER TABLE emp ADD listOfSalaries salaryList;
NESTED TABLE listOfSalaries STORE AS salaryList_tab;
I found the solution
ALTER TABLE emp ADD listOfSalaries salaryList NESTED TABLE listOfSalaries STORE AS salaryList_tab;

Oracle Object-Relational - PLS-00538 error and PLS-00539 error

Type definition:
CREATE TYPE INTERVENTO_TY AS OBJECT(
Testo VARCHAR2(20),
Timestmp DATE
) NOT FINAL;
CREATE TYPE COMMENTO_TY UNDER INTERVENTO_TY(
Ordine VARCHAR2(20)
);
CREATE TYPE POST_TY UNDER INTERVENTO_TY(
Titolo VARCHAR2(20),
MEMBER PROCEDURE AddCom(Ordine VARCHAR2, Testo VARCHAR2, Timestmp Date)
);
CREATE TYPE AUTORE_TY AS OBJECT(
Nome VARCHAR2(20),
Cognome VARCHAR2(20),
IdAutore INT
);
CREATE TYPE MEDIA_TY AS OBJECT(
Tipo VARCHAR2(20),
Nome VARCHAR2(20),
IdMedia INT,
Titolo VARCHAR2(20)
);
CREATE TYPE SCRIVE_INTERVENTO AS TABLE OF REF INTERVENTO_TY;
CREATE TYPE DI_AUTORE AS TABLE OF REF AUTORE_TY;
ALTER TYPE INTERVENTO_TY ADD ATTRIBUTE DI DI_AUTORE CASCADE;
ALTER TYPE AUTORE_TY ADD ATTRIBUTE SCRIVE SCRIVE_INTERVENTO CASCADE;
CREATE TYPE IN_INTERVENTO AS TABLE OF REF INTERVENTO_TY;
CREATE TYPE CONTIENE_MEDIA AS TABLE OF REF MEDIA_TY;
ALTER TYPE INTERVENTO_TY ADD ATTRIBUTE CONTIENE CONTIENE_MEDIA CASCADE;
ALTER TYPE MEDIA_TY ADD ATTRIBUTE IN_INT IN_INTERVENTO CASCADE;
CREATE TYPE COMMENTATO_POST AS TABLE OF REF COMMENTO_TY;
ALTER TYPE COMMENTO_TY ADD ATTRIBUTE A REF POST_TY CASCADE;
ALTER TYPE POST_TY ADD ATTRIBUTE COMMENTATO COMMENTATO_POST CASCADE;
CREATE TABLE AUTORE_TAB OF AUTORE_TY
NESTED TABLE SCRIVE STORE AS SCRIVE_NESTED;
CREATE TABLE INTERVENTO_TAB OF INTERVENTO_TY
NESTED TABLE DI STORE AS DI_NESTED
NESTED TABLE CONTIENE STORE AS CONTIENE_NESTED;
CREATE TABLE MEDIA_TAB OF MEDIA_TY
NESTED TABLE IN_INT STORE AS IN_INT_NESTED;
I'm trying to write an object procedure that creates a new instance of COMMENTO_TY, adds it to the COMMENTO_TAB table and the COMMENTATO nested table of POST_TY, but it gives me the following error:
CREATE OR REPLACE TYPE BODY Post_ty AS
MEMBER PROCEDURE addComm(commento VARCHAR2, ordine NUMBER, IdAut INTEGER) AS
NuovoCommento REF Commento_ty;
BEGIN
INSERT INTO Intervento_tab I
VALUES(Commento_ty(commento, SYSTIMESTAMP,
DI_AUTORE(
(SELECT REF(A) FROM AUTORE_TAB A WHERE IdAutore = IdAut)
),
CONTIENE_MEDIA(),
ordine,
(SELECT TREAT(REF(I) AS REF POST_TY)
FROM INTERVENTO_TAB I
WHERE VALUE(I) IS OF (POST_TY) AND
I.TIMESTMP = SELF.TIMESTMP)))
RETURNING REF(I) INTO NuovoCommento;
INSERT INTO TABLE(SELECT TREAT(VALUE(I) AS POST_TY).COMMENTATO
FROM INTERVENTO_TAB I
WHERE VALUE(I) IS OF (POST_TY)
AND I.TIMESTMP = SELF.TIMESTMP)
VALUES(NuovoCommento);
END addComm;
END;
/
PLS-00538: subprogram or cursor 'ADDCOM' is declared in an object type specification and must be defined in the object type body at line 3
PLS-00539: subprogram 'ADDCOMM' is declared in an object type body and must be defined in the object type specification at line 2
You have two errors:
PLS-00538: subprogram or cursor 'ADDCOM' is declared in an object type specification and must be defined in the object type body
and
PLS-00539: subprogram 'ADDCOMM' is declared in an object type body and must be defined in the object type specification
Notice how one is spelt ADDCOM and the other is ADDCOMM; you need to make sure the member procedure has the same name in the specification and the body.

Can I change an attribute name from a table derived from a type?

Folowing the Object-Relational Database model, I wanted to create the tables or_doctor and or_recepcionist derived from the type t_employee.
Here, follows the type structure:
DROP TYPE t_employee FORCE;
CREATE OR REPLACE TYPE t_employee AS OBJECT (
num_employee INTEGER,
name_employee VARCHAR2(50),
birthdate_employee DATE
);
And here, the tables' structure:
DROP TABLE or_doctor CASCADE CONSTRAINTS;
CREATE TABLE or_doctor OF t_employee (
PRIMARY KEY (num_employee),
name_employee NOT NULL,
birthdate_employee NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
DROP TABLE or_recepcionist CASCADE CONSTRAINTS;
CREATE TABLE or_recepcionist OF t_employee (
PRIMARY KEY (num_employee),
name_employee NOT NULL,
birthdate_employee NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
Doing so, the attributes names, on both tables, will end up with "employee". Could I change the attribute name so they are specific in each table at the moment I'm creating the table?
E.G.:
Table or_doctor: num_doct, name_doct, birthdate_doct.
Table or_recepcionist: num_recep, name_recep, birthdate_recep.
As a frame challenge, don't add a suffix to your identifiers then you don't need to worry about the suffix being incorrect:
CREATE TYPE t_employee AS OBJECT (
num INTEGER,
name VARCHAR2(50),
birthdate DATE
);
CREATE TABLE or_doctor OF t_employee (
PRIMARY KEY (num),
name NOT NULL,
birthdate NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
CREATE TABLE or_receptionist OF t_employee (
PRIMARY KEY (num),
name NOT NULL,
birthdate NOT NULL
) OBJECT IDENTIFIER IS SYSTEM GENERATED;
If you try to rename the column:
ALTER TABLE or_doctor RENAME COLUMN name TO name_doctor;
Then you will get the error:
ORA-23291: Only base table columns may be renamed
If you are using object-derived tables then you appear to be stuck with the identifiers from the object; so, make the object names generic so that they are appropriate in every place they are going to be used.

creating schema with a table containing serial field

How do I create (in a single SQL command) a schema and a table in it, but with the table containing a serial column (in Postgres) ?
For example, here I am attempting to create schema zoo with table animals from type animal_t with serial column animal_id:
DROP TYPE IF EXISTS animal_t CASCADE;
CREATE TYPE animal_t AS (
animal_id integer,
animal_name varchar
);
CREATE SCHEMA zoo
CREATE TABLE animals OF animal_t
animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_animal_id_seq')
CREATE SEQUENCE animals_animal_id_seq OWNED by animals.animal_id
;
Notes:
CREATE SCHEMA only accepts CREATE TABLE or CREATE SEQUENCE, it does not accept ALTER , this is why I have to do all of this in a single SQL sentence.
Result:
-bash-4.3$ psql dev < animal.sql
DROP TYPE
CREATE TYPE
ERROR: syntax error at or near "animal_id"
LINE 3: animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_a...
^
-bash-4.3$
You have two options
simplify your statement and get rid of the object type
CREATE SCHEMA zoo
CREATE table animal
(
animal_id serial,
animal_name varchar
);
use a search path if you want to avoid to prefix the table with a schema name:
DROP TYPE IF EXISTS animal_t CASCADE;
CREATE TYPE animal_t AS (
animal_id integer,
animal_name varchar
);
CREATE SCHEMA zoo;
set search_path = zoo;
CREATE SEQUENCE animals_animal_id_seq;
CREATE TABLE animals OF animal_t
animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_animal_id_seq');
alter sequence animals_animal_id_seq owned by animals.animal_id;
After creating the type and the schema, the current schema is set to the just created one, so all subsequent statements use zoo as the default schema.
Note that you can do this in a single transaction if that is another reason for you to use the "extended" create schema syntax.
I think the following should do it. The idea is to ALTER schema_name.thing:
CREATE SCHEMA zoo
CREATE TABLE animals OF animal_t ( animal_id WITH OPTIONS NOT NULL )
;
CREATE SEQUENCE zoo.animals_animal_id_seq OWNED BY zoo.animals.animal_id;
ALTER TABLE zoo.animals ALTER animal_id SET DEFAULT nextval('zoo.animals_animal_id_seq');

oracle11g sql: Warning: Type created with compilation errors

I'm trying to create a supertype customer service and subtype agent and supervisor, so they can inherent values however when I try to run this in oracle sql: a message comes up
Warning: Type created with compilation errors.
What is wrong with the code below?
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint supervisor_pk primary key(csID) );
You can use show errors in SQL*Plus or SQL Developer, or select * from user_errors, to see the error details.
Since you've shown six commands and the warning is about one of the first three (since it refers to type), and they appear OK independently apart from the constraint pointed put in comments, it looks like the whole script is being imterpreted as one command. It depends on your client settings, but you probably just need to seperate the commands with a / to cause them to execute. Becuase types can include PL/SQL the ; isn't treated as a statement seperator. So:
Create or replace type customer_s_type as object (
csID number,
csName varchar(15),
csType number ) NOT FINAL;
/
Create or replace type supervisor_type UNDER customer_s_type ( title varchar (10) );
/
Create or replace type agent_type UNDER customer_s_type (title varchar (10));
/
Create table supervisor of supervisor_type (
CONSTRAINT supervisor_PK PRIMARY KEY (csID));
Create table agent of agent_type (CONSTRAINT agent_PK PRIMARY KEY (csID));
create table customer_service(
csID number(10),
csType number(10),
constraint customer_service_pk primary key(csID) );