To fully utilize the power of a graph database, we also need to express the relationships between our nodes.
Relationships are represented in Cypher using an arrow
<-- between two nodes.
Notice how the syntax looks like the arrows and lines connecting our nodes in the visual representation.
Additional information, such as how nodes are connected (relationship type) and any properties pertaining to the relationship, can be placed in square brackets inside of the arrow.
In our example, the lines with
WORKS_FOR between nodes are our relationships.
Undirected relationships are represented with no arrow and just two dashes
This means that the relationship can be traversed in either direction.
While a direction must be inserted to the database, it can be matched with an undirected relationship where Cypher ignores any particular direction and retrieves the relationship and connected nodes, no matter what the physical direction is.
This allows the queries to be flexible and not force the user to know the physical direction of the relationship stored in the database.
Relationship types categorize and add meaning to a relationship, similar to how labels group nodes. In our property graph data model, relationships show how nodes are connected and related to each other. You can usually identify relationships in your data model by looking for actions or verbs.
You can specify any type of relationship you want between nodes, but we recommend good naming conventions using verbs and actions. Poor relationship type names make it more difficult to both read and write Cypher (remember, it should sound like English!).
For example, let us look at the relationship types from our example graph.
[:LIKES]- makes sense when we put nodes on either side of the relationship (Jennifer LIKES Graphs)
[:IS_FRIENDS_WITH]- makes sense when we put nodes with it (Jennifer IS_FRIENDS_WITH Michael)
[:WORKS_FOR]- makes sense with nodes (Jennifer WORKS_FOR Neo4j)
Just as we did with nodes, if we want to refer to a relationship later in a query, we can give it a variable like
We can also use longer, more expressive variable names like
If you do not need to reference the relationship later, you can specify an anonymous relationship using two dashes
--+, +-->+, +<--.
As an example, you could use either
-[rel:LIKES]-> and call the
rel variable later in your query to reference the relationship and its details.
This is a companion discussion topic for the original entry at https://neo4j.com/developer/cypher/syntax/