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.
- Creating nodes
1.1. Creating a single node
1.2. Creating a node with properties
1.3. Creating multiple nodes - Creating relationships
2.1. Creating a relationship between two nodes
2.2. Creating a relationship with properties - Creating a path
- 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) |
+------------+------------+
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"}] |
+---------------------------------+
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.