search
HomeDatabaseMysql Tutorialoracle:变长数组varray,嵌套表,集合

CREATE TYPE varray_type AS VARRAY( 2 ) OF VARCHAR2 ( 50 ); 这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改 变长数组类型和嵌套表类型 元素的大

<span>CREATE</span> TYPE varray_type <span>AS</span> VARRAY(<span>2</span>) <span>OF</span> <span>VARCHAR2</span>(<span>50</span>); 

 

这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 

 

更改元素类型的大小或精度

可以更改变长数组类型和嵌套表类型 元素的大小。

<span>ALTER</span><span> TYPE varray_type 
MODIFY ELEMENT TYPE </span><span>varchar2</span>(<span>100</span>) <span>CASCADE</span>;

 

CASCADE选项吧更改传播到数据库中的以来对象。也可以用 INVALIDATE 选项使依赖对象无效

增加变长数组的元素数目

<span>ALTER</span><span> TYPE vrray_name
MODIFY LIMIT </span><span>5</span> <span>CASCADE</span>;

使用变长数组

<span>CREATE</span> <span>TABLE</span><span> table_name(
column_name type,
var_col_name varray_type
);</span>

 

 获得变长数组的信息

<span>DESC</span><span>[</span><span>RIBE</span><span>]</span><span> varray_type;

</span><span>SELECT</span> <span>*</span> 
<span>FROM</span><span> user_varrays
</span><span>WHERE</span> type_name <span>=</span> varray_name;

 DESC 获得的是 varray_type AS VARRAY(2) OF VARCHAR2(50)

填充变长数组元素

oracle:变长数组varray,嵌套表,集合

<span>INSERT</span> <span>INTO</span> table_name <span>VALUES</span><span>(
  value,
  varray_type(
    </span><span>'</span><span>xxxx</span><span>'</span><span>,
    </span><span>'</span><span>xx</span><span>'</span><span>,
    </span><span>'</span><span>x</span><span>'</span><span>)
);</span>

oracle:变长数组varray,嵌套表,集合

 

 可以一次向变长数组添加多个数据。

查找变长数组元素

<span>SELECT</span> <span>*</span>
<span>FROM</span> table_Name;

 

如果变长数组中的元素有多个,会一起输出,输出的数据列是一个长列,跟包含对象的表一样。 

 更改变长数组元素

 要想更改变长数组的一个元素,需要把其他元素一起更改,整个变长数组作为一个整体来的。

<span>UPDATE</span><span> table_name
</span><span>SET</span> var_col_name <span>=</span> varray_type(<span>'</span><span>xxx</span><span>'</span>,<span>'</span><span>xxxxxx</span><span>'</span>)
WHERE expr1;

 

 

创建嵌套表类型

<span>CREATE</span> TYPE table_type <span>AS</span> <span>TABLE</span> <span>OF</span> type;

 

 其中type 可以为任何类型,包括varray 和 object ,通常object 居多。

 使用嵌套表类型

oracle:变长数组varray,嵌套表,集合

<span>CREATE</span> <span>TABLE</span><span> table_name(
  column_name type,
  tab_col_name table_type
)
NESTED </span><span>TABLE</span><span>
  table_col_name
STORE </span><span>AS</span><span>
  next_table_name [TABLESPACE user_name];</span>

oracle:变长数组varray,嵌套表,集合

 

 创建嵌套表的时候要为嵌套表类型另外创建一个表来保存数据, NESTED 以下的部分就是在干这事。那个表的名称为: next_table_name

TABLESPACE 可以将另外创建表放到另外的空间。

 获得表信息

<span>SET</span> DESCRIBE DEPTH <span>2</span>
<span>DESC</span><span>[</span><span>RIBE</span><span>]</span> table_name;

 

也可以直接通过数据字典来获得嵌套表的信息

<span>SELECT</span> <span>*</span>
<span>FROM</span><span> user_nested_tables
</span><span>WHERE</span> table_name <span>=</span> xxxx;

 

填充、查找嵌套表元素

跟变长数组方法一样

更改嵌套表元素

跟变长数组不同,嵌套表的元素可以单独更改:可以插入、更改和删除嵌套表元素。

插入

<span>INSERT</span> <span>INTO</span> <span>TABLE</span><span>(
  </span><span>SELECT</span> tab_col_name <span>FROM</span> table_name <span>WHERE</span><span> expr)
  </span><span>VALUES</span><span>(
    table_type(</span><span>'xx</span><span>xx</span><span>'</span><span>)
  )
);
</span>

 

 更改

oracle:变长数组varray,嵌套表,集合

<span>UPDATE</span> <span>TABLE</span><span>(
  </span><span>SELECT</span> tab_col_name <span>FROM</span> table_name <span>WHERE</span><span> expr
) T
</span><span>SET</span><span>
  VALUE(T)  </span><span>=</span><span> table_type(
    </span><span>'</span><span>xxxx</span><span>'</span><span>)
)
</span><span>WHERE</span><span> 
  VALUE(T) </span><span>=</span><span> table_type(
    expr2
   );</span>

