Relationship without Properties with New Nodes



In this scenario, we are going to create two Nodes and Relationship without properties at a time. That means, our Neo4J Database does NOT have these two nodes.

We use CQL CREATE command to create both end Nodes and new Relationship between them at a time.

Syntax

CREATE  
   (<node1-label-name>:<node1-name>)-
   [<relationship-label-name>:<relationship-name>]->
   (<node1-label-name>:<node1-name>)
RETURN <relationship-label-name>

Syntax Description

S.No.Syntax ElementDescription
1.CREATE,RETURNThey are Neo4J CQL keywords.
2.<node1-name>It is a name of "From Node" used to create a Relationship.
3.<node1-label-name>It is a label name of "From Node" used to create a Relationship.
4.<node2-name>It is a name of "To Node" used to create a Relationship.
5.<node2-label-name>It is a label name of "To Node" used to create a Relationship.
6.<relationship-name>It is a name of a Relationship.
7.<relationship-label-name>It is a label name of a Relationship.

NOTE -

In this syntax, RETURN clause is optional. If we want to see results immediately, then use it. Otherwise we can omit this clause.

Example

This example demonstrates how to create From Node, Relationship and To Node at a time by using CQL CREATE command.

Step 1 - Open Neo4J Data Browser

Neo4J CQL Tutorial

Step 2 - Type the below command on Data Browser

CREATE (fb1:FaceBookProfile1)-[like:LIKES]->(fb2:FaceBookProfile2) 

Here relationship name is "LIKES"

Relationship label is "like".

fb1 and FaceBookProfile1 are node name and node label name of "From Node" respectively.

fb2 and FaceBookProfile2 are node name and node label name of "To Node" respectively.

Neo4J CQL Tutorial

Step 3 - Click on "Execute" button and observe the results.

Neo4J CQL Tutorial

If we see the success message, we can observe two labels, two nodes and a relationship is created at a time.

Step 4 - Type the below command on Data Browser

MATCH (fb1:FaceBookProfile1)-[like:LIKES]->(fb2:FaceBookProfile2) 
RETURN like
Neo4J CQL Tutorial

Step 5 - Click on "Execute" button to view the results.By default, it shows the results in UI Mode.

Neo4J CQL Tutorial

NOTE -

If you observe the above diagram, it shows Nodes and Relationships in Bigger Font Size than previous diagrams. Because here we have used different Neo4J Data Browser Font and Color.

Please refer Neo4J Data Browser Graph Font chapter to understand how to change a Graph Font and Color

NOTE -

If we follow same steps of Example1 to create Relationship from FaceBookProfile2 to FaceBookProfile1 (That is in opposite direction), then we will have Bi-directional Relationship without Properties with new Nodes.

Advertisements