Neo4j Create Nodes
Node is a data or record in a graph database. In Neo4j, the CREATE statement is used to create a node. You can create the following things by using CREATE statement:
- Create a single node
- Create multiple nodes
- Create a node with a label
- Create a node with multiple labels
- Create a node with properties
- Returning the created node
Create a Single Node
To create a single node in Neo4j, specify the name of the node along with CREATE statement.
Syntax:
Note: You can add or ignore semicolon (;). It is optional.
Example1:
Open the localhost on the browser: http://localhost:7474/browser/ and use the following code:
Output:
You can see that a node is created.
Verification
Execute the following code to verify the creation of the node type:
Output:
Create Multiple Nodes
To create multiple nodes in Neo4j, use CREATE statement with the name of nodes separated by a comma.
Syntax:
Example2:
Let’s create 2 nodes: primary_node and secondary_node.
Verification
Output:
Note: It is displaying 3 nodes because we have created a node already in example1.
Create a node with a label
In Neo4j, a label is used to classify the nodes using labels. CREATE statement is used to create a label for a node in Neo4j.
Syntax:
Example3:
Let’s create a node “Kalam” with a label “scientist”.
Output:
Verification
Output:
Create a Node with Multiple Labels
To create multiple labels with a single node, you have to specify the labels for the node by separating them with a colon ” : “.
Syntax:
Example:
Create a node “Kalam” with label “person”, “president”, and “scientist”.
Output:
Verification
Create Node with Properties
In Neo4j, properties are the key-value pairs which are used by nodes to store data. CREATE statement is used to create node with properties, you just have to specify these properties separated by commas within the curly braces “{ }”.
Syntax:
Example:
Let’s create a node “Ajeet”, having the following properties:
Output:
Verification
Returning the created node
MATCH (n) RETURN n command is used to view the created nodes. This query returns all the existing nodes in the database.
But if you want to return the newly created node use the RETURN command with CREATE command:
Syntax:
Example:
Create a node “tutor” with following properties and return that node.
Output: