Creating Relationships

The actor’s name for Michael Caine is misspelled in the video. It has been corrected in the transcript.

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.

cypher
MATCH (p:Person {name: 'Michael Caine'})
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:

cypher
MATCH (p:Person {name: 'Michael Caine'})-[: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:

cypher
MATCH (p:Person {name: 'Michael Caine'})<-[: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.

cypher
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:

cypher
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:

cypher
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?

  • ADD

  • ADD RELATIONSHIP

  • MATCH

  • MERGE

Hint

You use this clause when you have a reference to the from node and the to node. It prevents you from creating duplicate relationships between nodes.

Solution

You use the MERGE clause when you have a reference to the from node and the to node. It prevents you from creating duplicate relationships between nodes.

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)

Hint

By default, MERGE will create the relationship in the left-to-right direction.

A relationship type must begin with ":".

Solution

MERGE (a)-[:LIKES]→(b) is correct where you are explicitly specifying the direction of the relationship between a and b.

MERGE (a)-[:LIKES]-(b) is also correct since direction of the relationship created between a and b, by default, is left to right.

These answers are incorrect, because the ":" must precede the relationship type:

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.

cypher
MERGE (p:Person {name: 'Lucille Ball'})
/*select:-[:ACTED_IN]->*/
(m:Movie {title: 'Mame'})
RETURN p, m
  • -[ACTED_IN]->

  • -[:ACTED_IN]->

  • <-[ACTED_IN]-

  • -[ACTED_IN]-

Hint

You must specify direction when you create relationships.

Solution

The answer is -[:ACTED_IN]->.

Person nodes may have one or more outgoing ACTED_IN relationships to a Movie node.

Summary

In this lesson, you learned how to create relationships in the graph. In the next challenge, you will demonstrate that you can create a relationship between nodes.