Databases Reference
In-Depth Information
bel: 'author' })
creates a node representing William Shakespeare. The newly created
node is assigned to the identifier
shakespeare
. Identifiers remain available for the du‐
ration of the current scope, but no longer; should we wish to give long-lived names to
nodes (or relationships), we simply add them to an index. This
shakespeare
identifier
is used later in the code to attach relationships to the underlying node. For example,
(shakespeare)-[:WROTE_PLAY { year: 1599 }]->(juliusCaesar)
creates a
WROTE
relationship
from
Shakespeare
to
the play
Julius Caesar
. This relationship has a
year
property with value
1599
.
Unlike the relational model, these commands don't introduce any ac‐
cidental complexity into the graph. The information meta-model—that
is, the structuring of nodes through relationships—is kept separate
from the business data, which lives exclusively as properties. We no
longer have to worry about foreign key and cardinality constraints pol‐
luting our real data, because both are explicit in the graph model in the
form of nodes and the semantically rich relationships that interconnect
them.
We can modify the graph at a later point in time in two different ways. We can, of course,
continue using
CREATE
statements to simply add to the graph. But we can also use
CREATE
UNIQUE
, which has the semantics of
ensuring
that a particular subgraph structure—some
of which may already exist, some of which may be missing—is in place once the com‐
mand has executed. In practice, we tend to use
CREATE
when we're adding to the graph
and don't mind duplication, and
CREATE UNIQUE
when duplication is not permitted by
the domain.
Beginning a Query
Now that we have a graph, we can start to query it. In Cypher we always begin our
queries from one or more well-known starting points in the graph—what are called
bound
nodes. (We can also start a query by binding to one or more relationships, but
this happens far less frequently than binding to nodes.) This is the purpose of the
START
clause: to give us some starting points, usually drawn from underlying indexes, for
exploring the rest of the graph.
For instance, if we wanted to discover more about performances at the Theatre Royal,
we'd use the Theatre Royal node as our starting point. However, if we were more inter‐
ested in a person's reviews, we'd use that person's node as a starting point instead.
Let's assume we want to find out about all the Shakespeare events that have taken place
in the Theatre Royal in Newcastle. These three things—Shakespeare, Theatre Royal, and
Newcastle—provide the starting points for our new query: