OOP concepts in oracle 11.g - oracle

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.

Related

Impossible to create trigger in a table with a reserved name in Oracle

I have a table named BLOB, and I can select, update, insert normally via SQL, but it's impossible to define a trigger for it, obviously the cause is the name.
In PL/SQL as a procedure, I work around the problem by creating a view which selects all the columns of the table, and using it in the body but it doesn't work on the table itself.
Does anyone have a solution? I cannot rename the table. Also using a synonym is out.
Structure of the table:
CREATE TABLE BLOB
(
BLOBID CHAR(7 BYTE) NOT NULL,
OGGETTO BLOB
)
The problem is related to the presence of a column of type BLOB in a table named BLOB, but only in PL/SQL environment (procs, triggers, ...)

how to store an integer value in to sql table

i created this table and i want to be able to add Integer value of 99 in to Assgn_Id and Ben_Id. so i am very confident with my create table query.
Create table Advisor (
Advisor# Integer,
AdvisorName Varchar2(15),
Assgn_Id Integer,
Ben_Id Integer);
my second query is where i get confused. after i inserted the values into the table it seems like it work fine, but when i describe the table i see Number(38) datatype into both columns instead of Integers(99).
Insert Into Advisor (Assgn_Id, Ben_Id)
Values(99, 99);
Integer, INT, smallint are ansi defined data types - that Oracle has implemented as a number
I like to use integer for my tables because I'm lazy, it's easier to type.
From the docs..
SQL statements that create tables and clusters can also use ANSI data
types and data types from the IBM products SQL/DS and DB2. Oracle
recognizes the ANSI or IBM data type name that differs from the Oracle
Database data type name. It converts the data type to the equivalent
Oracle data type, records the Oracle data type as the name of the
column data type, and stores the column data in the Oracle data type
based on the conversions shown in the tables that follow.

How do I select from a nested table column in Oracle using go?

I am using go1.10.1 and querying Oracle 11.2.0.4. My local Oracle client is a 12.2 instant client. The go Oracle driver is goracle 2.5.4.
I am querying a table with a nested table (id column) that I need to retrieve as a list or some other iterable object. The nested table is simply a table of numbers.
CREATE TYPE id_type IS TABLE OF NUMBER;
CREATE TABLE account
(account_name VARCHAR2(1024),
id id_type)
NESTED TABLE id STORE AS id_tab;
Can I use a native go type to retrieve the nested table or do I need to use a type in the goracle driver?
Can I use the standard database/sql package or do I need something additional like github.com/jmoiron/sqlx?

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.

Using TYPE in PLSQL for multiple tables

I am using TYPE to get the rowtype in tables. I want to use it to retrieve the rowtypes of Customer and Supplier (i.e. multiple tables).
Below is the code for Customers, how do I retrieve Customer and Supplier with the same code?
TYPE dept_table_type is table of CUSTOMER%ROWTYPE
INDEX BY PLS_INTEGER;
Thank you
You can use the ROWTYPE of a cursor to do what you're trying to do:
CURSOR MULTI_TABLE_CURSOR IS
SELECT *
FROM CUSTOMER
CROSS JOIN SUPPLIER;
TYPE DEPT_TABLE_TYPE IS TABLE OF MULTI_TABLE_CURSOR%ROWTYPE
INDEX BY PLS_INTEGER;
Note that you don't actually need to use the cursor if you don't have a use for it.
Best of luck.

Resources