# Creating and updating graphs

[Wiki](../README.md) / [Cypher query language](README.md)

**On this page**

- [CREATE](#create)
- [MERGE](#merge)
- [SET](#set)
- [REMOVE](#remove)
- [DELETE / DETACH DELETE](#delete--detach-delete)
- [FOREACH](#foreach)
- [Map and list-property updates](#map-and-list-property-updates)

## `CREATE`

Create nodes and relationships. Inline `{...}` **sets** properties.

```cypher
CREATE (:Person {name: 'Ada', age: 36});

CREATE (a:Person {name: 'Ada'})-[:KNOWS {since: 2015}]->(b:Person {name: 'Bob'});

// comma-separated patterns create in order, left to right; a later pattern
// may reuse a variable bound by an earlier one:
CREATE (a:Person {name: 'Eve'}), (b:Person {name: 'Fay'}), (a)-[:KNOWS]->(b);

// create relationships between already-matched nodes:
MATCH (a:Person {name: 'Ada'}), (b:Person {name: 'Cy'})
CREATE (a)-[:KNOWS]->(b);
```

## `MERGE`

Match the pattern if it exists, else create it — with optional `ON CREATE` /
`ON MATCH` side-effects.

```cypher
MERGE (u:User {id: 1});                      // get-or-create

MERGE (u:User {id: 1})
ON CREATE SET u.created = true
ON MATCH  SET u.seen = true;

// MERGE can extend already-bound variables:
MATCH (a:User {id: 1})
MERGE (a)-[:OWNS]->(b:Account {no: 'A-1'});
```

## `SET`

Set properties and add labels. Comma-separate multiple updates.

```cypher
MATCH (p:Person {name: 'Ada'})
SET p.seen = true, p.age = 37, p:VIP;        // two properties + a label
```

- Setting a property to `null` **removes** the key (equivalent to `REMOVE`):
  `SET p.nick = null`.
- `SET p:VIP` adds the `VIP` label (no-op if already present).

## `REMOVE`

Remove properties and labels.

```cypher
MATCH (p:Person {name: 'Ada'})
REMOVE p.seen, p:VIP;                          // remove a property and a label
```

## `DELETE` / `DETACH DELETE`

Delete relationships and nodes.

```cypher
MATCH (a)-[r:KNOWS]->(b) DELETE r;            // delete a relationship

MATCH (p:Person {name: 'Ada'}) DELETE p;      // error if p still has relationships
MATCH (p:Person {name: 'Ada'}) DETACH DELETE p;  // delete p and its relationships
```

- Plain `DELETE` of a node that still has relationships is an error — use
  `DETACH DELETE`.
- `DELETE` is **idempotent within a statement**: an entity bound by several rows
  is deleted once; a repeat is a no-op.

## `FOREACH`

Run a set of **updating** clauses once per element of a list, for each input row.
The loop variable is local to the `FOREACH`; no new binding escapes to the outer
query.

```cypher
// create three tag nodes
FOREACH (n IN [1, 2, 3] | CREATE (:Tag {v: n}));

// per matched person, attach two child nodes
MATCH (p:Person)
FOREACH (i IN [1, 2] | CREATE (p)-[:HAS]->(:Child {i: i}));
```

The body may contain `CREATE`, `MERGE`, `SET`, `REMOVE`, `DELETE` and nested
`FOREACH` — but not read clauses (`MATCH`/`WITH`/`RETURN`).

## Map and list-property updates

`SET n = map` replaces properties; `SET n += map` merges properties. Null values
remove keys. GDB also supports `SET n.values[index] = value`; an index beyond
the end extends the list with null padding, and an absent property becomes a list.
See [element indexes](../indexes/element.md) for querying staged list values.

```cypher
MATCH (p:Person {name:'Ada'}) SET p += {active:true};
MATCH (p:Person {name:'Ada'}) SET p.stages[2] = 'published';
```

## Related articles

[Indexes and constraints](../indexes/README.md) · [Procedure directory](../reference/procedures.md) · [Transactions and concurrency](../operations/transactions.md)
