- 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 Existing Nodes
In this scenario, we are going to use two existing nodes : CreditCard and Customer to create a Relationship with properties. That means, our Neo4J Database should have these two nodes.
We use CQL MATCH command to retrieve existing two nodes and CQL CREATE command to create new Relationship between them.
Syntax
MATCH (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) CREATE (<node1-label-name>)-[<relationship-label-name>:<relationship-name> {<define-properties-list>}]->(<node2-label-name>) RETURN <relationship-label-name>
Syntax Description
S.No. | Syntax Element | Description |
---|---|---|
1. | MATCH,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. |
8. | <define-properties-list> | It is a list of properties(name-value pairs) assigned to newly creating 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 {
Syntax -
{ <property1-name>:<property1-value>, <property2-name>:<property2-value>, ... <propertyn-name>:<propertyn-value> }
Syntax Description
S.No. | Syntax Element | Description |
---|---|---|
1. | <propertyx-name> | It is a name of Property assigned to newly creating Relationship. Where x is 1,2,...n values |
2. | <propertyx-value> | It is a value of Property assigned to newly creating Relationship. Where x is 1,2,...n values |
NOTE -
We should use Colon(:) operator to separate Property name and value.
We should use Comma(,) operator to separate one pair of Property(name-value) with another pair of Property.
Example
This example demonstrates how to Create Uni-directional Relationship without Properties between two existing Nodes: From Customer To CreditCard.
Step 1 - Open Neo4J Data Browser
Before creating a Relationship From Customer To CreditCard, first check wither those Nodes are available in our Neo4J Database. If Not, Create them.
Step 2 - Verify Customer and CreditCard Nodes are available.
Type the below commands on Data Browser and verify them
MATCH (e:Customer) RETURN e
MATCH (cc:CreditCard) RETURN cc
Step 3 - Type the below command on Data Browser
MATCH (cust:Customer),(cc:CreditCard) CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) RETURN r
Here relationship name is "DO_SHOPPING_WITH"
Relationship label is "r".
shopdate and price are properties of Relationship "r".
e and Customer are node name and node label name of Customer Node respectively.
cc and CreditCard are node name and node label name of CreditCard Node respectively.
Step 4 - Click on Execute button and see the results in UI mode.
As we are using RETURN clause, we do not need a separate MATCH + RETRUN command to view the details. It shows both Nodes and Relationship between them in UI Mode.
Step 5 - Click on Relationship arrow mark to view its properties in a separate window as shown below
Now we are able to view two properties to DO_SHOPPING_WITH relationship.
NOTE -
If we follow same steps of Example1 to create Relationship from CreditCard to Customer (That is in opposite direction), then we will have Bi-directional Relationship withProperties with existing Nodes.