当前位置:网站首页>Set of pl/sql
Set of pl/sql
2022-07-26 08:48:00 【Yun Wenwu】
aggregate
For the time being, users can simply understand the table as the set we call here , Compare the previous records with the collection , Records can be understood as a line of data ( One row and many columns ), A set can be understood as multiple values of a column ( A column with many lines ).
Take a beaver , If there is a teacher form, the information is as follows :
In the table t001------ Liu Yang can be called a record , Here's the picture :
Liu Yang ---- Chen Yan ---- Hu Mingxing can be called a collection , Here's the picture :
Properties and methods of collection
The attributes and methods of the set can be referred to by users java Simple understanding of the properties and methods of objects in , Simply distinguish between attributes and methods to see whether they pass parameters , If parameter transfer is generated, it is the method , conversely , Property
attribute :
- first—— Take the subscript of the first element of the set
- last—— Take the subscript of the last element of the set
- count—— The number of elements in the set
- limit—— The maximum capacity of the collection ( Index table 、 The maximum value of the nested table is unknown. What is returned is null, The variable array returns the length specified in the variable length array declaration )
Method :
- delete—— Delete elements in the collection
(1)delete—— Empty all elements of the collection
(2)delete(n)—— Delete the set with the subscript n The elements of , Cannot be used in variable arrays
(3)delete(m,n)—— Delete the subscript in the set n-m Between all the elements (m<=n), Cannot be used in variable arrays - extend—— Extend collection elements
(1)extend—— Add an empty element at the end of the collection
(2)extend(n)—— Add n An empty element
(3)extend(n,m)—— The first m Copies of elements n Copies are added to the end of the set - tirm/trim(i)—— Delete one or... From the end of the set n Elements ( Do not keep placeholders after deletion ), Cannot be used for index tables
- next(i)—— Take the subscript in the set as i The subscript of the next element of the element
- prior(i)—— Take the subscript in the set as i The subscript of the previous element of the element
call :
Collection variable name .[ Property name | Method name ];
Index table ( Associative array )——“ Sparse array ”
The index table can only be in pl/sql Cannot be stored in the database
One 、 Grammar format
declare
-- Declare the index table type
type Index table name is table of Data types of elements in the collection index by Subscript data type ;
-- Declare index table type variables
Index table variable name Index table name ;
begin
null;
/* The relevant operation */
end;
- You can use the index table variable name ( Subscript ):=… Assign values to the elements in the set in the format of , You can also use the index table variable name ( Subscript ) To access the elements
- When declaring the index table type , There are no restrictions on the data types of elements in the collection , But the accuracy must be further determined , For example, you cannot write varchar2 But you can write varchar2(211) ;by Users of post subscript data type can only use pls_integer,binary_integer,varchar2(…).
- Subscripts can be discontinuous or negative and must be integers , Subscripts are automatically sorted from small to large , For example, users assign values to subscripts 2,1,-2 The element assignment of , When printing with circular output, it will still be in -2,1,2 Sequential printing ,first And last Empathy
Two 、 Use subscripts to assign values and access examples of elements in the index table
declare
type a is table of varchar2(21) index by pls_integer;
b a;
begin
b(-1):=' Zhao ';
b(2):=' money ';
b(3):=' Grandchildren ';
-- Delete the index table with the subscript 3 The elements of
b.delete(3);
dbms_output.put_line(b(-1)||','||b(2)||','||b(3));
end;
The output effect is shown in the figure below :
3、 ... and 、 Examples of using set attributes and methods in index tables
-- attribute
declare
type a is table of varchar2(21) index by pls_integer;
b a;
begin
b(-1):=' Zhao ';
b(2):=' money ';
b(3):=' Grandchildren ';
dbms_output.put_line('b The subscript of the first element in the index table is '||b.first);
dbms_output.put_line('b The subscript of the last element in the index table is '||b.last);
dbms_output.put_line('b The index table can hold at most '||b.limit);
dbms_output.put_line('b The number of elements in the index table is '||b.count);
end;
/
-- Method
declare
type a is table of varchar2(21) index by pls_integer;
b a;
begin
b(-1):=' Zhao ';
b(2):=' money ';
b(3):=' Grandchildren ';
-- Print the subscript as 2 The subscript of the previous element on the element of
dbms_output.put_line(' Subscript to be 2 The subscript of the previous element on the element of :'||b.prior(2));
-- Print the subscript as 2 The subscript of the next element
dbms_output.put_line(' Subscript to be 2 The subscript of the next element :'||b.next(2));
-- Delete the subscript as -1 The elements of
b.delete(-1);
-- Print detection subscript -1 Whether there is a result for the element of
/*exists(i) The detection subscript is i Does the element of exist , There is returned true, Otherwise return to false */
if b.exists(-1) then
dbms_output.put_line(' There is ');
else
dbms_output.put_line(' non-existent ');
end if;
end;
The operation effect is as follows :
Four 、 Loop and index representation
- while
declare
type a is table of varchar2(21) index by pls_integer;
b a;
i NUMBER;
begin
b(-1):=' Zhao ';
b(2):=' money ';
b(3):=' Grandchildren ';
i:=b.first;
WHILE b.exists(i) LOOP
dbms_output.put_line(b(i));
i:=b.next(i);
END LOOP;
end;

- for
for The loop only applies when the numbers are continuous, for example 1,2,3 Etc. do not apply to index tables
- loop
declare
type a is table of varchar2(21) index by pls_integer;
b a;
i NUMBER;
begin
b(-1):=' Zhao ';
b(2):=' money ';
b(3):=' Grandchildren ';
i:=b.first;
LOOP
dbms_output.put_line(b(i));
EXIT WHEN i=b.last;
i:=b.next(i);
END LOOP;
end;

5、 ... and 、 Index tables and records ( Multiple rows and columns )
DECLARE
-- Declarative record
TYPE t_record is RECORD(
ENAME VARCHAR2(100),
AGE NUMBER
);
-- Declare the index table data type
TYPE xx is TABLE OF t_record index BY PLS_INTEGER;
-- Declare a variable that holds the data type of the index table
b xx;
BEGIN
b(1).ENAME:=' Zhang San ';
b(1).AGE:=18;
b(2).ENAME:=' Li Si ';
b(2).AGE:=20;
dbms_output.put_line(b(1).ENAME||', '||b(1).AGE||', '||b(2).ENAME||', '||b(2).AGE);
END;

Nested table ——“ Table in table ”
Nested tables can be stored in the database , Also available at pl/sql Use in
One 、 Grammar format
declare
-- Declare nested table data types
type Nested table name is table of Data types of elements in nested tables ;
-- Declare the host variable
Nested table variable name Nested table name ;
begin
-- initialization
Nested table variable name := Nested table name ();
/* User defined actions */
...;
end;
- Use integers ( Can only be positive ) As a subscript , Subscripts are continuous , Unlimited number of elements
- Initialization is required before the next related operation
Two 、 Application of attributes and methods of sets in nested tables
declare
type a is table of varchar2(21);
b a;
begin
b:=a(' Zhao ',' money ',' Grandchildren ');
dbms_output.put_line('b The number of elements in :'||b.count);
b.extend(3);
dbms_output.put_line(' After expansion b The number of elements in :'||b.count);
end;

3、 ... and 、 Loop and nested table
- while
declare
type a is table of varchar2(21);
b a;
i NUMBER;
begin
b:=a(' Zhao ',' money ',' Grandchildren ');
i:=b.first;
WHILE b.exists(i) LOOP
dbms_output.put_line(b(i));
i:=b.next(i);
END LOOP;
end;
- loop
declare
type a is table of varchar2(21);
b a;
i NUMBER;
begin
b:=a(' Zhao ',' money ',' Grandchildren ');
i:=b.first;
LOOP
dbms_output.put_line(b(i));
EXIT WHEN i=b.last;
i:=b.next(i);
END LOOP;
end;
- for
declare
type a is table of varchar2(21);
b a;
begin
b:=a(' Zhao ',' money ',' Grandchildren ');
FOR i in b.first..b.last LOOP
dbms_output.put_line(b(i));
END LOOP;
end;
The running effects of the three codes are as follows :
Four 、 Use in the database
(1) Create nested table types
create type Type name is table of Data types of elements in the collection ;
Examples are as follows :
-- Create a file called xxx Where the element type is varchar2(20) Nested table type
create type xxx is table of varchar2(20);
(2) Use
/* The syntax is as follows create table Main table name ( Name 1 data type 1, ..., Name n Nested table type name )nested table Nested table type column name store as Table name ( Tables that do not exist in the database ); */
- If there are multiple nested table type columns in the created table () After nested The number of statements of should be the same as the number of nested table type columns , And store as The following table name cannot be repeated , Examples are as follows :
create table demo(
DEPTNO NUMBER,
x xxx,
m xxx
)nested table x store as b,nested table m store as c;
- select Statements cannot directly query nested table data , To query, use the following statement :
select * from table(select Nested column names from Main table name );
- insert Sentences also make subtle differences , Examples are as follows :
--insert into Main table name ( Nested table type column name ) values( Nested table type name ( value 1, value 2, value 3,...));
insert into demo(x) values(xxx(' Zhao ',' money ',' Grandchildren '));
- Delete nested table type statements as follows :
drop type Nested table type name ;
- When deleting the main table , Nested tables also disappear (“ Under the nest , There is no end to eggs ”), But nested table types will not be deleted
drop table Main table name ;
(2-1) stay pl/sql Use in ( You can use it directly xxx type )
-- A simple example is as follows
declare
a xxx;
begin
a:=xxx(' Zhao ',' money ',' Grandchildren ');
dbms_output.put_line(a(1)||', '||a(2)||', '||a(3));
end;

