Orientdb 简明教程

OrientDB - Insert Record

OrientDB 是一个 NoSQL 数据库,可以存储文档和面向图的数据。NoSQL 数据库不包含任何表,那么您如何才能作为记录来插入数据。在此,您可以看到以类、特性、顶点和边的形式存在的表数据,这意味着类类似于表,而特性类似于文件。

OrientDB is a NoSQL database that can store the documents and graph-oriented data. NoSQL database does not contain any table, so how can you insert data as a record. Here you can see the table data in the form of class, property, vertex, and edge meaning classes are like tables, and properties are like files in the tables.

我们可以在 OrientDB 中使用 schema 来定义所有这些实体。可以将属性数据插入到一个类中。Insert 命令在数据库模式中创建一个新记录。记录可以无模式或遵循一些指定的规则。

We can define all these entities using schema in OrientDB. Property data can be inserted into a class. Insert command creates a new record in the database schema. Records can be schema-less or follow some specified rules.

以下是插入记录命令的基本语法。

The following statement is the basic syntax of the Insert Record command.

INSERT INTO [class:]<class>|cluster:<cluster>|index:<index>
   [(<field>[,]*) VALUES (<expression>[,]*)[,]*]|
   [SET <field> = <expression>|<sub-command>[,]*]|
   [CONTENT {<JSON>}]
   [RETURN <expression>]
   [FROM <query>]

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

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

SET − 为每个字段定义关联值。

SET − Defines each field along with the value.

CONTENT − 定义用于设置字段值的 JSON 数据。这是可选的。

CONTENT − Defines JSON data to set field values. This is optional.

RETURN − 定义要返回内容(而不是插入的记录数量)的表达式。最常见的用例为 −

RETURN − Defines the expression to return instead of number of records inserted. The most common use cases are −

  1. @rid − Returns the Record ID of the new record.

  2. @this − Returns the entire new record.

FROM – 要插入记录或结果集的位置。

FROM − Where you want to insert the record or a result set.

Example

我们考虑一个具有以下字段和类型的 Customer 表。

Let us consider a Customer table with the following fields and types.

Sr.No.

Field Name

Type

1

Id

Integer

2

Name

String

3

Age

Integer

您可以通过执行以下命令来创建架构(表)。

You can create the Schema (table) by executing the following commands.

CREATE DATABASE PLOCAL:/opt/orientdb/databases/sales
CREATE CLASS Customer
CREATE PROPERTY Customer.id integer
CREATE PROPERTY Customer.name String
CREATE PROPERTY Customer.age integer

执行所有命令后,您将获得具有 id、name 和 age 字段的表名 Customer。您可以通过在 Customer 表中执行选择查询来检查表。

After executing all the commands, you will get the table name Customer with id, name, and age fields. You can check the table by executing select query into the Customer table.

OrientDB 提供了不同的方式来插入记录。考虑以下包含示例记录的 Customer 表。

OrientDB provides different ways to insert a record. Consider the following Customer table containing the sample records.

Sr.No.

Name

Age

1

Satish

25

2

Krishna

26

3

Kiran

29

4

Javeed

21

5

Raja

29

以下命令是将第一条记录插入 Customer 表。

The following command is to insert the first record into the Customer table.

INSERT INTO Customer (id, name, age) VALUES (01,'satish', 25)

如果上述命令已成功执行,您将获得以下输出。

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

Inserted record 'Customer#11:0{id:1,name:satish,age:25} v1' in 0.069000 sec(s).

以下命令是将第二条记录插入 Customer 表。

The following command is to insert the second record into the Customer table.

INSERT INTO Customer SET id = 02, name = 'krishna', age = 26

如果上述命令已成功执行,您将获得以下输出。

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

Inserted record 'Customer#11:1{id:2,age:26,name:krishna} v1' in 0.005000 sec(s).

以下命令是将第三条记录插入 Customer 表。

The following command is to insert the third record into the Customer table.

INSERT INTO Customer CONTENT {"id": "03", "name": "kiran", "age": "29"}

如果上述命令已成功执行,您将获得以下输出。

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

Inserted record 'Customer#11:2{id:3,name:kiran,age:29} v1' in 0.004000 sec(s).

以下命令是将接下来的两条记录插入 Customer 表。

The following command is to insert the next two records into the Customer table.

INSERT INTO Customer (id, name, age) VALUES (04,'javeed', 21), (05,'raja', 29)

如果上述命令已成功执行,您将获得以下输出。

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

Inserted record '[Customer#11:3{id:4,name:javeed,age:21} v1,
Customer#11:4{id:5,name:raja,age:29} v1]' in 0.007000 sec(s).

您可以通过执行以下命令来检查是否已插入所有这些记录。

You can check if all these records are inserted or not by executing the following command.

SELECT FROM Customer

如果上述命令已成功执行,您将获得以下输出。

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

----+-----+--------+----+-------+----
#   |@RID |@CLASS  |id  |name   |age
----+-----+--------+----+-------+----
0   |#11:0|Customer|1   |satish |25
1   |#11:1|Customer|2   |krishna|26
2   |#11:2|Customer|3   |kiran  |29
3   |#11:3|Customer|4   |javeed |21
4   |#11:4|Customer|5   |raja   |29
----+-----+--------+----+-------+----