- Neo4j Tutorial
- Neo4j - Home
- Neo4j - Overview
- Neo4j - Data Model
- Neo4j - Environment Setup
- Neo4j - Building Blocks
- Neo4j CQL Write Clauses
- Neo4j - Merge Command
- Neo4j - Set Clause
- Neo4j - Delete Clause
- Neo4j - Remove Clause
- Neo4j - Foreach Clause
- Neo4j CQL Read Clause
- Neo4j - Match Clause
- Neo4j - Optional Match Clause
- Neo4j - Where Clause
- Neo4j - Count Function
- Neo4j CQL General Clauses
- Neo4j - Return Clause
- Neo4j - Order By Clause
- Neo4j - Limit Clause
- Neo4j - Skip Clause
- Neo4j - With Clause
- Neo4j - Unwind Clause
- Neo4j CQL Functions
- Neo4j - String Functions
- Neo4j - Aggregation Function
- Neo4j CQL Admin
- Neo4j - Backup & Restore
- Neo4j - Index
- Neo4j - Create Unique Constraint
- Neo4j - Drop Unique
- Neo4j Useful Resources
- Neo4j - Quick Guide
- Neo4j - Useful Resources
- Neo4j - Discussion
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 Element | Description |
---|---|---|
1. | CREATE,RETURN | They 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
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.
Step 3 - Click on "Execute" button and observe the results.
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
Step 5 - Click on "Execute" button to view the results.By default, it shows the results in UI Mode.
Click on Nodes and Relationship to view its Properties Window.
YoutubeVideo1 Node Properties
YoutubeVideo2 Node Properties
ACTION_MOVIES Relationship Properties
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.
To Continue Learning Please Login
Login with Google