Neo4j Create Relationship - neo4j tutorial - graph database
How to Create Relationship using Cypher in Neo4j?
- We can use the CREATE statement to create relationships between those nodes.
- CREATE statement is used to create relationship between nodes. These relationships define direction, type and form patterns of the data.
- It defines mainly three things:
- Creating Relationships
- Creating Relationships between existing nodes
- Creating relationships with label and properties
- The statement for creating a relationship consists of CREATE, followed by the details of the relationship that you're creating.
Neo4j cipher relationship
Example
- Now create a relationship between some of the nodes that we created previously. First, let's create a relationship between an artist and an album.
- We'll create the following relationship:
- Here's the Cypher CREATE statement to create the above relationship:
Relative Tags : neo , neo4j , graph database , neo4j cypher , neo4j python , neo4j tutorial , neo4j download , neograft
Explanation of the Above Code
- First, we use a MATCH statement to find the two nodes that we want to create the relationship between.
- There could be many nodes with an Artist or Album label so we narrow it down to just those nodes we're interested in.
- In this case, we use a property value to filter it down. We use the Name property that we'd previously assigned to each node.
- Then there's the actual CREATE statement. This is what creates the relationship. In this case, it references the two nodes by the variable name (i.e. a and b) that we gave them in the first line.
- The relationship is established by using an ASCII-code pattern, with an arrow indicating the direction of the relationship: (a)-[r:RELEASED]->(b).
- We give the relationship a variable name of r and give the relationship a type of RELEASED (as in "this band released this album"). The relationship's type is analogous to a node's label.
Adding More Relationships
- The above example is a very simple example of a relationship. One of the things that Neo4j is really good at, is handling many interconnected relationships.
- Let's build on the relationship that we just established, so that we can see how easy it is to continue creating more nodes and relationships between them.
- So we will create one more node and add two more relationships.
- We'll end up with the following graph:
- This graph shows that Devin Townsend plays in the band, performed on the album that the band released, and he also produced the album.
- So let's start by creating the node for Devin Townsend:
- Now create the relationships and return the graph: