Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between merge and create unique in Neo4j

I'm trying to figure out what is the difference between MERGE and CREATE UNIQUE. I know these features:

MERGE

I'm able to create node, if doesn't exist pattern.

    MERGE (n { name:"X" }) RETURN n;

This create node "n" with property name, empty node "m" and relationship RELATED.

    MERGE (n { name:"X" })-[:RELATED]->(m) RETURN n, m;

CREATE UNIQUE

I'm not able to create node like this.

    CREATE UNIQUE (n { name:"X" }) RETURN n;

If exists node "n", create unique makes empty node "m" and relationship RELATED.

    MATCH (n { name: 'X' }) CREATE UNIQUE (n)-[:RELATED]->(m) RETURN n, m;

If this pattern exists, nothing created, only returns pattern.

From my point of view, I see MERGE and CREATE UNIQUE are quite same queries, but with CREATE UNIQUE you can't create start node in relationship. I would be grateful, if someone could explain this issue and compare these queries, thx.

like image 998
EdWood Avatar asked Mar 31 '14 22:03

EdWood


People also ask

What is merge in Neo4j?

MERGE command is a combination of CREATE command and MATCH command. Neo4j CQL MERGE command searches for a given pattern in the graph. If it exists, then it returns the results. If it does NOT exist in the graph, then it creates a new node/relationship and returns the results.

What is unwind in Neo4j?

With UNWIND , you can transform any list back into individual rows.

Can a node have multiple labels Neo4j?

Neo4j CQL CREATE a Node Label We can say this Label name to a Relationship as "Relationship Type". We can use CQL CREATE command to create a single label to a Node or a Relationship and multiple labels to a Node. That means Neo4j supports only single Relationship Type between two nodes.

What is optional match in Neo4j?

An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.


1 Answers

CREATE UNIQUE has slightly more obscure semantics than MERGE. MERGE was developed as an alternative with more intuitive behavior than CREATE UNIQUE; if in doubt, MERGE is usually the right choice.

The easiest way to think of MERGE is as a MATCH-or-create. That is, if something in the database would MATCH the pattern you are using in MERGE, then MERGE will just return that pattern. If nothing matches, the MERGE will create all missing elements in the pattern, where a missing element means any unbound identifier.

Given

MATCH (a {uid:123}) MERGE (a)-[r:LIKES]->(b)-[:LIKES]->(c) 

"a" is a bound identifier from the perspective of the MERGE. This means cypher somehow already knows which node it represents.

This statement can have two outcomes. Either the whole pattern already exists, and nothing will be created, or parts of the pattern are missing, and a whole new set of relationships and nodes matching the pattern will be created.

Examples

// Before merge: (a)-[:LIKES]->()-[:LIKES]->()  // After merge: (a)-[:LIKES]->()-[:LIKES]->()   // Before merge: (a)-[:LIKES]->()-[:OWNS]->()  // After merge: (a)-[:LIKES]->()-[:OWNS]->() (a)-[:LIKES]->()-[:LIKES]->()   // Before merge: (a)  // After merge: (a)-[:LIKES]->()-[:LIKES]->() 
like image 98
Jacob Davis-Hansson Avatar answered Oct 21 '22 17:10

Jacob Davis-Hansson