CREATE clause

The CREATE clause is used to create nodes and relationships in a graph. Additionally CREATE is also used for additional database definitions, such as enums.

Indexing can increase performance when executing queries. Please take a look at our documentation on indexing for more details.

  1. Creating nodes
    1.1. Creating a single node
    1.2. Creating a node with properties
    1.3. Creating multiple nodes
    1.4. Creating node labels dynamically
  2. Creating relationships
    2.1. Creating a relationship between two nodes
    2.2. Creating a relationship with properties
    2.3. Creating relationship types dynamically
  3. Creating a path
  4. Creating an enum

1. Creating nodes

1.1. Creating a single node

Use the following query to create a single node. The RETURN clause is used to return results. A newly created node can be returned in the same query.

CREATE (n)
RETURN n;

Output:

+----+
| n  |
+----+
| () |
+----+

You can also specify a label while creating a node:

CREATE (n:Country)
RETURN n;

Output:

+------------+
| n          |
+------------+
| (:Country) |
+------------+

If you wish to add multiple labels to a node, use the following syntax:

CREATE (n:Country:City)
RETURN n;

Output:

+-----------------+
| n               |
+-----------------+
| (:Country:City) |
+-----------------+

1.2. Creating a node with properties

A node can be created with initial properties.

CREATE (n:Country {name: 'San Marino', continent: 'Europe'})
RETURN n;

Output:

+------------------------------------------------------+
| n                                                    |
+------------------------------------------------------+
| (:Country {continent: "Europe", name: "San Marino"}) |
+------------------------------------------------------+

1.3. Creating multiple nodes

To create multiple nodes, separate them with a comma.

CREATE (n:Country), (m:City)
RETURN n,m;

Output:

+------------+------------+
| n          | m          |
+------------+------------+
| (:Country) | (:City)    |
+------------+------------+

1.4. Creating node labels dynamically

Node labels can be created dynamically from variable values. The functionality only works with CREATE. Matching and merging of dynamic node labels is not supported since query plan and scanning indices are created upfront.

WITH {label_value: "Label"} as x
CREATE (n:x.label_value) RETURN n;

Output:

+------------+
| n          |
+------------+
| (:Label)   |
+------------+

This functionality can especially be useful when importing data from CSV or other sources, since at that point you can inject the arbitrary labels into the graph.

2. Creating relationships

2.1. Creating a relationship between two nodes

To create a relationship between two nodes, we need to specify which nodes either by creating them or filtering them with the WHERE clause.

CREATE (c1:Country {name: 'Belgium'}), (c2:Country {name: 'Netherlands'})
CREATE (c1)-[r:BORDERS_WITH]->(c2)
RETURN r;

Output:

+----------------+
| r              |
+----------------+
| [BORDERS_WITH] |
+----------------+

If the nodes already exist, the query would look like this:

MATCH (c1:Country),(c2:Country)
WHERE c1.name = 'Belgium' AND c2.name = 'Netherlands'
CREATE (c1)-[r:NEIGHBOURS]->(c2)
RETURN r;

Output:

+--------------+
| r            |
+--------------+
| [NEIGHBOURS] |
+--------------+

2.2. Creating a relationship with properties

You can add properties to a relationship at the time of creation.

MATCH (c1:Country),(c2:Country)
WHERE c1.name = 'Belgium' AND c2.name = 'Netherlands'
CREATE (c1)-[r:BORDERS_WITH {length: '30KM'}]->(c2)
RETURN r;

Output:

+---------------------------------+
| r                               |
+---------------------------------+
| [BORDERS_WITH {length: "30KM"}] |
+---------------------------------+

2.3. Creating relationship types dynamically

Relationship types can be created dynamically from variable values. The functionality only works with CREATE. Matching and merging of dynamic relationship types is not supported since query plan and scanning indices are created upfront.

WITH {edge_type_value: "EDGE_TYPE"} as x
CREATE ()-[r:x.edge_type_value]->() RETURN r;

Output:

+--------------+
| n            |
+--------------+
| [:EDGE_TYPE] |
+--------------+

This functionality can especially be useful when importing data from CSV or other sources, since at that point you can inject the arbitrary edge types into the graph.

3. Creating a path

When creating a path all the entities of the pattern will be created.

CREATE p=((n:Country {name: 'Belgium'})-[r:BORDERS_WITH {length: '30KM'}]->(m:Country {name: 'Netherlands'}))
RETURN p;

Output:

+------------------------------------------------------------------------------------------------+
| p                                                                                              |
+------------------------------------------------------------------------------------------------+
| (:Country {name: "Belgium"})-[BORDERS_WITH {length: "30KM"}]->(:Country {name: "Netherlands"}) |
+------------------------------------------------------------------------------------------------+

4. Creating an enum

To use enums in queries (as values, properties, etc) they first need to be created.

CREATE ENUM Suit VALUES { Hearts, Diamonds, Clubs, Spades };

ALTER can be used to modify the enums afterwards.