In order to create a relationship, we have to tell Neo4j about its start and end node, meaning the nodes need to be already in the database when creating the relationship. There are two possible solutions:
- Create nodes and the relationship(s) between them in one pass:
CREATE (n:Label {id: 1})
CREATE (m:Label {id: 2})
CREATE (n)-[:RELATED_TO]->(m)
- Create the nodes (if they don't already exist):
CREATE (:Label {id: 3})
CREATE (:Label {id: 4})
And then create the relationship. In that case, since the relationship is created in another query (another namespace), we need to first MATCH the nodes of interest:
MATCH (a {id: 3})
MATCH (b {id: 4})
CREATE (a)-[:RELATED_TO]->(b)
While nodes are identified with brackets, (), relationships are characterized by square brackets, [].
If we check the content of our graph after the first query, here is the result:
Reminder: while specifying a node label when creating a node is not mandatory, relationships must...