Relationship With Properties With New Nodes



In this scenario, we are going to create two Nodes and Relationship with 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>{<define-properties-list>})-
	[<relationship-label-name>:<relationship-name>{<define-properties-list>}]
	->(<node1-label-name>:<node1-name>{<define-properties-list>})
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.

We can represent this {<define-properties-list>} as below.

<define-properties-list> Syntax

{ 
	<property1-name>:<property1-value>,
	<property2-name>:<property2-value>,
	...
	<propertyn-name>:<propertyn-value>
}

Example

This example demonstrates how to create From Node, Relationship and To Node with Properties 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 (video1:YoutubeVideo1{title:"Action Movie1",updated_by:"Abc",uploaded_date:"10/10/2010"})
-[movie:ACTION_MOVIES{rating:1}]->
(video2:YoutubeVideo2{title:"Action Movie2",updated_by:"Xyz",uploaded_date:"12/12/2012"}) 

Here relationship name is "ACTION_MOVIES"

Relationship label is "movie".

video1 and YoutubeVideo1 are node name and node label name of "From Node" respectively.

video2 and YoutubeVideo2 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 (video1:YoutubeVideo1)-[movie:ACTION_MOVIES]->(video2:YoutubeVideo2) 
RETURN movie
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

Click on Nodes and Relationship to view its Properties Window.

YoutubeVideo1 Node Properties

Neo4J CQL Tutorial

YoutubeVideo2 Node Properties

Neo4J CQL Tutorial

ACTION_MOVIES Relationship Properties

Neo4J CQL Tutorial

NOTE -

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

Advertisements