Monday, 19 July 2021

Neo4j: Create nodes

In this post, you are going to learn how to create node in different ways.

 

Create Single Node

Using CREATE clause, you can create a node.

 

Syntax

CREATE (node_name)

 

Example

CREATE(my_node1)

 

Follow below steps to execute above query.

 

Step 1: Open neo4j browser by hitting following url in browser.

http://localhost:7474/browser/

 


Step 2: Execute following query in $ prompt.

CREATE(my_node1)


You can execute the query simply by hitting enter button or click on Play button available at right side of $ prompt.



Once you execute the query, you can see a message like ‘Created 1 node, completed after n ms’ .

 

Validate the node creation

Execute following query at $ prompt.

MATCH (n) RETURN n

 

This query return all the nodes from neo4j database.

 


As you see above image, it is confirmed that 1 node is created.

 

Create multiple nodes in same query

Using CREATE clause, you can create multiple nodes.

 

Syntax

CREATE (node_name_1), (node_name_2) .... (node_name_N)

 

Example

CREATE(my_node2), (my_node3)

 


You can confirm the same by executing MATCH query.

 


Create a node with label

You can create a node with some label.

 

Syntax

CREATE (node_name:label)

 

Example

CREATE (my_node4:demoNode4)

 


You can confirm the same by executing MATCH query.

 


Create a node with multiple labels

Syntax

CREATE (node_name:label1:label2:. . . . labelN)

 

Example

CREATE (my_nbode5:demoNode5:exampleNode5:helloNode5)

 


You can confirm the same by executing MATCH query.

 


Create a node with properties

Syntax

CREATE (node_name:label { key1: value, key2: value, . . . . . . . . .  })

 

Example

CREATE (Ram:Person{name: "Rama Krishna", age: 32, city: "Bangalore"})


You can confirm the same by executing MATCH query.

 


Return the created node after creation

Syntax

CREATE (node_name:label{properties. . . . }) RETURN node_name

 

Example

CREATE (Sailaja:Person{name: "Sailaja", age: 32, city: "Hyderabad", female: true}) RETURN Sailaja

 


Execute below command to delete all the nodes.

MATCH (n) DETACH DELETE (n)

 


 

You can confirm the same by retrieving all the nodes.



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment