Neo4j CQL Creating Nodes - Neo4j

How to create nodes using Neo4j CQL?

As discussed, a node is a data/record in a graph database which can be created in Neo4j using the CREATE clause. This chapter will explain how to

  • Create a single node
  • Create multiple nodes
  • Create a node with a label
  • Create a node with multiple labels
  • Create a node with properties
  • Return the created node

Creating a Single node

Node in Neo4j can be created by just specifying the name of the node which is created along with the CREATE clause.

Syntax

Below is the syntax for creating a node using Cypher Query Language.

Note − Semicolon (;) is optional.

Example

Below is a sample Cypher Query which creates a node in Neo4j.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server as shown in below screenshot.

start_button

Step 2 − Open your browser, copy paste below URL in your address bar http://localhost:7474/. This gives you the built-in browser app of Neo4j with a dollar prompt as shown in below screenshot.

browser_app

Step 3 − Copy and paste the required query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

dollar_prompt

Result

On executing, you will get below result.

single_node

Verification

To verify the creation of the node type, execute below query in the dollar prompt.

This query will return all the nodes in the database (we will explain this query in detail in the next chapters).

On executing, this query shows the created node as shown in below screenshot.

nodes_in_database

Creating Multiple Nodes

Create clause of Neo4j CQL is used for creating multiple nodes at the same time. For this, pass the names of the nodes to be created, separated by a comma.

Syntax

Below is the syntax for creating multiple nodes using the CREATE clause.

Example

Below is a sample Cypher Query which creates multiple nodes in Neo4j.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser_app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

desired_query

Result

multiple_node

Verification

To verify the creation of the node, type and execute below query in the dollar prompt.

This query will return all the nodes in the database (we will explain this query in detail in the next chapters).

On executing, this query will display the created node as shown in below screenshot.

multiple_verification

Creating a Node with a Label

A label in Neo4j is used for grouping (classify) the nodes using labels. Label can be created for a node in Neo4j using CREATE clause.

Syntax

Below is the syntax for creating a node with a label using Cypher Query Language.

Example

Below is a sample Cypher Query which creates a node with a label.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser_app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

node_label

Result

node_statement

Verification

To verify the creation of the node, type and execute below query in the dollar prompt.

This query returns all the nodes in the database (we will explain this query in detail in the next chapters).

On executing, this query will display the created node as shown in below screenshot.

label_verification

Creating a Node with Multiple Labels

You can also create multiple labels for a single node. You have to specify the labels for the node by separating them with a colon “ : ”.

Syntax

Below is the syntax to create a node with multiple labels.

Example

Below is a sample Cypher Query which creates a node with multiple labels in Neo4j.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser_app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

multiple_label

Result

On executing, you will get below result.

executed_query

Verification

To verify the creation of the node, type and execute below query in the dollar prompt.

This query returns all the nodes in the database (we will discuss this query in detail in the coming chapters).

On executing, this query will display the created node as shown in below screenshot.

multi_label_verification

Create Node with Properties

Properties are the key-value pairs using which a node stores data. You can create a node with properties using the CREATE clause. You need to specify these properties separated by commas within the flower braces “{ }”.

Syntax

Below is the syntax to create a node with properties.

Example

Below is a sample Cypher Query which creates a node with properties.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser_app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

property_step

Result

label_result

Verification

To verify the creation of the node, type and execute below query in the dollar prompt.

This query will return all the nodes in the database (we will explain this query in detail in the next chapters).

On executing, this query will display the created node as shown in below screenshot.

creation_node

Returning the Created Node

Throughout the chapter, we have used MATCH (n) RETURN n query to view the created nodes. This query will return all the existing nodes in the database.

Instead of this, we can use the RETURN clause with CREATE to view the newly created node.

Syntax

Below is the syntax to return a node in Neo4j.

Example

Below is a sample Cypher Query which creates a node with properties and returns it.

To execute the above query, carry out below steps

Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in below screenshot.

browser_app

Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in below screenshot.

play_button

Result

return_node

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Neo4j Topics