CREATE
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
SELECT *
FROM cypher('graph_name', $$
CREATE /* Create clause here, no following clause */
$$) as (a agtype);
a |
0 row(s) returned |
Create single vertex
Creating a single vertex is done by issuing the following query.
Query
SELECT *
FROM cypher('graph_name', $$
CREATE (n)
$$) 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
SELECT *
FROM cypher('graph_name', $$
CREATE (n), (m)
$$) 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 syntax below.
Query
SELECT *
FROM cypher('graph_name', $$
CREATE (:Person)
$$) as (v agtype);
Nothing is returned from this query.
Result
v |
0 row(s) returned |
Create vertex and add labels and properties
When creating a new vertex with labels, you can add properties at the same time.
Query
SELECT *
FROM cypher('graph_name', $$
CREATE (:Person {name: 'Andres', title: 'Developer')
$$) as (n agtype);
Nothing is returned from this query.
Result
n |
(0 rows) |
Return created node
Creating a single node is done by issuing the following query.
Query
SELECT *
FROM cypher('graph_name', $$
CREATE (a {name: 'Andres')
RETURN a
$$) 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 get the two vertices. Once the nodes are loaded, we simply create an edge between them.
Query
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);
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 how it’s done when creating vertices. Note that the values can be any expression.
Query
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);
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
v1 When you use CREATE and a pattern, all parts of the pattern that are not already in scope at this time will be created.
Query
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 nodes and two relationships in one go, assigns it to a path variable, 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) |