Neo4j CQL - Relationship Basics



Neo4j Graph Database follows the Property Graph Model to store and manages its data.

As per Property Graph Model, Relationships should be directional. Otherwise, Neo4j will throw an error message.

Based on directionality, Neo4j Relationships are categorized into two main types.

  • Uni-directional Relationships
  • Bi-directional Relationships

We can use Neo4j CQL CREATE command to create relationships between two nodes in the following scenarios. These scenarios are applicable for both Uni and Bi-directional relationships.

  • Create Relationship without Properties between two existing Nodes

  • Create Relationship with Properties between two existing Nodes

  • Create Relationship without Properties between two new Nodes

  • Create Relationship with Properties between two new Nodes

  • Create Relationship with/without Properties between two exiting Nodes with WHERE clause

NOTE -

We are going to create the relationship between Customer and CreditCard as show below

Neo4j CQL Tutorial

In the previous chapter, we have already created Customer and CreditCard nodes. Now we will see how to create a Relationship between them

This diagram depicts a relationship from Customer to CreditCard like

Customer → CreditCard

Here relationship is arrow mark (→)

As Neo4j CQL syntax is in human readable format. Neo4j CQL also uses similar kind of arrow mark to create a relationship between two nodes.

Each Relationship (→) contains two nodes

  • From Node
  • To Node

From the above diagram, Customer node is "From Node" and CreditCard Node is "To Node" to this relationship.

With respect to Nodes, they are two kinds of relationships

  • Outgoing Relationship
  • Incoming Relationship

From the above diagram, Relationship is a "Outgoing Relationship" to Customer Node and the same relationship is "Incoming Relationship" to CreditCard Node.

Consider the below diagram. Here we have created relationship from "CreditCard" Node to "Customer" Node.

Neo4j CQL Tutorial

From the above diagram, Relationship is a "Outgoing Relationship" to "CreditCard" Node and the same relationship is "Incoming Relationship" to "Customer" Node.

Consider the below diagram. Here we have created two relationships between "CreditCard" and "Customer" Nodes: One from "CreditCard" to "Customer". Another from "Customer" to "CreditCard". That means it is Bi-directional Relationship.

Neo4j CQL Tutorial

The following scenarios are explained here -

Advertisements