Skip to main content

What is the difference between varray,nested table and asociative array in oracle sql plsql interview question and answer?

Collection:

It is the group of elements having the same data type and it is made of arrays, lists and sets. It is divided into three parts i.e. 
  1. Varray.
  2. Nested Tables.
  3. Associative array or index by tables.
It is used for storing large chunks of data and performing manipulation operations with the help of keyword BULK Collect. Collection has built in methods which are as follows:

count: It gives the number of methods in the collection.

delete: It will delete all the elements from the collection and setting count to zero.

delete(n): It will delete nth element from the collection. If mth element is null, then no action is performed.

delete(n,q): It will delete elements from nth position to q position.

exists(n): Return true if n element is present in the collection else returns false.

extend: It adds null element to the collection end.

extend(n): It add m element to the collection end.

extend(n,q): It add n copies of the qth element to the collection end.

first: It gives start index number in collection with integer subscript.

last: It gives end index number in collection with integer subscript.

next(n): It gives the index that succeeds nth index.

prior(n): It gives the index that precedes nth index.

trim: It will delete an element from the end of collection.

trim(n): It will delete nth element from the end of collection.


Collection Exceptions:

COLLECTION_IS_NULL:

This exception is thrown if the collection is null by default.

NO_DATA_FOUND:

It is thrown if either a select statement fetches no row, or a program point to an element that is deleted in a nested table. It is raised by an element which is unitialised in an index by table.

SUBSCRIPT_BEYOND_COUNT:

It is thrown if the subscript is more than the total number of elements in the collection.

SUBSCRIPT_OUTSIDE_LIMIT:

It is thrown when the subscript is beyond the threshold range.

VALUE_ERROR:

It is thrown if a subscript cannot be converted to the key type or its NULL. It is raised if a key is of type PLS_INTEGER range & subscript lies beyond the range. It can be held in the column of the database. It is used for doing manipulation on SQL operational work by joining tables. It is like the dynamic array so that the upper limit can be used of any type.


 VARRAY:

It stores the fixed number of elements, and it is like one dimensional array. Number of elements can be modified at the runtime. It is consecutive set of elements which is having the similar data type. It can be handled with the help of SQL statements. Process is not easy and flexible as in the nested channel.

 Maximum size of varray is defined in its type definition. It has sequentially memory management beginning with 1st subscript. Lowest location address point is the starting element. Highest location address point is the last element. All the elements presents are identified with an index.

This collection type has numeric subscript and dense characteristics.

TYPE V_Array_type IS VARRAY(12) OF VARCHAR2(20);
v_year V_Array_type :=V_Array_type();
BEGIN
v_year.extend(12);
v_year(1):='JANUARY';
v_year(2):='FEBRUARY'; 
v_year(3):='MARCH'; 
v_year(4):='APRIL'; 
v_year(5):='MAY'; 
v_year(6):='JUNE'; 
v_year(7):='JULY'; 
v_year(8):='AUGUST'; 
v_year(9):='SEPTEMBER'; 
v_year(10):='OCTOBER';
v_year(11):='NOVEMBER';
v_year(12):='DECEMBER';
END;






create table v_year_det(v_year varchar2(20));



DECLARE
TYPE V_Array_type IS VARRAY(12) OF VARCHAR2(20);
v_year V_Array_type :=V_Array_type();
BEGIN
v_year.extend(12);
v_year(1):='JANUARY';
v_year(2):='FEBRUARY'; 
v_year(3):='MARCH'; 
v_year(4):='APRIL'; 
v_year(5):='MAY'; 
v_year(6):='JUNE'; 
v_year(7):='JULY'; 
v_year(8):='AUGUST'; 
v_year(9):='SEPTEMBER'; 
v_year(10):='OCTOBER';
v_year(11):='NOVEMBER';
v_year(12):='DECEMBER';
FORALL I in v_year.first..v_year.last
insert into v_year_det values(v_year(i));
END;
/



select * from v_year_det;




NESTED TABLE:

It is like the single column table database or 1 dimensional array where the array size is dynamic. Its having subscript is of numeric type. It can be variable by providing the rows with subscript starting from 1.It is similar like an array. It can be held in the column of the database. It has both dense and sparse collection characteristics which means that any element can be deleted randomly (sparse feature) by DELETE procedure. 

Data deletion can cause discontinuity in an index. Next function helps to iterate to the next subscript. Data is retrieved with select statements which is stored in the tables. It can be built at the schema level or in the PLSQL block. It is like the database object which is accessible within the database or the subprogram.

