The CREATE clause is used to create graph vertices and edges.

Terminal CREATE clauses

A CREATE clause that is not followed by another clause is called a terminal clause. When a cypher query ends with a terminal clause, no results will be returned from the cypher function call. However, the cypher function call still requires a column list definition. When cypher ends with a terminal node, define a single value in the column list definition: no data will be returned in this variable.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. CREATE /* Create clause here, no following clause */
  4. $$) as (a agtype);
a
0 row(s) returned

Create single vertex

Creating a single vertex is done by issuing the following query.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. CREATE (n)
  4. $$) as (v agtype);

Nothing is returned from this query.

v
(0 rows)

Create multiple vertices

Creating multiple vertices is done by separating them with a comma.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. CREATE (n), (m)
  4. $$) as (v agtype);

Result

a
0 row(s) returned

Create a vertex with a label

To add a label when creating a vertex, use the following syntax:

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. CREATE (:Person)
  4. $$) as (v agtype);

Nothing is returned from this query.

Result

v
0 row(s) returned

Create a vertex with labels and properties

You can create a vertex with labels and properties at the same time.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. CREATE (:Person {name: 'Andres', title: 'Developer'})
  4. $$) as (n agtype);

Nothing is returned from this query.

Result

n
(0 rows)

Return created node

You can create and return a node within the same query as follows:

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. CREATE (a {name: 'Andres'})
  4. RETURN a
  5. $$) as (a agtype);

The newly-created node is returned.

Result

a
{id: 0; label: ‘’; properties: {name: ‘Andres’}}::vertex
(1 row)

Create an edge between two nodes

To create an edge between two vertices, we first MATCH the two vertices. Once the nodes are matched, we create an edge between them.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (a:Person), (b:Person)
  4. WHERE a.name = 'Node A' AND b.name = 'Node B'
  5. CREATE (a)-[e:RELTYPE]->(b)
  6. RETURN e
  7. $$) as (e agtype);

The created edge is returned by the query.

Result

e
{id: 3; startid: 0, endid: 1; label: ‘RELTYPE’; properties: {}}::edge
(1 row)

Create an edge and set properties

Setting properties on edges is done in a similar manner to setting properties when creating vertices. Note that the values can be any expression.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. MATCH (a:Person), (b:Person)
  4. WHERE a.name = 'Node A' AND b.name = 'Node B'
  5. CREATE (a)-[e:RELTYPE {name:a.name + '<->' + b.name}]->(b)
  6. RETURN e
  7. $$) as (e agtype);

The newly-created edge is returned by the example query.

Result

e
{id: 3; startid: 0, endid: 1; label: ‘RELTYPE’; properties: {name: ‘Node A<->Node B’}}::edge
(1 row)

Create a full path

When you use CREATE and a pattern, all parts of the patterns that are not already in scope at this time will be created.

Query

  1. SELECT *
  2. FROM cypher('graph_name', $$
  3. CREATE p = (andres {name:'Andres'})-[:WORKS_AT]->(neo)<-[:WORKS_AT]-(michael {name:'Michael'})
  4. RETURN p
  5. $$) as (p agtype);

This query creates three nodes and two relationships simultaneously, assigns the pattern to a path variable, and returns said pattern.

Result

p
[{id:0; label: ‘’; properties:{name:’Andres’}}::vertex,
{id: 3; startid: 0, endid: 1; label: ‘WORKS_AT’; properties: {}}::edge,
{id:1; label: ‘’; properties: {}}::vertex,
{id: 3; startid: 2, endid: 1; label: ‘WORKS_AT’; properties: {}}::edge,
{id:2; label: ‘’; properties: {name:’Michael’}}::vertex]::path
(1 row)