Cypher Fundamentals-Creating Relationships
QUIZ
Creating Relationships
Creating a relationship between two nodes
In this lesson you will learn how to write Cypher clauses to create relationships between existing nodes in the graph.
Just like you can use MERGE
to create nodes in the graph, you use MERGE
to create relationships between two nodes. First you must have references to the two nodes you will be creating the relationship for. When you create a relationship between two nodes, it must have:
-
Type
-
Direction
For example, if the Person and Movie nodes both already exist, we can find them using a MATCH
clause before creating the relationship between them.
MATCH (p:Person {name: 'Michael Cain'})
MATCH (m:Movie {title: 'The Dark Knight'})
MERGE (p)-[:ACTED_IN]->(m)
Here we find the two nodes that we want to create the relationship between. Then we use the reference to the found nodes to create the ACTED_IN relationship.
We can confirm that this relationship exists as follows:
MATCH (p:Person {name: 'Michael Cain'})-[:ACTED_IN]-(m:Movie {title: 'The Dark Knight'})
RETURN p, m
By default, in Neo4j Browser, the visualization connects nodes that have relationships between them.
Notice also that you need not specify direction in the MATCH
pattern since the query engine will look for all nodes that are connected, regardless of the direction of the relationship.
For example, if we specified this relationship pattern:
MATCH (p:Person {name: 'Michael Cain'})<-[:ACTED_IN]-(m:Movie {title: 'The Dark Knight'})
RETURN p, m
This query returns no nodes since there are no nodes with the ACTED_IN relationship to Person nodes in the graph.
Creating nodes and relationships using multiple clauses
We can also chain multiple MERGE
clauses together within a single Cypher code block.
MERGE (p:Person {name: 'Chadwick Boseman'})
MERGE (m:Movie {title: 'Black Panther'})
MERGE (p)-[:ACTED_IN]-(m)
This code creates two nodes and a relationship between them. Because we have specified the variables p and m, we can use them in the code to create the relationship between the two nodes.
Note that in this MERGE
clause where we create the relationships, we did not specify the direction of the relationship. By default, if you do not specify the direction when you create the relationship, it will always be assumed left-to-right.
We can confirm that this relationship exists as follows:
MATCH (p:Person {name: 'Chadwick Boseman'})-[:ACTED_IN]-(m:Movie {title: 'Black Panther'})
RETURN p, m
Using MERGE
to create nodes and a relationship in single clause
What MERGE
does is create the node or relationship if it does not exist in the graph.
This code successfully creates the nodes and relationship:
MERGE (p:Person {name: 'Emily Blunt'})-[:ACTED_IN]->(m:Movie {title: 'A Quiet Place'})
RETURN p, m
You can execute this Cypher code multiple times and it will not create any new nodes or relationships.
Check your understanding
1. Creating relationships
From what you have learned thus far, what clause do you use to create a relationship?
-
INSERT
ADD
ADD RELATIONSHIP
MERGE
2. Using MERGE
for relationships
Suppose you want to create the LIKES relationship between a reference to a node a, and a reference to a node b where the direction of the relationship is from a to b.
Which statements below will create the LIKES relationship from a to b?
MERGE (a)-[LIKES]→(b)
MERGE (a)-[:LIKES]→(b)
MERGE (a)-[LIKES]-(b)
MERGE (a)-[:LIKES]-(b)
3. Create relationship between two existing nodes
Suppose our graph has a Person node for Lucille Ball and a Movie node for Mame. How do we create the ACTED_IN relationship between these two nodes?
Use the dropdown below complete the code.
Once you have selected your option, click the Check Results query button to continue.
MERGE (p:Person {name: 'Lucille Ball'})
(m:Movie {title: 'Mame'})
RETURN p, m