CREATE

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

Note!
In YMatrix AGE, all vertices and edges created by CREATE clauses within a single cypher() function call are placed on the same segment.

Terminal CREATE Clauses

A CREATE clause that is not followed by any other clause is called a terminal clause. When a Cypher query ends with a terminal clause, the cypher() function returns no rows. However, the function call still requires a column definition list — for example, AS (a agtype). When the query ends with a terminal clause, you may define any dummy column name; it will not contain data.

Query

SELECT *
FROM cypher('graph_name', $$
    CREATE /* This is a terminal CREATE clause with no subsequent clauses */
$$) AS (a agtype);

Result

a
Returns 0 rows

Creating a Single Vertex

Use the following query to create a single vertex.

Query

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

This query returns no data.

v
(0 rows)

Creating Multiple Vertices

Separate multiple vertex patterns with commas.

Query

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

Result

a
Returns 0 rows

Creating a Labeled Vertex

Add labels to a vertex using the syntax :Label.

Query

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

This query returns no data.

Result

v
Returns 0 rows

Creating a Labeled Vertex with Properties

You may create a labeled vertex and assign properties in the same CREATE clause.

Query

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

This query returns no data.

Result

n
(0 rows)

Returning Created Nodes

You can both create and return nodes in the same query using RETURN.

Query

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

Returns the newly created node.

Result

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

Creating Self-Loops

To create an edge where the start and end vertices are the same (startid = endid), use either of the following approaches:

  • Create the self-loop directly in CREATE.
  • First MATCH the vertex, then CREATE the edge.

Query (Direct Creation)

SELECT *
FROM cypher('graph_name', $$
    CREATE (a)-[e:RELTYPE]->(a)
    RETURN e
$$) AS (e agtype);

Result

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

Query (Using MATCH First)

SELECT *
FROM cypher('graph_name', $$
    MATCH (a:Person)
    CREATE (a)-[e:RELTYPE]->(a)
    RETURN e
$$) AS (e agtype);

Result

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

Creating an Edge Between Two Nodes

To create an edge between two existing vertices, first MATCH both vertices, then CREATE the edge.

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);

Returns the newly created edge.

Result

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

Creating an Edge with Properties

Edge properties are specified similarly to vertex properties. Property values may be arbitrary expressions.

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);

Returns the newly created edge.

Result

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

Creating a Full Path

When CREATE is used with a pattern, all parts of the pattern that do not already exist in the current scope are 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 vertices and two edges, binds the full path to 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)