Relationship without Properties with Existing Nodes



In this scenario, we are going to use two existing nodes : CreditCard and Customer to create a Relationship without 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>]->(<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.

NOTE -

In this syntax, RETURN clause is optional. If we want to see results immediately, then use it. Otherwise we can omit this clause.

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

It is Neo4J Data Browser Homepage.

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 (e:Customer),(cc:CreditCard) 
CREATE (e)-[r:DO_SHOPPING_WITH ]->(cc) 

Here relationship name is "DO_SHOPPING_WITH"

Relationship label is "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 observe the results.

Neo4J CQL Tutorial

It shows that one new Relationship is added to the Neo4J Database

Step 5 - Type the below command on Data Browser

MATCH (e)-[r:DO_SHOPPING_WITH ]->(cc) 
RETURN r
Neo4J CQL Tutorial

Step 6 - Click on "Execute" button to view the results.By default, it shows the results in UI Mode.

Neo4J CQL Tutorial

Click and drag Nodes and relationships represented as Circles and arrow mark to view the diagram in the proper view

Neo4J CQL Tutorial

Step 7 - View the each node and relationship properties in detail.

Click on "Customer" or CreditCard node to view its properties in a separate window

Neo4J CQL Tutorial

Click on "DO_SHOPPING_WITH" relationship to view its property. As we have created this relationship without any properties, Properties tab is showing "No Properties" message.

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 without Properties with existing Nodes.

Advertisements