Database Programming With PL/SQL 6-2: Practice Activities: Indexing Tables of Records
Database Programming With PL/SQL 6-2: Practice Activities: Indexing Tables of Records
Database Programming With PL/SQL 6-2: Practice Activities: Indexing Tables of Records
Try It / Solve It
1. PL/SQL collections:
A. In your own words, describe what a PL/SQL collection is.
C. What is the difference between an INDEX BY table and a database table such as EMPLOYEES or
COUNTRIES?
D. Describe the difference between an INDEX BY table and an INDEX BY table of records.
E. Look at the following code. Describe the difference between t_pops and v_pops_tab. Is v_pops_tab an
INDEX BY table or an INDEX BY table of records? How do you know?
DECLARE
TYPE t_pops IS TABLE OF countries.population%TYPE
INDEX BY BINARY_INTEGER;
v_pops_tab t_pops;
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
2. INDEX BY tables of countries in South America:
A. Write and execute an anonymous block that declares and populates an INDEX BY table of countries in
South America (region_id = 5). The table should use country_id as a primary key, and should store the
country names as the element values. The data should be stored in the table in ascending sequence of
country_id. The block should not display any output. Save your code.
B. Modify the block so that after populating the INDEX BY table, it uses a FOR loop to display the contents
of the INDEX BY table. You will need to use the FIRST, LAST, and EXISTS table methods. Execute the
block and check the displayed results. Save your code.
C. Modify the block again so that instead of displaying all the contents of the table, it displays only the first
and last elements and the number of elements in the INDEX BY table. Execute the block and check the
displayed results.
Hint: declare a cursor to fetch the employee data, then declare the INDEX BY table as cursor-
name%ROWTYPE. Save your code.
B. Modify the block so that after populating the table of records, it uses a FOR loop to display to display
the contents. You will need to use the FIRST, LAST and EXISTS table methods. Execute the block and
check the displayed results. Save your code.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.