Nodes & Relationships¶
Representing Nodes in Cypher¶
Node Variables¶
To depict nodes in Cypher, we surround the node with parentheses, e.g. (node)
.
Node Variables. If we later want to refer to the node, we can give it a variable
like (p)
for person or (t)
for thing.
In real-world queries, we might use longer, more expressive variable names like (person)
or (thing)
.
Just like in any other programming language, you can name your variables however you want and reference them by that same name later in a query.
Node Labels¶
Labels are kind of like tags. For example :
(p:Employee)
or (k:Customer)
.
It feels like telling SQL
which table to look for when investigating a the particular row.
Just like to tell SQL to query a person’s information from an Employee
or Customer
table.
Note
If you do not specify a label for Cypher to filter out non-matching node categories, the query will check all of the nodes in the database! This would be cumbersome if you had a very large graph.
() //anonymous node (no label or variable) can refer to any node in the database
(p:Person) //using variable p and label Person
(:Technology) //no variable, label Technology
(work:Company) //using variable work and label Company
Node Properties¶
Extra info can be added to the different nodes.
CREATE (john:Person {name: 'John',age:50,job:'Trader'})
A property can have the following data types:
Number
Integer
Float
String
Boolean
Temporal types: Date, Time, LocalTime, DateTime, LocalDateTime and Duration
Relationships in Cypher¶
We need to express the relationships between our nodes.
Relationships are represented in Cypher using an
arrow -->
or <--
between two nodes. Relationships are preferably
given a name in UPPERCASE. Undirected relationships are represented with
no arrow and just two dashes --
.
Create a Person (a)
that likes Technology (c)
:
CREATE (a:Person)-[:LIKES]->(c:Technology)
Add another person (b)
that likes the same Technology node
MATCH (t:Technology)
CREATE (b:Person)-[:LIKES]-> (t)
Relationship Variables:
As an example, you could use either -[rel]->
or -[rel:LIKES]->
and call the rel
variable later in your query to reference the relationship and its details.
Node or Relationship Properties¶
This is the final step in creating the structure. We can add properties to nodes.
Adding a Person with name
Jennifer to the network , who likes
Technology since
2018.
MATCH (t:Technology)
CREATE (p:Person {name: 'Jennifer'})-[:LIKES {since:2018}]-> (t)
Patterns in Cypher¶
Nodes and relationships make up the building blocks for graph patterns.
The following pattern finds us Persons
that like Technology
.
(p:Person)-[:LIKES]->(t:Technology)
Of course this pattern delivers us nothing. It merely specifies what we are looking for. The pattern needs to be enriched with keywords.
MATCH (p:Person)-[:LIKES]->(t:Technology)
RETURN (p)
Naming Conventions¶
There are some recommended naming conventions:
Node labels : Camel-case, beginning with an upper-case character :VehicleOwner rather than :vehicle_owner
Relationship types: Upper-case, using underscore to separate words :OWNS_VEHICLE rather than :ownsVehicle
Last change: Oct 30, 2023