Chapter 11. Records and Collections

Records and collections are examples of composite data structures, which means that they are composed of more than one element or component, each with its own value. Records in PL/SQL programs are very similar in concept and structure to the rows of a database table. The record as a whole does not have a value of its own; instead, each individual component or field has a value, and the record gives you a way to store and access these values as a group. Records can greatly simplify your life as a programmer, allowing you to write and manage your code more efficiently by shifting from field-level declarations and manipulation to record-level operations.

A collection is a data structure that acts like a list or single-dimensional array. Collections are, in fact, the closest you can get to traditional arrays in the PL/SQL language. You can use collections to manage lists of information in your programs. This chapter will help you decide which of the three different types of collections (associative array, nested table, or VARRAY) best fit your program requirements, and will show you how to define and manipulate those structures.

First, let's get to know records. They are simpler, and are often used within collections.

11.1 Records in PL/SQL

A record is similar in structure to a row in a database table. Each row in a table has one or more columns of various datatypes. A record is composed of one or more fields. There are three different ways to define a record, but once defined, the same rules apply for referencing and changing fields in a record. Let's take a look at some of the benefits of using records. Then we'll examine the different ways to define a record, and finish up with examples of using records in your programs.