Syntax:
 TYPE <<type>> IS TABLE OF <<element>> [NOT NULL];
Here, type is the type specifier and element is the data type.


DECLARE
TYPE V_Array_type IS TABLE OF VARCHAR2(20);
v_year V_Array_type :=V_Array_type();
BEGIN
v_year.extend(12);
v_year(1):='JANUARY';
v_year(2):='FEBRUARY'; 
v_year(3):='MARCH'; 
v_year(4):='APRIL'; 
v_year(5):='MAY'; 
v_year(6):='JUNE'; 
v_year(7):='JULY'; 
v_year(8):='AUGUST'; 
v_year(9):='SEPTEMBER'; 
v_year(10):='OCTOBER';
v_year(11):='NOVEMBER';
v_year(12):='DECEMBER';
END;

truncate table v_year_det;


select * from v_year_det;



DECLARE
TYPE V_Array_type IS TABLE OF VARCHAR2(20);
v_year V_Array_type :=V_Array_type();
BEGIN
v_year.extend(12);
v_year(1):='JANUARY';
v_year(2):='FEBRUARY'; 
v_year(3):='MARCH'; 
v_year(4):='APRIL'; 
v_year(5):='MAY'; 
v_year(6):='JUNE'; 
v_year(7):='JULY'; 
v_year(8):='AUGUST'; 
v_year(9):='SEPTEMBER'; 
v_year(10):='OCTOBER';
v_year(11):='NOVEMBER';
v_year(12):='DECEMBER';
FORALL I in v_year.first..v_year.last
insert into v_year_det values(v_year(i));
END;



select * from v_year_det;





ASSOCIATIVE ARRAY:

It is also called as the index by tables. It has subscript to access the elements. It is represented by the key value pair. Each unique keys are used to identify the value in the array. Data type of key can be integer or string. Key is added to the index by table by simply assigning value to first time. For modification of the same entry, same key is used. Key should be unique by combining strings together to show unique value or using primary key in the table.

It provides an easy maintenance of subscript which are created in PLSQL block. It is like the SQL table where the values are obtained with primary key. It is used for temporary data storage. It can be used instead of sql tables for avoiding the network traffic and disk storage which is required by sql tables. It does not store the persistent data. So that, it can be used with sql statements like select and insert. It is used for unending sessions of the database by declaring its datatype as package and defining them inside the body of the package.

Syntax:

TYPE type IS TABLE OF element [NOT NULL] INDEX BY [BINARY_INTEGER | PLS_INTEGER| VARCHAR2(size)];
INDEX BY key;

Here, the key is numeric. It can be either BINARY_INTEGER or PLS_INTEGER.Its data type can be var,varchar2,long or string. Varchar based key should be mentioned with the length.
element is the data type
size is the maximum no of elements, and it should be positive integer value.
type is the specifier.

DECLARE
TYPE marks IS TABLE OF NUMBER INDEX BY VARCHAR2(20);
marks_student marks;
student varchar2(20);
BEGIN
marks_student('Rahul'):=90;
marks_student('Shyam'):=99;
student :=marks_student.FIRST;
WHILE student IS NOT null LOOP
dbms_output.put_line('Student name is ' || student || ' and marks is ' || TO_CHAR(marks_student(student)));
student :=marks_student.NEXT(student);
END LOOP;
END;
/






DECLARE
TYPE V_Array_type IS TABLE OF VARCHAR2(20) INDEX BY VARCHAR2(10);
v_year V_Array_type;
BEGIN
v_year(1):='JANUARY';
v_year(2):='FEBRUARY'; 
v_year(3):='MARCH'; 
v_year(4):='APRIL'; 
v_year(5):='MAY'; 
v_year(6):='JUNE'; 
v_year(7):='JULY'; 
v_year(8):='AUGUST'; 
v_year(9):='SEPTEMBER'; 
v_year(10):='OCTOBER';
v_year(11):='NOVEMBER';
v_year(12):='DECEMBER';
END;









truncate table v_year_det;


select * from v_year_det;



