15.19. Create Unique

15.19.1. Create relationship if it is missing
15.19.2. Create node if missing
15.19.3. Create nodes with values
15.19.4. Create relationship with values
15.19.5. Describe complex pattern

CREATE UNIQUE is in the middle of MATCH and CREATE — it will match what it can, and create what is missing. CREATE UNIQUE will always make the least change possible to the graph — if it can use parts of the existing graph, it will.

Another difference to MATCH is that CREATE UNIQUE assumes the pattern to be unique. If multiple matching subgraphs are found an exception will be thrown.

[Tip]Tip

In the CREATE UNIQUE clause, patterns are used a lot. Read Section 15.8, “Patterns” for an introduction.

15.19.1. Create relationship if it is missing

CREATE UNIQUE is used to describe the pattern that should be found or created.

Query

START left=node(1), right=node(3,4)
CREATE UNIQUE left-[r:KNOWS]->right
RETURN r

The left node is matched agains the two right nodes. One relationship already exists and can be matched, and the other relationship is created before it is returned.

Result

r
2 rows
Relationships created: 1
1 ms

:KNOWS[4] {}

:KNOWS[3] {}


Try this query live. (1) {"name":"A"} (2) {"name":"root"} (3) {"name":"B"} (4) {"name":"C"} (1)-[:KNOWS]->(4) {} (1)-[:KNOWS]->(3) {} (2)-[:X]->(1) {} (2)-[:X]->(3) {} (2)-[:X]->(4) {} start left=node(1), right=node(3,4) create unique left-[r:KNOWS]->right return r

15.19.2. Create node if missing

If the pattern described needs a node, and it can’t be matched, a new node will be created.

Query

START root=node(2)
CREATE UNIQUE root-[:LOVES]-someone
RETURN someone

The root node doesn’t have any LOVES relationships, and so a node is created, and also a relationship to that node.

Result

someone
1 row
Nodes created: 1
Relationships created: 1
3 ms

Node[5]{}


Try this query live. (1) {"name":"A"} (2) {"name":"root"} (3) {"name":"B"} (4) {"name":"C"} (5) {} (1)-[:KNOWS]->(4) {} (2)-[:X]->(1) {} (2)-[:X]->(3) {} (2)-[:X]->(4) {} (5)-[:LOVES]->(2) {} start root=node(2) create unique root-[:LOVES]-someone return someone

15.19.3. Create nodes with values

The pattern described can also contain values on the node. These are given using the following syntax: prop : <expression>.

Query

START root=node(2)
CREATE UNIQUE root-[:X]-(leaf {name:'D'} )
RETURN leaf

No node connected with the root node has the name D, and so a new node is created to match the pattern.

Result

leaf
1 row
Nodes created: 1
Relationships created: 1
Properties set: 1
3 ms

Node[5]{name:"D"}


Try this query live. (1) {"name":"A"} (2) {"name":"root"} (3) {"name":"B"} (4) {"name":"C"} (5) {"name":"D"} (1)-[:KNOWS]->(4) {} (2)-[:X]->(1) {} (2)-[:X]->(3) {} (2)-[:X]->(4) {} (5)-[:X]->(2) {} start root=node(2) create unique root-[:X]-(leaf {name:'D'} ) return leaf

15.19.4. Create relationship with values

Relationships to be created can also be matched on values.

Query

START root=node(2)
CREATE UNIQUE root-[r:X {since:'forever'}]-()
RETURN r

In this example, we want the relationship to have a value, and since no such relationship can be found, a new node and relationship are created. Note that since we are not interested in the created node, we don’t name it.

Result

r
1 row
Nodes created: 1
Relationships created: 1
Properties set: 1
1 ms

:X[4] {since:"forever"}


Try this query live. (1) {"name":"A"} (2) {"name":"root"} (3) {"name":"B"} (4) {"name":"C"} (5) {} (1)-[:KNOWS]->(4) {} (2)-[:X]->(1) {} (2)-[:X]->(3) {} (2)-[:X]->(4) {} (5)-[:X]->(2) {"since":"forever"} start root=node(2) create unique root-[r:X {since:'forever'}]-() return r

15.19.5. Describe complex pattern

The pattern described by CREATE UNIQUE can be separated by commas, just like in MATCH and CREATE.

Query

START root=node(2)
CREATE UNIQUE root-[:FOO]->x, root-[:BAR]->x
RETURN x

This example pattern uses two paths, separated by a comma.

Result

x
1 row
Nodes created: 1
Relationships created: 2
2 ms

Node[5]{}


Try this query live. (1) {"name":"A"} (2) {"name":"root"} (3) {"name":"B"} (4) {"name":"C"} (5) {} (1)-[:KNOWS]->(4) {} (2)-[:X]->(1) {} (2)-[:X]->(3) {} (2)-[:X]->(4) {} (2)-[:FOO]->(5) {} (2)-[:BAR]->(5) {} start root=node(2) create unique root-[:FOO]->x, root-[:BAR]->x return x