 |
|
Multi-Dimensional Pointers
Oracle Database Tips by Donald Burleson
|
To understand multi-dimensional
pointers, let's begin with a simple example of a natural hierarchy
(Figure 6.7). In this example, we see that each university has many
departments, each department offers many courses, and that each course
offers many sections. This is a natural descending hierarchy of
one-to-many relationships.
Figure 6.7 A Natural Hierarchy of data
relationships
Let's take a look at how we could create a data
structure that would embed pointers to establish these relationships.
Department table
department_name
(1-20) *course
Course table
course
number
course_name
(0-10) *section
(1) *department
Section table
section_number
semester
instructor_name
building
room
days
time
(1) *course
From this definition, we see that the department
table consists of the department name, followed by from one to 20 pointers to
courses. The course table contains the course number and course name followed
by from zero to ten pointers to sections and one "owner" pointer, pointing to
the department's row. The section table consists mostly of data items, except
for the pointer to the course row.
As we know there are many options for modeling
this type of descending one-to-many data relationship. In a ?vanilla?
relational database, each entity would exist as a table, with the primary key of
the owner table copied into the member table. But there is an alternative to
modeling this structure in the object/relational model. Let's take a look at
how a hierarchical data structure might be implemented in Oracle:
CREATE TYPE full_name (
first_name varchar(20),
MI char(1),
last_name varchar(20));
CREATE TYPE section_type (
section_number number(5),
instructor_name full_name,
semester char(3),
building varchar(20),
room char(4),
days_met char(5),
time_met char(20));
CREATE TABLE section OF section_type;
CREATE TYPE section_array AS VARRAY(10) OF
section_type;
CREATE TYPE course_type (
course_ID number(5),
course_name varchar(20),
credit_hours number(2),
section_list section_array);
CREATE TABLE course OF course_type;
CREATE TYPE course_array as VARRAY(20) OF
course_type;
CREATE TYPE dept_type (
dept_name varchar(20),
chairperson_name full_name,
course_list course_array);
CREATE TABLE department OF dept_type;
This is what this data structure would look like
(Figure 6.8).
Figure 6.8 An implementation of
multidimensional row pointers
Here we see that the pointers allow fast access
from owner to member in the hierarchy. But where are the "owner" pointers? As
it turns out, we must first define the hierarchy before we have the necessary
definitions to include the pointers. Let's add them using the ALTER TYPE
statement:
ALTER TYPE section_type
ADD COLUMN course_owner_pointer course_type;
ALTER TYPE course_type
ADD COLUMN department_owner_pointer department_type;
We have now created a two-way pointer structure,
such that all owner rows in the hierarchy point to their member rows, while all
member rows will point up to their owners. However, we must bear in mind that
these are only data structures; it is up to the programmer, to assign these
pointers when the rows are created.
In a sense, this data structure is the
object/relational equivalent to the **char data structure. Essentially, a
**char data structure is a structure where we have a pointer to an array of
pointers to characters. In Oracle, the department has an array of pointers to
curses, which, in turn, contain arrays of pointers to sections.
But how do we query these pointers with SQL? In
order to accommodate the new object features, most object/relational vendors are
implementing the CAST and the MULTISET extensions to SQL. For example, here is
what the query to populate the student_list internal table:
INSERT INTO
COURSE (STUDENT_LIST)
(CAST
(MULTISET
(SELECT
student_name,
student_address,
grade
FROM
GRADE, STUDENT
WHERE
GRADE.course_name = ?CS101?
AND
GRADE.student_name =
STUDENT.student_name
)
)
);
As we can see, the new SQL extensions are rather
foreign to those who are accustomed to pure relational syntax.