Neo4j CQL - Creating a Relationship

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 −
Create relationships
Create a relationship between the existing nodes
Create a relationship with label and properties
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.
Following is the syntax to create a relationship using the CREATE clause.
CREATE (node1)-[:RelationshipType]->(node2) 

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"})
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 

Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
On executing, you will get the following result.
You can also create a relationship between the existing nodes using the MATCH clause.
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 

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 − 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 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
On executing, you will get the following result.
You can create a relationship with label and properties using the CREATE clause.
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) 

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 − 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 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
On executing, you will get the following result.
In Neo4j, a path is formed using continuous relationships. A path can be created using the create clause.
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 

To execute the above query, carry out the following steps −
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 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
On executing, you will get the following result.
 Print
 Add Notes
 Bookmark this page