DECLARE
TYPE V_Array_type IS TABLE OF VARCHAR2(20) INDEX BY VARCHAR2(10);
v_year V_Array_type;
BEGIN
v_year(1):='JANUARY';
v_year(2):='FEBRUARY'; 
v_year(3):='MARCH'; 
v_year(4):='APRIL'; 
v_year(5):='MAY'; 
v_year(6):='JUNE'; 
v_year(7):='JULY'; 
v_year(8):='AUGUST'; 
v_year(9):='SEPTEMBER'; 
v_year(10):='OCTOBER';
v_year(11):='NOVEMBER';
v_year(12):='DECEMBER';
FOR I in v_year.first..v_year.last
LOOP
insert into v_year_det values(v_year(i));
END LOOP;
END;
/




DIFFERENCE:

Array

Nested Table

Upper limit is fixed but it is not fixed for nested table.

Size can be increased dynamically.

Due to consecutive subscript, it is dense.

It is dense at the time of creation but become sparse once the elements are deleted in between.


VARRAY

NESTED TABLE

ASSOCIATIVE ARRAY

·        Number of elements is bounded or fixed. Variable size is fixed at the definition type.

        Number of elements is not bounded or fixed.

        Number of elements is not bounded or fixed. It is user defined.

·        Subscript type or index type is an integer.

        Subscript type or index type is an integer.

       Subscript type or index type is varchar or integer.

·        It is always continuous collection. It will not delete the elements in between.

          It starts with continuous and became sparse latter.

       It is neither continuous nor sparse. It is sorted as per the index information.

·        It is created in schema object and plsql block.

        It is created in schema object and plsql block.

         It is created in plsql block.

·        It is applicable for extend function.

         It is applicable for extend function.

          It is not applicable for extend function.

·        It is applicable for trim function.

        It is applicable for trim function.

         It is not applicable for trim function. This is because there is no separate step for an initialization process.

·        It is applicable for limit function.

          It is not applicable for limit function. It will throw the null value.

         It is not applicable for limit function. It will throw the error exception.

·        It is applicable for count,first,last,delete,prior,next and exits.

         It is applicable for count, first, last, delete,prior,next and exits.

         It is applicable for count, first,last,delete, prior, next and exits.


Comments

Popular posts from this blog

Difference between union and union all in oracle sql plsql

"This is one of the most important questions that interviewer might be asking in an interview.Infyosis,Linked group,Kyndryl,Vodafone Idea,IBM and Mphasis etc" INTRODUCTION: It is said to be set operators which is used to combine, add or concatenate two or more result sets. It is used in the multiple select statements where to combine to produce the final desired results.In other words,multiple select statements of similar column name in same order and having same data type to produce the unified result.In MIS datalake,when you are adding the modules of GST reconciliation which is to be used in micros in Excel or VB script, union and union all set operator is preferred for eliminating duplicate records or keeping duplicate records as per business requirement. Column must be of the same datatype for these operations of one select statement with another select statements. Columns must be in the same order in this set operators in the multiple select statements. Same number of co...

Difference between decode and case oracle sql plsql interview question and answer

" This is the most accepted question for PLSQL developer role.It has been asked in many interviews like Mphasis and Kyndryl. "  INTRODUCTION: Case was came after oracle database version 8.1.6 whereas decode was came before oracle database version 8.1.6 .Decode and case statements in an oracle work or acts like if then elsif conditional statements. if sal='2000000' then 'General Manager' elsif sal='3000000' then 'Senior Manager' elsif sal='1200000' then 'Manager' else 'Employee' end if; create table student_details(Student_RollNo Number,Student_Name Varchar2(20),student_marks Number,student_result varchar2(1)); insert into student_details values (100,'Rahul',90,'P'); insert into student_details values (101,'Rakesh',30,'F'); insert into student_details values (102,'Ram',60,'P'); insert into student_details values (103,'Rohan',10,'F'); insert into student_detai...

Explain oracle pl sql datatypes interview question and answer for fresher and experienced.

 Oracle PLSQL Data Types : PLSQL constant,value,variable,parameter,function which return the value are having the data type. It will determine the storage space information occupied and occurred in the memory or storage format and range constraints for valid values and operations as per specified size limit. It will determine how the data will be going to stored, handled and treated by the oracle engine during the period of processing and storage. It is used in the PLSQL blocks. It allows you to define your sub-types. PLSQL codes are embedded into the java program. Subtypes are used to make the data types compatible with data types used in the PLSQL program while embedding the PLSQL code into the java program. Subtype will be compatible with ANSI/ISO & IBM. It provides the pre-defined data types. Pre-defined data types are categorized into the four types: Composite LOB Reference Scalar Scalar Datatypes: It stores the values without the internal components. It holds the single ...