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 MERGE clause ensures that a specified pattern exists in the graph — either by matching existing data or by creating it.
MERGE either matches an existing vertex or creates new data. It combines the behavior of MATCH and CREATE.
Note!
Restrictions onMERGEin YMatrix AGE:
ON MATCH SETandON CREATE SETsyntax is not supported.MERGE (n:node {name: 'Lisa'}) ON MATCH SET n.age = 23 -- ERROR: syntax error at or near "ON"Combining
MERGEwith other write operations (CREATE,SET) viaWITHraises an error.Distributed transaction errors occur when
MERGEis chained withCREATEorSETusingWITHto pass bound variables:-- Error CREATE (n) WITH n AS a MERGE (a)-[:e]->() -- ERROR: AssignTransactionId() called by Segment Reader process-- Error
CREATE (n {i : 1}) SET n.i = 2 MERGE ({i: 2})
-- ERROR: AssignTransactionId() called by Segment Reader process**Recommendation:** Split `MERGE` and other write operations into separate Cypher statements.
For example, you may specify that the graph must contain a user vertex with a particular name. If no vertex with that name exists, a new one is created and its name property is set. When MERGE is applied to a full pattern, the behavior is all-or-nothing: either the entire pattern matches, or the entire pattern is created. MERGE does not partially reuse existing patterns. To achieve partial matching, decompose the pattern into multiple MERGE clauses.
Like MATCH, MERGE can match multiple occurrences of a pattern. If multiple matches exist, all are passed to subsequent query stages.
SELECT * FROM cypher('graph_name', $$
CREATE (A:Person {name: "Charlie Sheen", bornIn: "New York"}),
(B:Person {name: "Michael Douglas", bornIn: "New Jersey"}),
(C:Person {name: "Rob Reiner", bornIn: "New York"}),
(D:Person {name: "Oliver Stone", bornIn: "New York"}),
(E:Person {name: "Martin Sheen", bornIn: "Ohio"})
$$) AS (result agtype);
To merge a vertex with a label, specify a pattern containing a single labeled vertex.
SELECT * FROM cypher('graph_name', $$
MERGE (v:Critic)
RETURN v
$$) AS (v agtype);
If a vertex labeled Critic exists, it is returned. Otherwise, a new Critic vertex is created and returned.
| v |
|---|
{id: 0; label: 'Critic'; properties: {}}::vertex |
| 1 row returned |
Merge a vertex where not all properties match an existing vertex.
SELECT * FROM cypher('graph_name', $$
MERGE (charlie {name: 'Charlie Sheen', age: 10})
RETURN charlie
$$) AS (v agtype);
If a vertex with all specified properties exists, it is returned. Otherwise, a new vertex named 'Charlie Sheen' is created and returned.
| v |
|---|
{id: 0; label: 'Actor'; properties: {name: 'Charlie Sheen', age: 10}}::vertex |
| 1 row returned |
Merge a vertex constrained by both label and properties that match an existing vertex.
SELECT * FROM cypher('graph_name', $$
MERGE (michael:Person {name: 'Michael Douglas'})
RETURN michael.name, michael.bornIn
$$) AS (Name agtype, BornIn agtype);
'Michael Douglas' matches the existing Person vertex, and the query returns its name and bornIn properties.
| Name | BornIn |
|---|---|
"Michael Douglas" |
"New Jersey" |
| 1 row returned |