Orientdb 简明教程

OrientDB - Sequences

Sequences 是在自动递增机制中使用的概念,它在 OrientDB v2.2 中引入。在数据库术语中,序列是一种管理计数器字段的结构。简单来说,序列通常在需要始终递增的数字时使用。它支持两种类型−

Sequences is a concept used in auto increment mechanism and it is introduced in OrientDB v2.2. In database terminology, sequence is a structure that manages the counter field. Simply said sequences are mostly used when you need a number that always increments. It supports two types−

ORDERED −每次指针调用返回新值的 .next 方法。

ORDERED − Each time the pointer calls the .next method that returns a new value.

CACHED −序列将在每个节点上缓存“N”个项目。若要调用每个项目,我们使用 .next() ,在缓存包含多个项目时首选它。

CACHED − The sequence will cache ‘N’ items on each node. To call each item we use .next(), which is preferred when the cache contains more than one item.

Create Sequence

序列通常用于自动增加人员的 ID 值。与 OrientDB 的其他 SQL 概念一样,它还执行与 RDBMS 中的序列类似的操作。

Sequence is usually used to auto increment the id value of a person. Like other SQL concepts of OrientDB it also preforms similar operations as Sequence in RDBMS.

以下语句是创建序列的基本语法。

The following statement is the basic syntax to create sequences.

CREATE SEQUENCE <sequence> TYPE <CACHED|ORDERED> [START <start>]
[INCREMENT <increment>] [CACHE <cache>]

以下是上文中选项的详细信息。

Following are the details about the options in the above syntax.

<Sequence> −序列的本地名称。

<Sequence> − Local name for sequence.

TYPE −定义序列类型为有序或缓存。

TYPE − Defines the sequence type ORDERED or CACHED.

START −定义初始值。

START − Defines the initial value.

INCREMENT −为每个 .next 方法调用定义增量。

INCREMENT − Defines the increment for each .next method call.

CACHE −定义预缓存的值的数量(如果使用了缓存序列类型)。

CACHE − Defines the number of value to pre-cache, in the event that you used to cache sequence type.

让我们创建一个以数字 1201 开头的名为“seqid”的序列。尝试以下查询使用序列实现此示例。

Let us create a sequence named ‘seqid’ which starts with number 1201. Try the following queries to implement this example with sequence.

CREATE SEQUENCE seqid START 1201

如果成功执行了以上查询,您会获得以下输出。

If the above query is executed successfully, you will get the following output.

Sequence created successfully

尝试以下查询使用序列“seqid”插入帐户表的 ID 值。

Try the following query to use sequence ‘seqid’ to insert the id value of Account table.

INSERT INTO Account SET id = sequence('seqid').next()

如果成功执行了以上查询,您会获得以下输出。

If the above query is executed successfully, you will get the following output.

Insert 1 record(s) in 0.001000 sec(s)

Alter Sequence

Alter sequence 是一条用于更改序列属性的命令。它将修改除序列类型以外的所有序列选项。

Alter sequence is a command used to change the properties of a sequence. It will modify all the sequence options except sequence type.

以下语句是更改序列的基本语法。

The following statement is the basic syntax to alter sequence.

ALTER SEQUENCE <sequence> [START <start-point>]
[INCREMENT <increment>] [CACHE <cache>]

以下是上文中选项的详细信息。

Following are the details about the options in the above syntax.

<Sequence> −定义要更改的序列。

<Sequence> − Defines the sequence you want to change.

START −定义初始值。

START − Defines the initial value.

INCREMENT −为每个 .next 方法调用定义增量。

INCREMENT − Defines the increment for each .next method call.

CACHE −定义在使用缓存序列类型时要预缓存的值的数量。

CACHE − Defines the number of value to pre-cache in the event that you used to cache sequence type.

尝试以下查询更改名为 seqid 的序列的启动值从“1201 到 1000”。

Try the following query to alter the start value from ‘1201 to 1000’ of a sequence named seqid.

ALTER SEQUENCE seqid START 1000

如果成功执行了以上查询,您会获得以下输出。

If the above query is executed successfully, you will get the following output.

Altered sequence successfully

Drop Sequence

Drop sequence 是一条用于删除序列的命令。

Drop sequence is a command used to drop a sequence.

以下语句是删除序列的基本语法。

The following statement is the basic syntax to drop a sequence.

DROP SEQUENCE <sequence>

其中 <Sequence> 为要删除的序列。

Where <Sequence> defines the sequence you want to drop.

尝试以下查询删除名为“seqid”的序列。

Try the following query to drop a sequence named ‘seqid’.

DROP SEQUENCE seqid

如果成功执行了以上查询,您会获得以下输出。

If the above query is executed successfully, you will get the following output.

Sequence dropped successfully