11.1. Create

Creating graph elements — nodes and relationships, is done with CREATE.

[Tip]Tip

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

Create nodes

Create single node

Creating a single node is done by issuing the following query.

Query 

CREATE (n)

Nothing is returned from this query, except the count of affected nodes.

Result

Nodes created: 1

(empty result)

Try this query live none create (n)

Create a node with a label

To add a label when creating a node, use the syntax below.

Query 

CREATE (n:Person)

Nothing is returned from this query.

Result

Nodes created: 1
Labels added: 1

(empty result)

Try this query live none create (n:Person)

Create a node with multiple labels

To add labels when creating a node, use the syntax below. In this case, we add two labels.

Query 

CREATE (n:Person:Swedish)

Nothing is returned from this query.

Result

Nodes created: 1
Labels added: 2

(empty result)

Try this query live none create (n:Person:Swedish)

Create node and add labels and properties

When creating a new node with labels, you can add properties at the same time.

Query 

CREATE (n:Person { name : 'Andres', title : 'Developer' })

Nothing is returned from this query.

Result

Nodes created: 1
Properties set: 2
Labels added: 1

(empty result)

Try this query live none create (n:Person {name : 'Andres', title : 'Developer'})

Return created node

Creating a single node is done by issuing the following query.

Query 

CREATE (a { name : 'Andres' })
RETURN a

The newly created node is returned.

Result

a
1 row
Nodes created: 1
Properties set: 1

Node[0]{name:"Andres"}

Try this query live none create (a {name : 'Andres'}) return a

Create relationships

Create a relationship between two nodes

To create a relationship between two nodes, we first get the two nodes. Once the nodes are loaded, we simply create a relationship between them.

Query 

MATCH (a:Person),(b:Person)
WHERE a.name = 'Node A' AND b.name = 'Node B'
CREATE (a)-[r:RELTYPE]->(b)
RETURN r

The created relationship is returned by the query.

Result

r
1 row
Relationships created: 1

:RELTYPE[0]{}

Try this query live create (_0:`Person` {`name`:"Node A"}) create (_1:`Person` {`name`:"Node B"}) match (a:Person), (b:Person) where a.name = 'Node A' and b.name = 'Node B' create (a)-[r:RELTYPE]->(b) return r

Create a relationship and set properties

Setting properties on relationships is done in a similar manner to how it’s done when creating nodes. Note that the values can be any expression.

Query 

MATCH (a:Person),(b:Person)
WHERE a.name = 'Node A' AND b.name = 'Node B'
CREATE (a)-[r:RELTYPE { name : a.name + '<->' + b.name }]->(b)
RETURN r

The newly created relationship is returned by the example query.

Result

r
1 row
Relationships created: 1
Properties set: 1

:RELTYPE[0]{name:"Node A<->Node B"}

Try this query live create (_0:`Person` {`name`:"Node A"}) create (_1:`Person` {`name`:"Node B"}) match (a:Person), (b:Person) where a.name = 'Node A' and b.name = 'Node B' create (a)-[r:RELTYPE {name : a.name + '<->' + b.name }]->(b) return r

Create a full path

When you use CREATE and a pattern, all parts of the pattern that are not already in scope at this time will be created.

Query 

CREATE p =(andres { name:'Andres' })-[:WORKS_AT]->(neo)<-[:WORKS_AT]-(michael { name:'Michael' })
RETURN p

This query creates three nodes and two relationships in one go, assigns it to a path identifier, and returns it.

Result

p
1 row
Nodes created: 3
Relationships created: 2
Properties set: 2

[Node[0]{name:"Andres"},:WORKS_AT[0]{},Node[1]{},:WORKS_AT[1]{},Node[2]{name:"Michael"}]

Try this query live none create p = (andres {name:'Andres'})-[:WORKS_AT]->(neo)<-[:WORKS_AT]-(michael {name:'Michael'}) return p

Use parameters with CREATE

Create node with a parameter for the properties

You can also create a graph entity from a map. All the key/value pairs in the map will be set as properties on the created relationship or node. In this case we add a Person label to the node as well.

Parameters 

{
  "props" : {
    "name" : "Andres",
    "position" : "Developer"
  }
}

Query 

CREATE (n:Person { props })
RETURN n

Result

n
1 row
Nodes created: 1
Properties set: 2
Labels added: 1

Node[0]{name:"Andres",position:"Developer"}

Create multiple nodes with a parameter for their properties

By providing Cypher an array of maps, it will create a node for each map.

[Note]Note

When you do this, you can’t create anything else in the same CREATE clause.

Parameters 

{
  "props" : [ {
    "name" : "Andres",
    "position" : "Developer"
  }, {
    "name" : "Michael",
    "position" : "Developer"
  } ]
}

Query 

CREATE (n { props })
RETURN n

Result

n
2 rows
Nodes created: 2
Properties set: 4

Node[0]{name:"Andres",position:"Developer"}

Node[1]{name:"Michael",position:"Developer"}