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.
  • Neo4j cipher relationship

    Neo4j cipher relationship

  • 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.

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:
 Neo4j Create A Relationship Using Cypher 1
  • Here's the Cypher CREATE statement to create the above relationship:
MATCH (a:Artist),(b:Album)
WHERE a.Name = "Strapping Young Lad" AND b.Name = "Heavy as a Really Heavy Thing"
CREATE (a)-[r:RELEASED]->(b)
RETURN r
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:
 Neo4j Create A Relationship Using Cypher 2
  • 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:
CREATE (p:Person { Name: "Devin Townsend" })
  • Now create the relationships and return the graph:
MATCH (a:Artist),(b:Album),(p:Person)
WHERE a.Name = "Strapping Young Lad" AND b.Name = "Heavy as a Really Heavy Thing" AND p.Name = "Devin Townsend" 
CREATE (p)-[pr:PRODUCED]->(b), (p)-[pf:PERFORMED_ON]->(b), (p)-[pl:PLAYS_IN]->(a)
RETURN a,b,p

This neo4j tutorial site provides you all the following key points and informations on neograft , neo technology , graphdb , neo4j tutorial , neo4j download , neo4j graph database , open source graph database , neo4j database , neo 4 j , nosql graph database , graph database comparison , best graph database , graphical database , graph database examples , neo database , graph database open source , in memory graph database , database graph , graph based database , graph database neo4j , neo4j pricing , neo4j graph , neo4j example , neo4j performance , neo4j license , graph data model , graph oriented database , neo4j enterprise pricing , neo4j create database , neo4j create new database , neo4j enterprise , neo4j ruby , neo4j node , neo4j with , neo4j start , mysql graph database , neo4j online , graph store , neo4j plugins , neo4j create , neo4j where , neo4j version , neo4j architecture , start neo4j , allegrograph , open source graph , graph database tutorial , neo4j query , neo4j book , what is graph database , neo4j training , apache graph database , neo4j rest api , google graph database , neo4j vs mongodb , download neo4j , python graph database , cypher neo4j , what is neo4j , neo for j , neo4j manual , neo4j spatial , graph database python , neo4j cluster , neo4j demo , neo4j wiki , neo4j docs , neo4j documentation , install neo4j , neo4j github , neo4j data modeling , mongodb vs neo4j , neo4j install , neo4j community , neo4j scalability , infinite graph , neo4j cypher tutorial , neo4j getting started , neo4j console , neo4j open source , neo4j community edition , neo4j logo , world of graphs , neo4j hosting , neo4j vs , neo4j query language , neo j , neo4j driver , neo4j client

Related Searches to Neo4j create relationship