Neo4j 简明教程
Neo4j CQL - Creating Nodes
正如讨论的那样,一个节点是图数据库中的一个数据/记录。你可以使用 CREATE 从句在 Neo4j 中创建一个节点。本章教你如何 -
As discussed, a node is a data/record in a graph database. You can create a node in Neo4j using the CREATE clause. This chapter teaches you how to −
-
Create a single node
-
Create multiple nodes
-
Create a node with a label
-
Create a node with multiple labels
-
Create a node with properties
-
Returning the created node
Creating a Single node
你可以简单地通过指定要创建的节点名称,并使用 CREATE 从句来在 Neo4j 中创建一个节点。
You can create a node in Neo4j by simply specifying the name of the node that is to be created along with the CREATE clause.
Syntax
以下是使用 Cypher 查询语言创建节点的语法。
Following is the syntax for creating a node using Cypher Query Language.
CREATE (node_name);
Note - 分号 (;) 是可选的。
Note − Semicolon (;) is optional.
Example
以下是一个示例 Cypher 查询,在 Neo4j 中创建了一个节点。
Following is a sample Cypher Query which creates a node in Neo4j.
CREATE (sample)
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j 桌面应用程序,并启动 Neo4j 服务器,如下图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server as shown in the following screenshot.

Step 2 - 打开你的浏览器,复制代码并粘贴下面的 URL 到你的地址栏 http://localhost:7474/. 这将给你 Neo4j 的内建浏览器应用程序,其中有个美元提示符,如下图所示。
Step 2 − Open your browser, copy paste the following URL in your address bar http://localhost:7474/. This will give you the built-in browser app of Neo4j with a dollar prompt as shown in the following screenshot.

Step 3 - 将目标查询复制并粘贴到美元提示符中,然后按下在以下截图中突出显示的播放按钮(执行查询)。
Step 3 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Verification
要验证节点类型的创建,在美元提示符中执行以下查询。
To verify the creation of the node type, execute the following query in the dollar prompt.
MATCH (n) RETURN n
此查询返回数据库中的所有节点(我们将在即将到来的章节中详细讨论此查询)。
This query returns all the nodes in the database (we will discuss this query in detail in the coming chapters).
执行此查询后,将显示创建的节点,如下图所示。
On executing, this query shows the created node as shown in the following screenshot.

Creating Multiple Nodes
Neo4j CQL 的 create 子句也用于同时创建多个节点。为此,你需要传递要创建的节点的名称,用逗号分隔。
The create clause of Neo4j CQL is also used to create multiple nodes at the same time. To do so, you need to pass the names of the nodes to be created, separated by a comma.
Syntax
以下是使用 CREATE 从句创建多个节点的语法。
Following is the syntax to create multiple nodes using the CREATE clause.
CREATE (node1),(node2)
Example
以下是一个示例 Cypher 查询,在 Neo4j 中创建多个节点。
Following is a sample Cypher Query which creates multiple nodes in Neo4j.
CREATE (sample1),(sample2)
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Verification
要验证节点的创建,请在美元提示符中键入并执行以下查询。
To verify the creation of the node, type and execute the following query in the dollar prompt.
MATCH (n) RETURN n
此查询返回数据库中的所有节点(我们将在即将到来的章节中详细讨论此查询)。
This query returns all the nodes in the database (we will discuss this query in detail in the coming chapters).
执行此查询后,将显示创建的节点,如下图所示。
On executing, this query shows the created node as shown in the following screenshot.

Creating a Node with a Label
Neo4j 中的标签用于使用标签对节点进行分组(分类)。你可以使用 CREATE 从句为 Neo4j 中的节点创建一个标签。
A label in Neo4j is used to group (classify) the nodes using labels. You can create a label for a node in Neo4j using the CREATE clause.
Syntax
以下是使用 Cypher 查询语言用标签创建节点的语法。
Following is the syntax for creating a node with a label using Cypher Query Language.
CREATE (node:label)
Example
以下是一个创建带有标签的节点的示例 Cypher 查询。
Following is a sample Cypher Query which creates a node with a label.
CREATE (Dhawan:player)
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Verification
要验证节点的创建,请在美元提示符中键入并执行以下查询。
To verify the creation of the node, type and execute the following query in the dollar prompt.
MATCH (n) RETURN n
此查询返回数据库中的所有节点(我们将在即将到来的章节中详细讨论此查询)。
This query returns all the nodes in the database (we will discuss this query in detail in the coming chapters).
执行此查询后,将显示创建的节点,如下图所示。
On executing, this query shows the created node as shown in the following screenshot.

