Unlike nested tables, varrays are ordered and bound collections. That is, the number of elements within a varray is limited to the number specified when you create this type. In this lesson, we will look at the two different ways of inserting data into a varray:
- A single DML statement to insert the data into the main table and into the varray
- A single DML statement to insert the data into the main table and empty data into the varray
Using the first technique, inserting the data into the main table and the varray, you must specify the data for the main table and the name of the varray and, within parentheses, specify the data for the varray. For example, let us insert data into the
CUSTOMER_OBJ_TABLE
table, which has
PHONE_LIST
as a varray:
INSERT INTO CUSTOMER_OBJ_TABLE VALUES
(CUSTOMER_TYPE (1 ,'Amy' ,'Black' ,
ADDRESS_TYPE('100 West Main' ,NULL, 'Omaha' ,'NE','98765'),
PHONE_ARRAY('1-808-876-6543','1-088-209-0987'),
'22-DEC-99','JANET'));
In the example above, we inserted two elements into the varray and also inserted data into the other columns of the object table.
Using the second technique, it is also possible to have a blank record for a varray while inserting data into the main table. For example, we will use the
INSERT
statement mentioned above to insert a blank record into a varray:
INSERT INTO CUSTOMER_OBJ_TABLE VALUES
(CUSTOMER_TYPE (1 ,'Amy' ,'Black' ,
ADDRESS_TYPE('100 West Main' ,NULL, 'Omaha' ,'NE','98765'),
PHONE_ARRAY(), '22-DEC-99','JANET'));
In the example above, we inserted an empty record instead of a
NULL
value into a varray.
The data inserted into the varray can be inserted later via an
UPDATE
statement. We will learn more about updating varrays later in this module. Although it is possible to insert individual elements into nested tables, it is not possible to insert individual elements into a varray.
Inserting Row into varray
- With SQL*Plus, connect to the database by using
PETSTORE
as the username, GREATPETS
as the password, and MYDB
as the host string.
- You are now connected to the database. Next, build an
INSERT
statement to insert a record into the varray within the CUSTOMER_OBJ_TABLE
table. The values for the record are as follows: cust_id
is 56
, name of the person is Norma Grey residing at 100 South East Main Burbank CA 65443 having the following phone numbers: 1-800-876-3243 and 1-714-785-8763. Joan last updated the record on 12/22/99.
- SQL*Plus now displays the result of the DML statement.