What's the difference between "index by table" and "index by table of records"?
I've come across both ter开发者_开发问答ms, but they sound synonymous. Is there a distinction between the two?
An "index-by table" is Oracle's term for "associative array". These are arrays that contain elements that you can address (or index by) either an integer or string. They're probably called that because of the use of the INDEX BY keywords when defining the array.
An abbreviation of the example given in the Oracle documentation:
DECLARE
TYPE population_type IS TABLE OF NUMBER INDEX BY VARCHAR2(64);
country_population population_type;
howmany NUMBER;
BEGIN
country_population('Greenland') := 100000; -- Creates new entry
howmany := country_population('Greenland');
...
You can create index-by tables containing records, where records are essentially a structure containing multiple types. A record, for example, often contains the same types as a row in a table.
Again, from the Oracle documentation:
DECLARE
TYPE EmpTabTyp IS TABLE OF employees%ROWTYPE
INDEX BY PLS_INTEGER;
emp_tab EmpTabTyp;
BEGIN
/* Retrieve employee record. */
SELECT * INTO emp_tab(100) FROM employees
WHERE employee_id = 100;
END;
Here, emp_tab is an index-by table, indexed by integers, containing records of employees%ROWTYPE.
I'm not sure where you got the phrases from, but TABLE OF
and INDEX BY
are separate parts of a collection type declaration. TABLE OF
defines the type of a collection's field(s), which can be a datatype (i.e. TABLE OF NUMBER
) or a record type (i.e. TABLE OF MY_TABLE%TYPE
).
INDEX BY
refers to the method of looking up this collection, almost like a key-value pair. For example, I might use INDEX BY VARCHAR2(10)
so that I can use a textual key to retrieve a value from the collection type.
Here's an illustration:
DECLARE
TYPE my_type IS TABLE OF NUMBER INDEX BY VARCHAR2(10);
n_my_value NUMBER;
BEGIN
my_type ('the key') := 99;
n_my_value := my_type ('the key');
END;
/
精彩评论