YMatrix
Quick Start
Connecting
Benchmarks
Deployment
Data Usage
Manage Clusters
Upgrade
Global Maintenance
Expansion
Monitoring
Security
Best Practice
Technical Principles
Data Type
Storage Engine
Execution Engine
Streaming Engine(Domino)
MARS3 Index
Extension
Advanced Features
Advanced Query
Federal Query
Grafana
Backup and Restore
Disaster Recovery
Graph Database
Introduction
Clauses
Functions
Advanced
Guide
Performance Tuning
Troubleshooting
Tools
Configuration Parameters
SQL Reference
The CREATE clause is used to create vertices and edges in a graph.
Note!
In YMatrix AGE, all vertices and edges created byCREATEclauses within a singlecypher()function call are placed on the same segment.
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.
SELECT *
FROM cypher('graph_name', $$
CREATE /* This is a terminal CREATE clause with no subsequent clauses */
$$) AS (a agtype);
| a |
|---|
| Returns 0 rows |
Use the following query to create a single vertex.
SELECT *
FROM cypher('graph_name', $$
CREATE (n)
$$) AS (v agtype);
This query returns no data.
| v |
|---|
| (0 rows) |
Separate multiple vertex patterns with commas.
SELECT *
FROM cypher('graph_name', $$
CREATE (n), (m)
$$) AS (v agtype);
| a |
|---|
| Returns 0 rows |
Add labels to a vertex using the syntax :Label.
SELECT *
FROM cypher('graph_name', $$
CREATE (:Person)
$$) AS (v agtype);
This query returns no data.
| v |
|---|
| Returns 0 rows |
You may create a labeled vertex and assign properties in the same CREATE clause.
SELECT *
FROM cypher('graph_name', $$
CREATE (:Person {name: 'Andres', title: 'Developer'})
$$) AS (n agtype);
This query returns no data.
| n |
|---|
| (0 rows) |
You can both create and return nodes in the same query using RETURN.
SELECT *
FROM cypher('graph_name', $$
CREATE (a {name: 'Andres'})
RETURN a
$$) AS (a agtype);
Returns the newly created node.
| a |
|---|
{id: 0; label: ''; properties: {name: 'Andres'}}::vertex |
| (1 row) |
To create an edge where the start and end vertices are the same (startid = endid), use either of the following approaches:
CREATE.MATCH the vertex, then CREATE the edge.SELECT *
FROM cypher('graph_name', $$
CREATE (a)-[e:RELTYPE]->(a)
RETURN e
$$) AS (e agtype);
| e |
|---|
{id: 0; startid: 0, endid: 0; label: 'RELTYPE'; properties: {}}::edge |
| (1 row) |
SELECT *
FROM cypher('graph_name', $$
MATCH (a:Person)
CREATE (a)-[e:RELTYPE]->(a)
RETURN e
$$) AS (e agtype);
| e |
|---|
{id: 0; startid: 0, endid: 0; label: 'RELTYPE'; properties: {}}::edge |
| (1 row) |
To create an edge between two existing vertices, first MATCH both vertices, then CREATE the edge.
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.
| e |
|---|
{id: 3; startid: 0, endid: 1; label: 'RELTYPE'; properties: {}}::edge |
| (1 row) |
Edge properties are specified similarly to vertex properties. Property values may be arbitrary expressions.
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.
| e |
|---|
{id: 3; startid: 0, endid: 1; label: 'RELTYPE'; properties: {name: 'Node A<->Node B'}}::edge |
| (1 row) |
When CREATE is used with a pattern, all parts of the pattern that do not already exist in the current scope are created.
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.
| 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) |