Neo4j CQL - Relationship &; WHERE clause




Create Relationship with WHERE clause

In Neo4j CQL, We can create a Relationship between two Nodes in different ways.

  • Create a Relationship between two existing nodes

  • Create a two Nodes and Relationship between them at a time

  • Create a Relationship between two existing nodes with WHERE clause

We have already discussed first two approaches in the previous chapters. Now we are going to discuss on "Create a Relationship between two existing nodes with WHERE clause" in this chapter

Create Relationship with WHERE clause syntax:

MATCH (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) 
WHERE <condition>
CREATE (<node1-label-name>)-[<relationship-label-name>:<relationship-name>
       {<relationship-properties>}]->(<node2-label-name>) 

Syntax Description

S.No.Syntax ElementDescription
1.MATCH,WHERE,CREATEThey are Neo4j CQL keywords.
2.<node1-label-name>It is a Node one label name used to create a Relationship.
3.<node1-name>It is a Node one name used to create a Relationship.
4.<node2-label-name>It is a Node one label name used to create a Relationship.
5.<node2-name>It is a Node one name used to create a Relationship.
6.<condition>It is a Neo4j CQL WHERE clause condition. It may be simple or complex.
7.<relationship-label-name>It is a label name of newly creating Relationship between Node one and Node two.
8.<relationship-name>It is a name of newly creating Relationship between Node one and Node two.
9.<relationship-properties>It is a properties list(key-value pairs) of newly creating Relationship between Node one and Node two.

Example1:-

This example demonstrates how to Create a Relationship between two existing nodes with WHERE clause.

Steps to follow:

  • Open Neo4j Data Browser

  • Neo4j CQL Tutorial

    It is Neo4j Data Browser Homepage

  • Type the below command on Data Browser to verify our required Customer node is available in our Neo4j Database.

MATCH (cust:Customer)
RETURN cust.id,cust.name,cust.dob
Neo4j CQL Tutorial
  • Click on "Execute" button and observe the results.

  • Neo4j CQL Tutorial

    If we observe the results, it shows that our required Customer node is available in our Neo4j Database.

  • Type the below command on Data Browser to verify our required CreditCard node is available in our Neo4j Database.

  • MATCH (cc:CreditCard)
    RETURN cc.id,cc.number,cc.expiredate,cc.cvv
    
    Neo4j CQL Tutorial
  • Click on "Execute" button and observe the results.

  • Neo4j CQL Tutorial

    If we observe the results, it shows that our required CreditCard node is available in our Neo4j Database.

  • Type the below command on Data Browser to create a Relationship between Customer and CreditCard Nodes.

  • MATCH (cust:Customer),(cc:CreditCard) 
    WHERE cust.id = "1001" AND cc.id= "5001" 
    CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) 
    RETURN r
    
    Neo4j CQL Tutorial
  • Click on "Execute" button and observe the results.

  • Neo4j CQL Tutorial

    Click on Relationship and observe its properties in a separate window

    Neo4j CQL Tutorial

    Now we have created a NEW Relationship between two existing Nodes by using Neo4j CQL WHERE Clause.

    Advertisements