Neo4j 简明教程

Neo4j CQL - Creating a Relationship

在 Noe4j 中,关系是一个元素,我们使用它来连接图中的两个节点。这些关系具有方向、类型和数据形式模式。本章将介绍如何:

In Noe4j, a relationship is an element using which we connect two nodes of a graph. These relationships have direction, type, and the form patterns of data. This chapter teaches you how to −

  1. Create relationships

  2. Create a relationship between the existing nodes

  3. Create a relationship with label and properties

Creating Relationships

我们可以使用 CREATE 子句创建一个关系。我们将根据关系的方向在方括号 “[ ]” 中指定关系,因为它是放在连字符 “ - ” 和箭头 “ → ” 之间的,如下所示语法中所示。

We can create a relationship using the CREATE clause. We will specify relationship within the square braces “[ ]” depending on the direction of the relationship it is placed between hyphen “ - ” and arrow “ → ” as shown in the following syntax.

Syntax

以下是使用 CREATE 子句创建关系的语法。

Following is the syntax to create a relationship using the CREATE clause.

CREATE (node1)-[:RelationshipType]->(node2)

Example

首先,在数据库中创建两个节点 Ind 和 Dhawan,如下所示。

First of all, create two nodes Ind and Dhawan in the database, as shown below.

CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
CREATE (Ind:Country {name: "India"})

现在,在两个节点之间创建一个名为 BATSMAN_OF 的关系,如下所示:

Now, create a relationship named BATSMAN_OF between these two nodes as −

CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)

最后,返回两个节点以查看已创建的关系。

Finally, return both the nodes to see the created relationship.

RETURN Dhawan, Ind
browser app

将所需查询复制并粘贴到美元提示符中,然后按下以下屏幕截图中突出显示的播放按钮(以执行查询)。

Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.

highlighted query

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

executing

Creating a Relationship Between the Existing Nodes

你还可以使用 MATCH 子句在现有节点之间创建关系。

You can also create a relationship between the existing nodes using the MATCH clause.

Syntax

以下是使用 MATCH 子句创建关系的语法。

Following is the syntax to create a relationship using the MATCH clause.

MATCH (a:LabeofNode1), (b:LabeofNode2)
   WHERE a.name = "nameofnode1" AND b.name = " nameofnode2"
CREATE (a)-[: Relation]->(b)
RETURN a,b

Example

以下就是一个使用 match 子句创建关系的 Cypher 查询示例。

Following is a sample Cypher Query which creates a relationship using the match clause.

MATCH (a:player), (b:Country) WHERE a.name = "Shikar Dhawan" AND b.name = "India"
CREATE (a)-[r: BATSMAN_OF]->(b)
RETURN a,b

要执行以上查询,请执行以下步骤。

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.

browser app

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.

existing node

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

executing

Creating a Relationship with Label and Properties

可以使用 CREATE 子句创建带有标签和属性的关系。

You can create a relationship with label and properties using the CREATE clause.

Syntax

下面是使用 CREATE 子句创建带有标签和属性的关系的语法。

Following is the syntax to create a relationship with label and properties using the CREATE clause.

CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2)

Example

下面是一个示例 Cypher 查询,它创建了一个带有标签和属性的关系。

Following is a sample Cypher Query which creates a relationship with label and properties.

MATCH (a:player), (b:Country) WHERE a.name = "Shikar Dhawan" AND b.name = "India"
CREATE (a)-[r:BATSMAN_OF {Matches:5, Avg:90.75}]->(b)
RETURN a,b

执行上述查询,执行以下步骤:

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.

browser app

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.

label property

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

executing

Creating a Complete Path

在 Neo4j 中,关系连续使用路径进行形成。可以使用 create 子句创建路径。

In Neo4j, a path is formed using continuous relationships. A path can be created using the create clause.

Syntax

下面是使用 CREATE 子句在 Neo4j 中创建路径的语法。

Following is the syntax to create a path in Neo4j using the CREATE clause.

CREATE p = (Node1 {properties})-[:Relationship_Type]->
   (Node2 {properties})[:Relationship_Type]->(Node3 {properties})
RETURN p

Example

执行上述查询,执行以下步骤:

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.

browser app

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.

champions trophy

Result

执行后,您将获得以下结果。

On executing, you will get the following result.

result