Creating a Node with Multiple Labels
还可以为单个节点创建多个标签。需要通过用冒号“ : ”分隔标签来指定节点的标签。
You can also create multiple labels for a single node. You need to specify the labels for the node by separating them with a colon “ : ”.
Syntax
以下是创建具有多个标签的节点的语法。
Following is the syntax to create a node with multiple labels.
CREATE (node:label1:label2:. . . . labeln)
Example
以下是一个在 Neo4j 中创建带有多个标签的节点的示例 Cypher 查询。
Following is a sample Cypher Query which creates a node with multiple labels in Neo4j.
CREATE (Dhawan:person:player)
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Verification
要验证节点的创建,请在美元提示符中键入并执行以下查询。
To verify the creation of the node, type and execute the following query in the dollar prompt.
MATCH (n) RETURN n
此查询返回数据库中的所有节点(我们将在即将到来的章节中详细讨论此查询)。
This query returns all the nodes in the database (we will discuss this query in detail in the coming chapters).
执行此查询后,将显示创建的节点,如下图所示。
On executing, this query shows the created node as shown in the following screenshot.

Create Node with Properties
属性是节点用于存储数据 key-value 对。可以使用 CREATE 子句创建带有属性的节点。需要在花括号“ { }”中用逗号分隔指定这些属性。
Properties are the key-value pairs using which a node stores data. You can create a node with properties using the CREATE clause. You need to specify these properties separated by commas within the flower braces “{ }”.
Syntax
以下是创建带有属性的节点的语法。
Following is the syntax to create a node with properties.
CREATE (node:label { key1: value, key2: value, . . . . . . . . . })
Example
以下是一个创建带有属性的节点的示例 Cypher 查询。
Following is a sample Cypher Query which creates a node with properties.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

Verification
要验证节点的创建,请在美元提示符中键入并执行以下查询。
To verify the creation of the node, type and execute the following query in the dollar prompt.
MATCH (n) RETURN n
此查询返回数据库中的所有节点(我们将在即将到来的章节中详细讨论此查询)。
This query returns all the nodes in the database (we will discuss this query in detail in the coming chapters).
执行此查询后,将显示创建的节点,如下图所示。
On executing, this query shows the created node as shown in the following screenshot.

Returning the Created Node
在整个章节中,我们使用 MATCH (n) RETURN n 查询来查看创建的节点。此查询返回数据库中所有现有的节点。
Throughout the chapter, we used the MATCH (n) RETURN n query to view the created nodes. This query returns all the existing nodes in the database.
我们能够使用 RETURN 子句和 CREATE 来代替上述方法来查看新创建的节点。
Instead of this, we can use the RETURN clause with CREATE to view the newly created node.
Syntax
以下是返回 Neo4j 中节点的语法。
Following is the syntax to return a node in Neo4j.
CREATE (Node:Label{properties. . . . }) RETURN Node
Example
以下是一个创建带有属性并返回该属性的节点的示例 Cypher 查询。
Following is a sample Cypher Query which creates a node with properties and returns it.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"}) RETURN Dhawan
执行上述查询,执行以下步骤:
To execute the above query, carry out the following steps −
Step 1 - 打开 Neo4j Desktop App 并启动 Neo4j Server。使用 URL http://localhost:7474/ 打开 Neo4j 的内置浏览器应用,如下面的屏幕截图所示。
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.

Step 2 - 在美元提示符中复制并粘贴所需的查询,并按播放按钮(执行查询)突出显示在下面的屏幕截图中。
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
