Neo4j CQL
Neo4j CQL Write Clauses
- Neo4j - Foreach Clause
- Neo4j - Remove Clause
- Neo4j - Delete Clause
- Neo4j - Set Clause
- Neo4j - Merge Command
Neo4j CQL Read Clause
Neo4j CQL General Clauses
- Neo4j - Unwind Clause
- Neo4j - With Clause
- Neo4j - Skip Clause
- Neo4j - Limit Clause
- Neo4j - Order By Clause
- Neo4j - Return Clause
Neo4j CQL Functions
Neo4j CQL Admin
Neo4j Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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
Creating Relationships
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
Following is the syntax to create a relationship using the CREATE clause.
CREATE (node1)-[:RelationshipType]->(node2)
Example
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
![Browser App](/neo4j/images/browser_app.jpg)
Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highpghted in the following screenshot.
![Highpghted Query](/neo4j/images/highpghted_query.jpg)
Result
On executing, you will get the following result.
![Executing](/neo4j/images/executing.jpg)
Creating a Relationship Between the Existing Nodes
You can also create a relationship between the existing nodes using the MATCH clause.
Syntax
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
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.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highpghted in the following screenshot.
![Existing Node](/neo4j/images/existing_node.jpg)
Result
On executing, you will get the following result.
![Executing](/neo4j/images/executing.jpg)
Creating a Relationship with Label and Properties
You can create a relationship with label and properties using the CREATE clause.
Syntax
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
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.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highpghted in the following screenshot.
![Label Property](/neo4j/images/label_property.jpg)
Result
On executing, you will get the following result.
![Executing](/neo4j/images/executing.jpg)
Creating a Complete Path
In Neo4j, a path is formed using continuous relationships. A path can be created using the create clause.
Syntax
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 − 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](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highpghted in the following screenshot.
![Champions Trophy](/neo4j/images/champions_trophy.jpg)
Result
On executing, you will get the following result.
![Result](/neo4j/images/result.jpg)