Neo4j - Create a Node using Cypher
To create nodes and relationships using Cypher, use the CREATE
statement.
The statement consists of CREATE
, followed by the details of the node or relationship that you're creating.
Example
Let's create a music database that contains band names and their albums.
The first band will be called Strapping Young Lad. So we will create an Artist node and call it Strapping Young Lad.
Here's the Cypher CREATE
statement to create the above node:
This Cypher statement creates a node with an Artist label. The node has a property called Name, and the value of that property is Strapping Young Lad.
The a
prefix is a variable name that we provide. We could've called this anything. This variable can be useful if we need to refer to it later in the statement (which we don't in this particular case). Note that a variable is restricted to a single statement.
So go ahead and run the above statement in the Neo4j browser. The statement will create the node.
Displaying the Node
The CREATE
statement creates the node but it doesn't display the node.
To display the node, you need to follow it up with a RETURN
statement.
Let's create another node. This time it will be the name of an album. But this time we'll follow it up with a RETURN
statement.
The above statement creates a node with an Album label. It has two properties: Name and Released.
Note that we return the node by using its variable name (in this case b
).
Creating Multiple Nodes
You can create multiple nodes at once by separating each node with a comma:
Or you can use multiple CREATE
statements:
Next, we will create a relationship between some of our nodes.