In Noe4j, a dating is an element the use of which we connect nodes of a graph. These relationships have direction, type, and the form patterns of statistics. This bankruptcy teaches you how to −
- Create relationships
- Create a dating between the existing nodes
- Create a relationship with label and residences
Creating Relationships
We can create a dating the use of the CREATE clause. We will specify dating inside the rectangular braces “[ ]” depending at the route of the connection it's miles located among hyphen “ - ” and arrow “ → ” as proven inside the following syntax.
Syntax
Following is the syntax to create a dating the use of the CREATE clause.
CREATE (node1)-[:RelationshipType]->(node2)
Example
First of all, create nodes Ind and Dhawan in the database, as shown beneath.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
CREATE (Ind:Country {name: "India"})
Now, create a dating named BATSMAN_OF between these nodes as −
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)
Finally, return each the nodes to look the created relationship.
RETURN Dhawan, Ind
Copy and paste the desired question in the dollar spark off and press the play button (to execute the query) highlighted in the following screenshot.
Result
On executing, you will get the subsequent end result.
Creating a Relationship Between the Existing Nodes
You can also create a dating between the existing nodes the use of the MATCH clause.
Syntax
Following is the syntax to create a courting 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
Following is a sample Cypher Query which creates a relationship the use of 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 subsequent steps.
Step 1 − Open the Neo4j computing device App and begin the Neo4j Server. Open the integrated browser app of Neo4j the usage of the URL http://localhost:7474/ as proven within the following screenshot.
Step 2 − Copy and paste the desired query in the dollar set off and press the play button (to execute the question) highlighted in the following screenshot.
Result
On executing, you may get the following end result.
Creating a Relationship with Label and Properties
You can create a relationship with label and homes the use of the CREATE clause.
Syntax
Following is the syntax to create a dating with label and homes the usage of the CREATE clause.
CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2)
Example
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 question, perform the following steps −
Step 1 − Open the Neo4j laptop App and start the Neo4j Server. Open the built-in browser app of Neo4j the use of the URL http://localhost:7474/ as proven within the following screenshot.
Step 2 − Copy and paste the desired question in the dollar prompt and press the play button (to execute the query) highlighted within the following screenshot.
Result
On executing, you may get the following result.
Creating a Complete Path
In Neo4j, a route is shaped using continuous relationships. A direction may be created the usage of the create clause.
Syntax
Following is the syntax to create a direction in Neo4j the use of the CREATE clause.
CREATE p = (Node1 {properties})-[:Relationship_Type]->
(Node2 {properties})[:Relationship_Type]->(Node3 {properties})
RETURN p
Example
To execute the above question, carry out the following steps −
Step 1 − Open the Neo4j laptop App and begin the Neo4j Server. Open the integrated browser app of Neo4j the use of the URL http://localhost:7474/ as shown inside the following screenshot.
Step 2 − Copy and paste the favored question in the dollar activate and press the play button (to execute the query) highlighted inside the following screenshot.
Result
On executing, you may get the following result.