Variable array ——“ Dense array ”
Variable arrays can be stored in the database , Also available at pl/sql Use in
One 、 Grammar format
declare
-- Declare variable array types
type Variable array type name is varray( The upper limit of the number of elements ) of Data types of elements in the array ;
-- Declare variables that carry variable array types
Variable name Variable array type name ;
begin
-- initialization
Variable name := Variable array type name ( value 1, value 2, value n);
/* The relevant operation */
...;
end;
- Use integers ( Can only be positive )(pls_integer,binary_integer) As a subscript , Subscripts are continuous ; The number of elements is limited
- The variable array has an upper limit ,limit Property returns the number of elements given at the time of declaration
- Initialization is required before the next related operation
Two 、 Application of attributes and methods of sets in variable arrays
declare
type a is varray(7) of varchar2(100);
b a;
begin
b:=a(' Zhao ',' money ',' Grandchildren ',' Li ');
dbms_output.put_line(' Number of elements before expansion :'||b.count);
dbms_output.put_line('b The maximum number of elements in the set :'||b.limit);
b.extend(3);
dbms_output.put_line(' Number of elements after expansion :'||b.count);
dbms_output.put_line('b The maximum number of elements in the set :'||b.limit);
b.trim;
dbms_output.put_line(' Number of elements after deleting the last element :'||b.count);
end;

3、 ... and 、 Loop and variable array
- while
declare
type a is varray(4) of varchar2(100);
b a;
i NUMBER;
begin
b:=a(' Zhao ',' money ',' Grandchildren ',' Li ');
i:=b.first;
WHILE b.exists(i) LOOP
dbms_output.put_line(b(i));
i:=b.next(i);
END LOOP;
end;
- loop
declare
type a is varray(4) of varchar2(100);
b a;
i NUMBER;
begin
b:=a(' Zhao ',' money ',' Grandchildren ',' Li ');
i:=b.first;
LOOP
dbms_output.put_line(b(i));
EXIT WHEN i=b.last;
i:=b.next(i);
END LOOP;
end;
- for
declare
type a is varray(4) of varchar2(100);
b a;
begin
b:=a(' Zhao ',' money ',' Grandchildren ',' Li ');
FOR i IN b.first..b.last LOOP
dbms_output.put_line(b(i));
END LOOP;
end;
The operation effect is as follows :
Four 、 Use in the database
The use of variable array types is like using a data type , However, when inserting this column of data, you should also pay special attention to its collection identity
(1) Create variable array types
create type Variable array type name is varray( Number of capacitable elements ) of Data types of elements in the array ;
Examples are as follows :
create TYPE n is VARRAY(4) OF VARCHAR2(100);
(2) Use
/* The syntax is as follows : create table Table name ( Name 1 data type 1, ..., Name n Variable array type name ) */
Examples are as follows :
-- Build table
create TABLE demo(
no NUMBER,
x n
);
-- insert data
INSERT INTO demo VALUES(10,n(' Zhao ',' money ',' Grandchildren ',' Li '));
-- Query data
SELECT * FROM table(SELECT x FROM demo);

- Delete is the same as nested table type , Therefore, I will not repeat here
(2-1) stay pl/sql Use in ( You can use it directly n type )
DECLARE
b n;
BEGIN
b:=n(' Zhao ',' money ',' Grandchildren ',' Li ');
FOR i in b.first..b.last LOOP
dbms_output.put_line(b(i));
END LOOP;
END;

边栏推荐
- 六、品达通用权限系统__pd-tools-log
- OA项目之我的会议(查询)
- keepalived双机热备
- KV database based on raft consensus protocol
- Mysql database connection / query index and other common syntax
- 2022年收益率最高的理财产品是哪个?
- What are the contents of Oracle OCP and MySQL OCP certification exams?
- Web概述和B/S架构
- 6、 Pinda general permission system__ pd-tools-log
- Alphabetic string
猜你喜欢

基于C语言设计的换乘指南打印系统

PXE principles and concepts

pl/sql之动态sql与异常

Human computer interaction software based on C language

【数据库 】GBase 8a MPP Cluster V95 安装和卸载

Cadence(十)走线技巧与注意事项

6、 Pinda general permission system__ pd-tools-log

【FreeSwitch开发实践】使用SIP客户端Yate连接FreeSwitch进行VoIP通话

Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field

Study notes of automatic control principle -- dynamic model of feedback control system
随机推荐
C # use npoi to operate Excel
P1825 [USACO11OPEN]Corn Maze S
Grid segmentation
Oracle 19C OCP 1z0-083 question bank (7-12)
基于C语言实现的人机交互软件
Xshell batch send command to multiple sessions
Spark scheduling analysis
[untitled]
Huffman transformation software based on C language
ES6模块化导入导出)(实现页面嵌套)
tcp 解决short write问题
P3743 Kotori's equipment
合工大苍穹战队视觉组培训Day5——机器学习,图像识别项目
[database] gbase 8A MPP cluster v95 installation and uninstall
Oracle 19C OCP 1z0-082 certification examination question bank (36-41)
Cve-2021-26295 Apache OFBiz deserialization Remote Code Execution Vulnerability recurrence
Xtrabackup appears' flush no '_ WRITE_ TO_ BINLOG TABLES‘: 1205 (HY000) Lock wait timeout exceeded;
pl/sql之集合
内存管理-动态分区分配方式模拟
C Entry series (31) -- operator overloading