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 ElementDescription
1.MATCH,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.
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 {} as below.

Syntax -

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

Syntax Description

S.No.Syntax ElementDescription
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

Neo4J CQL Tutorial

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.

Neo4J CQL Tutorial

Step 4 - Click on Execute button and see the results in UI mode.

Neo4J CQL Tutorial

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.

Neo4J CQL Tutorial

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.

Advertisements