oracle:变长数组varray,嵌套表,集合

 

 T 为获得需要修改的那行数据对应的 嵌套表位置,WHERE 为判断语句,如果table_type 为object类型,expr2 这样写: 'x','xx','xxx'

删除:

oracle:变长数组varray,嵌套表,集合

<span>DELETE</span> <span>FROM</span> <span>TABLE</span><span>(
  </span><span>SELECT</span> tab_col_name <span>FROM</span> table_name <span>WHERE</span><span> expr
)T
</span><span>WHERE</span><span>
  VALUE(T) </span><span>=</span><span> table_type(
    expr2
  );</span>

oracle:变长数组varray,嵌套表,集合

 

 

集合方法

 

EXISTS(N)

如果第n个元素存在,返回TRUE

COUNT

该函数集合元素的数目

DELETE

DELETE(n)

DELETE(n,m)

删除集合元素

l         删除所有元素

l         删除第n个元素

l         删除n到m的元素

FIRST

返回集合第一个(最小的)元素索引号,如果集合为空,返回NULL

LAST

返回集合中最后一个(最大的)元素索引号,如果集合为空,返回NULL

NEXT(n)

返回集合当前元素的下n元素的索引号,如果它不存在就返回NULL

PRIOR(n)

返回集合当前元素的前n元素的索引号,如果它不存在就返回NULL

LIMIT

返回varray中创建元素的最大个数

EXTEND

EXTEND(n)

EXTEND(n,m)

增加集合的大小。

l         添加一个,设为空

l         添加n个,设为空

l         添加n个,设为m

TRIM

TRIM(n)

从集合末尾处删除元素

l         删除一个

l         删除n个

 调用方法是: tab_col_name.COUNT

嵌套表运算符操作

例如:

var_tab_1 table_type;

var_tab_2 table_type;

var_tab_3 table_type;

reslut BOOLEAN;

 

var_tab_1 :=table_type('1','2');

var_tab_2 :=table_type('3','4');

var_tab_3 :=table_type('2','1');

 

result:= var_tab_1 =var_tab_3  result 为true;

result:= var_tab_2 var_tab_3  result 为true;

 

IN 和 NOT IN 运算符

用于检测一个嵌套表的内容是否出现在令一个嵌套表的内容中。

result:= var_tab_1 IN (var_tab_3);   result 为 TRUE;

result:= var_tab_2 NOT IN (var_tab_3);   result 为 TRUE;

 

SUBMULITSET 子集运算符

检查一个嵌套表的内容是否为另外一个嵌套表的子集

result:= var_tab_1 SUBMULITSET OF var_tab_3  result 为TRUE;

 

MULTISET 集合运算符

返回的是一个嵌套集

MULTISET UNION

MULTISET INTERSECT

MULTISET EXCEPT

并 交  差 ,另外还有

ALL  全部

DISTINCT 去重

var_tab1 := var_tab2 MUSTISET UNION ALL var_tab2

 

CARDINALITY 获得嵌套表中元素数目

CARDINALITY(var_tab1)

(跟count 有什么不同。。- -!)

 

 MEMBER OF  运算符

检测嵌套表的一个元素是否存在

'xxx' MEMBER OF var_tab1;  返回BOOLEAN 

 

SET 运算符

将传入的嵌套表去重后返回

var_tab1 := SET (var_tab2);

 

IS A SET 

判断时候符合每个元素都不同

result:= var_tab1 IS A SET;

 

IS EMPTY

判断嵌套表是否为空

 

 COLLECT 运算符

 将值列表作为嵌套表返回,可以配合 CAST 运算符将返回的嵌套表强制转换为一种嵌套表类型。

<span>SELECT</span><span> COLLECT(column_name)
</span><span>FROM</span> <span>TABLE</span>
<span>WHERE</span> expr;

 

 

POWERMULTISET 

获得嵌套表的子嵌套表

<span>SELECT</span> <span>*</span>
<span>FROM</span> <span>TABLE</span><span>(
  POWERMULTISET(table_tpye(</span><span>'</span><span>1</span><span>'</span>,<span>'</span><span>2</span><span>'</span><span>))
);</span>

 

获得

table_type(<span>'</span><span>1</span><span>'</span><span>)
table_type(</span><span>'</span><span>2</span><span>'</span><span>)
table_type(</span><span>'</span><span>1</span><span>'</span>,<span>'</span><span>2</span><span>'</span>)

 

注意:PS/SQL 不支持这个

 

POWERMULTISET_BY_CARDINALITY 

获得指定长度以下的嵌套表

<span>SELECT</span> <span>*</span>
<span>FROM</span> <span>TABLE</span><span>(
  POWERMULTISET_BY_CARDINALITY(table_tpye(</span><span>'</span><span>1</span><span>'</span>,<span>'</span><span>2</span><span>'</span>),<span>2</span><span>)
);</span>

 

获得

table_type(<span>'</span><span>1</span><span>'</span>,<span>'</span><span>2</span><span>'</span>)

 

 

PS/SQL 不支持这个

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor