Create (clause)
Not to be confused with the CREATE
statement from the DDL, the CREATE
clause in openCypher is
similar to the INSERT
clause of SQL and lets you insert records into your node and relationship
tables. We describe the generic semantics of the CREATE
clause below.
We will use the example database for demonstration, whose schema and data import commands are given here.
Insert new nodes
The following query inserts a single node record, (Alice, 35)
, into the User
node table:
The properties to be set are specified using a mapping: {prop1 : val1, prop2 : val2, ...}
.
If you queried the database now for a User
node with name Alice
, you would get the following result:
Any node property which is not specified in the mapping but which exists in the schema, will be set to NULL. For example the following query will set the age property the inserted node record to NULL.
Insert new relationships
You can insert records to your relationship tables by
first binding two variables s
and t
to nodes, and then
“drawing” a relationship pattern between s
and t
.
For example, the following creates a Follows relationship
from the User node with name Adam
to the User node with
name Noura
.
Similar to inserting node records, any relationship property which is not specified in the query will be set to NULL.
General semantics
The general semantics of CREATE
is described here. You can specify
an arbitrary graph pattern P
after the CREATE
clause.
Then, for each tuple t
that was produced before the CREATE
statement,
each node n
and relationship r
that is not bound by t
is inserted
as a new node and relationship. For example, the following query
adds a Follows
relationship with since=2022
from User node Zhang
to every other User node (including from Zhang
to Zhang
) in the database:
This is because the a
variable matches to User node Zhang
and the b
variable matches to any
node in the User
table. As a result, this query creates a Follows
relationship from the User
node
Zhang
to every other User
node.