CREATE

Updated at:

Use the CREATE clause to create vertices and edges in a graph.

Prepare test data

Before running the examples below, create a graph:

SELECT create_graph('graph_name');

Terminal clauses

A CREATE clause that is not followed by any other clause is called a terminal clause. A terminal clause returns no data, but the cypher() function still requires a column list. Define a single dummy column in the column list — no data is returned in that variable.

SELECT *
FROM cypher('graph_name', $$
    CREATE /* your CREATE pattern here */
$$) as (a agtype);

Result:

 a
---
(0 rows)

Create vertices

Create a single vertex

SELECT *
FROM cypher('graph_name', $$
    CREATE (n)
$$) as (v agtype);

Result:

 v
---
(0 rows)

To create multiple vertices at once, separate them with commas:

SELECT *
FROM cypher('graph_name', $$
    CREATE (n), (m)
$$) as (v agtype);

Result:

 v
---
(0 rows)

Add labels and properties

Attach a label when creating a vertex:

SELECT *
FROM cypher('graph_name', $$
    CREATE (:Person)
$$) as (v agtype);

Result:

 v
---
(0 rows)

Add properties at the same time:

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

Result:

 n
---
(0 rows)

Return the created vertex

Add a RETURN clause to retrieve the vertex in the same query:

SELECT *
FROM cypher('graph_name', $$
    CREATE (a {name: 'Andres'})
    RETURN a
$$) as (a agtype);

Result:

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

Create edges

Edges connect two vertices. To create an edge between existing vertices, first MATCH them to bind them to variables, then CREATE the edge. The MATCH step is required because CREATE only creates graph elements that are not already in scope — without it, new disconnected vertices would be created instead of an edge between the existing ones.

Create an edge between two existing vertices

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

Result:

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

Set properties on an edge

Set properties on an edge the same way as on a vertex — as a map literal inside the relationship pattern. Property values can be any expression, including references to matched vertex properties:

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

Result:

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

Create a full path

When CREATE is used with a multi-element pattern, every element in the pattern that is not already bound is created. This lets you create multiple vertices and edges in a single query.

Assign the pattern to a path variable to return the entire path:

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

This query creates three vertices and two edges simultaneously, assigns the pattern to the path variable p, and returns it.

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)