Difficulty: Easy
Correct Answer: A PL/SQL collection is a single variable that can hold multiple elements of the same type, and the main collection types are associative arrays, nested tables, and varrays.
Explanation:
Introduction / Context:
PL/SQL collections provide an in memory way to work with sets of values inside procedural code. They are similar to arrays or lists in other programming languages and are frequently used for bulk operations, lookups, and temporary storage. This question asks what a PL/SQL collection is and which collection types Oracle supports.
Given Data / Assumptions:
Concept / Approach:
A PL/SQL collection is a composite data structure that groups multiple elements of the same data type under one variable name. Each element is accessed by a subscript or index. Oracle supports three main kinds of collections: associative arrays (also called index by tables), nested tables, and varrays. Associative arrays are sparse, unbounded collections indexed by integer or string keys. Nested tables are unbounded sets that can be stored in database columns. Varrays are arrays with a fixed upper bound that preserve element order.
Step-by-Step Solution:
Step 1: Define a collection type using CREATE TYPE at the schema level or TYPE inside a PL/SQL block, specifying the element type.Step 2: Choose associative arrays when you need fast lookups by key and do not require storage in a table column.Step 3: Choose nested tables when you need a growable collection that can also be stored and queried in relational tables as a column type.Step 4: Choose varrays when you know a fixed maximum number of elements and want to preserve ordering and use them in both PL/SQL and SQL.Step 5: Recognise that all of these are considered PL/SQL collections and hold multiple elements of the same type, matching option A.
Verification / Alternative check:
When you declare and use a collection in PL/SQL, you can extend it, assign values, and loop over it using standard syntax. For example, an associative array can be declared as TYPE num_table IS TABLE OF NUMBER INDEX BY PLS_INTEGER; and then used as a simple array structure. Observing how PL/SQL code accesses elements with indices confirms that a collection is a single variable holding many values.
Why Other Options Are Wrong:
Option B confuses collections with sets of tables. Option C treats collections as indexes, which they are not. Option D claims that collections are only cursors, ignoring their in memory storage role and explicit data structures.
Common Pitfalls:
Developers sometimes choose the wrong collection type and then struggle with limitations, such as trying to store associative arrays in table columns. Another pitfall is forgetting to initialize nested tables and varrays before using them. Understanding the differences among collection types helps you design efficient and clear PL/SQL programs.
Final Answer:
A PL/SQL collection is a single variable that can hold multiple elements of the same type, and the main collection types are associative arrays, nested tables, and varrays.
Discussion & Comments