All Products
Search
Document Center

PolarDB:CREATE

Last Updated:Feb 27, 2025

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

Terminal REMOVE clauses

A CREATE clause that is not followed by other clauses is called a terminal clause. When a Cypher query ends with a terminal clause, no results are 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 is returned in this variable.

Example

SELECT *
FROM cypher('graph_name', $$
    CREATE /* Create a clause here with no following clauses * /
$$) as (a agtype);

Sample result:

 a 
---
(0 rows)

Prepare test data

SELECT create_graph('graph_name');

Create a single vertex

You can execute the following query to create a single vertex:

Example

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

This query does not return any result.

 v 
---
(0 rows)

Create multiple vertices

Separate multiple vertices by using commas when you create them.

Example

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

Sample result:

 v 
---
(0 rows)

Create a vertex with labels

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

Example

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

This query does not return any result.

 v 
---
(0 rows)

Create a vertex with labels and properties

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

Example

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

This query does not return any result.

 n 
---
(0 rows)

Return the created node

You can create and return a node in the same query:

Example

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

The created node is returned.

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

Create an edge between two nodes

To create an edge between two vertices, you need to MATCH the two vertices. After the nodes are matched, we create an edge between them.

Example

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.

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

Create an edge and set properties

The method of setting properties on edges is a similar to that of setting properties when creating vertices. Note that the values can be any expression.

Example

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 created edge is returned.

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

Create a full path

When using CREATE and a pattern, all parts of the patterns that are not already in scope are created.

Example

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 simultaneously, assigns the pattern to a path variable, and returns the pattern.

                                             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)