Lesson 6 | Delete an entire Array or nested Table |
Objective | Write a delete command to remove all rows from a nested table or an Array |
DECLARE TYPE emp_nested_table IS TABLE OF VARCHAR2(100); emp_names emp_nested_table := emp_nested_table('Alice', 'Bob', 'Charlie'); BEGIN emp_names.DELETE; -- Removes all elements from the nested table DBMS_OUTPUT.PUT_LINE('All elements deleted from nested table.'); END; /
DECLARE TYPE emp_varray IS VARRAY(5) OF VARCHAR2(100); emp_names emp_varray := emp_varray('Alice', 'Bob', 'Charlie'); BEGIN emp_names := emp_varray(); -- Reinitializes the VARRAY, effectively deleting all elements DBMS_OUTPUT.PUT_LINE('All elements deleted from VARRAY.'); END; /
UPDATE employees SET projects = CAST(NULL AS project_nested_table) WHERE employee_id = 1001;Where:
projects
is a nested table column in the employees
table.project_nested_table
is the nested table type.projects
is a nested table column in the employees
table.project_nested_table
is the nested table type.DELETE
with THE
THE
keyword was used in older Oracle versions with nested tables, but it is deprecated and no longer necessary in modern versions, including Oracle 19c.Example of deprecated syntax (not recommended in Oracle 19c):
DELETE FROM THE (SELECT nested_table_column FROM parent_table WHERE condition);
Recommended modern equivalent (Oracle 19c):
DELETE FROM TABLE (SELECT nested_table_column FROM parent_table WHERE condition);
DELETE
with TABLE
TABLE
keyword is still valid in Oracle 19c only when dealing with collections, such as nested tables or VARRAYs.Example:
DELETE FROM TABLE (SELECT nested_table_column FROM parent_table WHERE condition);
DELETE FROM THE (...)
is deprecated and no longer needed in Oracle 19c.DELETE FROM TABLE (...)
is still viable but is only used for deleting elements from a nested table collection.DELETE
statement with the THE
or the TABLE
keyword, as following example shows:
DELETE TABLE(SELECT d.detail_nest FROM SALE_HEADER d WHERE d.sale_id = 35);The DML statement above deletes all the elements from a nested table.
NULL
, as the following example shows:
UPDATE SALE_HEADER SET detail_nest = NULL WHERE sale_id = 35);
NULL
, which in turn deletes all the elements within the table.Now that you have learned few techniques for deleting rows from nested tables and varrays, try them yourself with the following tutor-based exercise. In this exercise, you will be asked to build a DELETE
statement to delete the records from the SALE_HEADER
table.