How to give a field of an object the same type as a column of a table? - oracle

I would like to a object with a field that has the same type as column of a table.
create table TA (
TA_1 number
);
CREATE TYPE my_obj2 IS object
(
TA_1 TA.TA_1%type
);
ORA-24344: success with compilation error
Is it possible. If yes, what is wrong in this code?
code

You can't do that.
The CREATE TYPE document states:
This data type must be stored in the database; that is, either a predefined data type or a user-defined standalone collection type.

Related

PL/SQL Record and Table Variables inchanged

I am new to Oracle PL/SQL and have been given some code I am being asked to modify. I have been looking through the oracle documentation and think I know what it is doing but would like to get a verification as to if my understanding is correct or not.
Here is the code:
TYPE array_rec_type IS RECORD (gla_sub tblName.id%type);
v_closes_sc array_rec_type;
TYPE v_sc_type is TABLE OF v_closes_sc%TYPE INDEX BY BINARY_INTEGER;
sc_array v_sc_type;
Here is what I believe is being declared here:
TYPE array_rec_type IS RECORD (gla_sub tblName.id%type);
It looks like a collection is being declared using the column type from the id column from the tblName table. Which could be a number or UUID, etc.
v_closes_sc array_rec_type;
A variable of the newly created type is being created
TYPE v_sc_type is TABLE OF v_closes_sc%TYPE INDEX BY BINARY_INTEGER;
A new type is being created using the collection (Record) previously created a template for the table. The table will have a single column of ID.
sc_array v_sc_type;
A new variable is being created to hold the new table variable.
A record is not a collection, so your explanation is slightly confused. From the docs (which you've probably already looked at:
In a collection, the internal components always have the same data type, and are called elements. ... To create a collection variable, you either define a collection type and then create a variable of that type or use %TYPE.
In a record, the internal components can have different data types, and are called fields. You can access each field of a record variable by its name ... To create a record variable, you either define a RECORD type and then create a variable of that type or use %ROWTYPE or %TYPE.
So looking at what you said:
Here is what I believe is being declared here:
TYPE array_rec_type IS RECORD (gla_sub tblName.id%type);
This is a record, not a collection. In this case it has a single field called gla_sub which is indeed being declared using the data type of the id column from the tblName table. The use of %type means that, to some extent, you don't need to know what the data type actually is, and you may not have to change your code if that changes (if the size of a varchar2 column is increased, for instance).
v_closes_sc array_rec_type;
Correct - a variable of the newly created type is being created.
TYPE v_sc_type is TABLE OF v_closes_sc%TYPE INDEX BY BINARY_INTEGER;
This is a collection, specifically an associative array. It's a sparse array whose members are instance of the record type declared above, with a numeric index. That could also be declared using the type directly, as:
TYPE v_sc_type is TABLE OF array_rec_type INDEX BY BINARY_INTEGER;
sc_array v_sc_type;
Correct - a new variable is being created of that just-declared table type.
Having a record type with a single field seems a bit pointless, as you could have a collection based on the table column data type. Your existing code will be expecting that structure though, so changing it isn't quite trivial.

Oracle PL/SQL table of parent object type breaks when I add another inheriting child object type

I am using custom object types with Oracle PL/SQL, including several object types that inherit from a parent object. I have a TP_DOCUMENTS parent object, and child document types, such as TP_PUBLICATION, TP_CONTRACT, etc. We successfully created a table of TP_DOCUMENT and have added records of TP_PUBLICATION, TP_CONTRACT, and other child document records. However, I needed to create an additional type of document. Once I did this, it broke the DOCUMENTS table. How can I create additional child types, without breaking the table of the parent object (making me lose all the data previously contained in the parent object table!!)?
Here is some of my code:
create or replace TYPE "TP_DOCUMENT" AS OBJECT
(
...fields go here
) NOT FINAL
create or replace TYPE "TP_PUB_INSTRUCTION" UNDER TP_DOCUMENT()
CREATE TABLE DOCUMENTS OF TP_DOCUMENT
After creating these types (and others with additional fields), I created the table DOCUMENTs as shown above. I tried to create another sub-type, and the DOCUMENTS table broke.
MORE INFORMATION:
The error code message is as follows:
ORA-04063: table/view has errors
Cause: Attempt to execute a stored procedure or use a view that has errors. For stored procedures, the problem could be syntax errors or references to other, non-existent procedures. For views, the problem could be a reference in the view's defining query to a non-existent table. Can also be a table which has references to non-existent or inaccessible types.
Action: Fix the errors and/or create referenced objects as necessary.
Thank you!
UPDATE WITH ANSWER FROM COMMENTER BELOW:
I had unfortunately dropped a Sub-Type using the Force option. That likely was the cause for why my Documents table was corrupted. In the future, I will use the Validate command (see answer below).
Instead of FORCE you should use the VALIDATE option when dropping types:
VALIDATE
If you specify VALIDATE when dropping a type, then Oracle Database
checks for stored instances of this type within substitutable columns
of any of its supertypes. If no such instances are found, then the
database completes the drop operation.
This clause is meaningful only for subtypes. Oracle recommends the use
of this option to safely drop subtypes that do not have any explicit
type or table dependencies.
Here's an example:
create or replace type tp_document as object
(
a number
) not final;
create or replace type tp_pub_instruction under tp_document();
create table documents of tp_document;
--This fails with this error message:
--ORA-02303: cannot drop or replace a type with type or table dependents
drop type tp_pub_instruction;
--This works since there's no data with that type.
drop type tp_pub_instruction validate;

How to update a type as datatype reference in another table

create or replace type My_abc_typ FORCE as object
(
...
Pqr pqr_typ,
...
)
/
Actually the Table My_abc is created as
CREATE TABLE "ABC"."My_abc" OF "ABC"."My_abc_TYP"
(... ,
...)
and primary keys and indexes are there for this
the problem is in type i ve changed the data type of a particular column , and compiled it... its working fine.
but the table is not reflecting the change... its showing
(
...
Pqr (null),
...
)
in order to modify that column alone i executed the following query
alter table My_abc modify Pqr pqr_typ;
the following error appears
SQL Error: ORA-04063: table "ABC.My_abc" has errors
04063. 00000 - "%s has errors"
*Cause: Attempt to execute a stored procedure or use a view that has
errors. For stored procedures, the problem could be syntax errors
or references to other, non-existent procedures. For views,
the problem could be a reference in the view's defining query to
a non-existent table.
Can also be a table which has references to non-existent or
inaccessible types.
*Action: Fix the errors and/or create referenced objects as necessary.
Instead of re-creating type:
modify the typ attribute as:
ALTER TYPE my_abc_typ MODIFY ATTRIBUTE (pqr VARCHAR2(20))CASCADE NOT INCLUDING TABLE DATA;
Link: http://docs.oracle.com/cd/E11882_01/appdev.112/e11822/adobjadv.htm
Thanks.

Oracle check constraint for varray type

I have a custom type defined as this :
CREATE TYPE myType_t AS VARRAY(2) of char(10);
Is it possible to add a check constraint on the char(10) type, so myType_t items respect a certain regex? I tried things like
CREATE TYPE myType_t AS VARRAY(2) of char(10)
( constraint c_myType_format check ( regexp_like(IdontKnowWhatToWriteHere, '[:digit:]{8}'));
which obviously doesn't work... I thought maybe defining another type for the
AS VARRAY(2) of myOtherType_t
but here again I don't know where to put the regex check.
And yes, I also tried adding the constraint to the table that will use my type, but I can't find the correct syntax.
According to Oracle documentation:
Oracle does not support constraints on columns or attributes whose
type is a user-defined object, nested table, VARRAY, REF, or LOB

OOP concepts in oracle 11.g

i have a question related to Object Orienting programming in oracle database(11.g),generally i have studyed how to create object types or how to create table from existing object,but i am stoped on one topic,namely,suppose that we have created following type
create or replace type mono.Item_type as object(
item_id integer,
part REF mono.Part_type,
quantity integer);
where Part_type is already existed object type.
i know that i can create table from this object like this:
create table tablename of Item_type,for instance
create table item_list of Item_type
what what would be different,if instead of this,we use
create or replace type mono.Item_List as table of Item_type;
here inspite of this,that we use type keyword,we are creating table again and what is different between create table tablename of Item_type and
create or replace type mono.Item_List as table of Item_type;?
When you use create table tablename of Item_type, you're creating a table in the DB.
This table is a structure that can be used to store user's data .
When you use create or replace type mono.Item_List as table you're defining a new Datatype (similar to array).
This Datatype can be used as a type of a column in a DB table or as a type of a variable in plsql code.

Resources