ORACLE PL/SQL from entry to proficient in notes-collection types of records and collections (6)

How to download this book and related resources: pay attention to the WeChat public account, click the function introduction-book link to download, you can get it

illustrate

This article mainly introduces the following knowledge points:

  • Collection Types
    6.1. Introduction to Collections
    6.2. Defining Index Tables
    6.3. Manipulating Index Tables
    6.4. Defining Nested Tables
    6.5. Manipulating Nested Tables
    6.6. Nesting Tables in Databases
    6.7. Defining Variable Length Arrays
    6.8. Manipulating Variable Length Arrays
    6.9. Variable-Length Arrays in Databases
    6.10. Choosing a Collection Type

6.1. Introduction to Collections

Introduction to Collections

6.2 Defining the index table

define index table

6.3 Manipulating Index Tables

DECLARE
  --定义索引表(使用数字)
  TYPE idx_deptno_table IS TABLE OF NUMBER(2) INDEX BY VARCHAR2(20);
  v_deptno idx_deptno_table;
  --定义索引表(使用字符串)
  TYPE idx_deptno_table2 IS TABLE OF VARCHAR2(20) INDEX BY PLS_INTEGER;
  v_deptno2 idx_deptno_table2;
  --定义索引表(使用%TYPE)
  TYPE idx_deptno_table3 IS TABLE OF hyz_dept.deptno%TYPE INDEX BY hyz_dept.dname%TYPE;
  v_deptno3 idx_deptno_table3;
BEGIN
  --为索引表赋值
  v_deptno('财务部') := 10;
  v_deptno('研究部') := 20;
  v_deptno('销售部') := 30;
  v_deptno2(10) := '财务部';
  v_deptno2(20) := '研究部';
  v_deptno2(30) := '销售部';
  v_deptno3('财务部') := 10;
  v_deptno3('研究部') := 20;
  v_deptno3('销售部') := 30;
  dbms_output.put_line('销售部编号' || v_deptno('销售部'));
  dbms_output.put_line('研究部编号' || v_deptno('研究部'));
  dbms_output.put_line('财务部编号' || v_deptno('财务部'));
  dbms_output.put_line('30编号是' || v_deptno2(30));
  dbms_output.put_line('20编号是' || v_deptno2(20));
  dbms_output.put_line('10编号是' || v_deptno2(10));
  dbms_output.put_line('销售部编号' || v_deptno3('销售部'));
  dbms_output.put_line('研究部编号' || v_deptno3('研究部'));
  dbms_output.put_line('财务部编号' || v_deptno3('财务部'));
END;

6.4 Defining Nested Tables

define nested table

6.5 Manipulating Nested Tables

code example

--初始化嵌套表例子
DECLARE
  TYPE emp_name_table IS TABLE OF VARCHAR(20); --定义嵌套表(员工名称)
  emp_name_info emp_name_table := emp_name_table('小王', '小明'); --初始化嵌套表
BEGIN
  dbms_output.put_line('员工1:' || emp_name_info(1));
  dbms_output.put_line('员工1:' || emp_name_info(2));
END;
--未初始化的嵌套表例子(一)
DECLARE
  TYPE deptno_table IS TABLE OF NUMBER(2); --定义嵌套表(部门名称)
  deptno_info deptno_table; --未初始化的嵌套表
BEGIN
  IF deptno_info IS NULL --初始值为NULL
   THEN
    deptno_info := deptno_table();
  END IF;
  deptno_info.extend(5); --扩充五个
  FOR i IN 1 .. 5 LOOP
    deptno_info(i) := i * 10;
    dbms_output.put_line('部门号:' || deptno_info(i));
  END LOOP;
  dbms_output.put_line('部门总数:' || deptno_info.count);
END;
--未初始化的嵌套表例子(二)
--例一中的extend语法和如下语句语法完全一致
DECLARE
  TYPE deptno_table IS TABLE OF NUMBER(2); --定义嵌套表(部门名称)
  deptno_info deptno_table; --未初始化的嵌套表
BEGIN
  IF deptno_info IS NULL --初始值为NULL
   THEN
    deptno_info := deptno_table(10, 20, 30, 40, 50); --扩充五个
  END IF;
  FOR i IN 1 .. 5 LOOP
    deptno_info(i) := i * 10;
    dbms_output.put_line('部门号:' || deptno_info(i));
  END LOOP;
  dbms_output.put_line('部门总数:' || deptno_info.count);
END;

6.6 Nested Tables in Databases

grammar:

CREATE OR REPLACE TYPE type_name
AS TABLE OF element_type [ NOT NULL ] ;
Nested tables in a database

6.7. Defining variable-length arrays

Define a variable-length array

6.8 Manipulating variable-length arrays

DECLARE
  TYPE projectlist IS VARRAY(50) OF VARCHAR2(16); --定义变长数组(项目列表)
  project_list projectlist := projectlist('OK', 'HELLO', 'ERP'); --定义一个初始化的变长数组
  TYPE empno_type IS VARRAY(50) OF NUMBER(4); --定义变长数组(项目列表)
  empno_list empno_type; --定义一个未初始化的变长数组
BEGIN
  dbms_output.put_line(project_list(3));
  project_list.extend; --扩展第四个元素
  project_list(4) := 'LIST'; --赋值到第四个元素
  empno_list := empno_type(300, 301, 302, 303, NULL);
  empno_list(5) := 304;--为第五个元素赋值,原值为NULL
  dbms_output.put_line(empno_list(5));
END;

6.9 Variable-Length Arrays in Databases

variable length array in database

6.10 Choosing a Collection Type

Similarities Between Nested Tables and Indexed Tables
  • Nested tables are extended from indexed tables, so nested tables contain all table properties of indexed tables.
  • Both nested tables and indexed tables use subscripts to access elements in the collection.
  • Nested tables have the same data type as indexed tables.
Differences between nested tables and indexed tables
  • Nested tables can be stored in the database, but indexed tables cannot, so if the table type needs to be stored in the database
    . Nested tables should be considered.
  • The legal subscript range of nested table is 1~214748361, the subscript cannot be negative; and the index table can be negative subscript, the range is -2l47483647~2147483647. Therefore, if you consider the subscript with negative number, you should use the index table.
  • 索引表在每次调用语句块或在包初始化时在内存中自动构建, 能够保存容量不固
    定的信息'因为它的长度是大小可变的, 其值不能为 NULL; 而嵌套表是一种对
    象类型, 如果不显式使用构造函数, 则其值为 NULL, 可以使用 IS NULL 进行
    检查。
  • 嵌套表可以使用其他的方法,比如 EXTEND和TRIM 等方法进行操作,而索引表
    不需要。
  • PL/SQL会自动在主机数组和索引表之间进行转换, 而嵌套表不能在主机数组之间
    进行转换。
变长数组与嵌套表共同点
  • 变长数组与嵌套表都使用下标符号对单个元素进行访问,在使用前都必须使用构造函数进行初始化。
  • 都可以存储在数据库表中, 都可以应用集合方法。
变长数组与嵌套表区别(决定了是否选择此种类型)
  • 变长数组一旦声明, 元素数目就被固定,而嵌套表没有一个明确的大小上限。
  • 当存储到数据库中时, 变长数组保持了元素的排序和下标的值,而嵌套表则不同。
思维导图
选择集合类型

本文全部知识点思维导图

集合之集合类型篇

本人是一枚程序猿,如果觉得整理的不错,请关注个人微信公众号(扫一扫):

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326010153&siteId=291194637