Creating cities in Neo4j
We will model cities as nodes, as shown in Figure 2.4, with the city's name and country as properties. Cities should have unique names. For this, we can add a constraint before we start creating cities in our graph.
The query is as follows:
neo4j-sh (?)$ CREATE CONSTRAINT ON (city: City) ASSERT city.name IS UNIQUE;
The output of the preceding query is as shown:
+-------------------+ | No data returned. | +-------------------+ Constraints added: 1
This adds a constraint on all nodes that will henceforth be created with the label City
to have a unique name property. The city in (city: City)
is a placeholder, like a variable, for any node with label City
. Note that the addition of a uniqueness constraint is idempotent—it can be repeated multiple times without throwing an error or changing the constraint after it first gets added.
It's a good practice to add a uniqueness constraint, like we did, before we start adding nodes that have a particular label. While...