As discussed, a node is a statistics/record in a graph database. You can create a node in Neo4j the use of the CREATE clause. This chapter teaches you the way to −
- Create a unmarried node
- Create more than one nodes
- Create a node with a label
- Create a node with more than one labels
- Create a node with residences
- Returning the created node
Creating a Single node
You can create a node in Neo4j by means of certainly specifying the name of the node that is to be created in conjunction with the CREATE clause.
Syntax
Following is the syntax for developing a node using Cypher Query Language.
CREATE (node_name);
Note − Semicolon (;) is non-obligatory.
Example
Following is a pattern Cypher Query which creates a node in Neo4j.
CREATE (sample)
To execute the above question, perform the subsequent steps −
Step 1 − Open the Neo4j computing device App and start the Neo4j Server as proven within the following screenshot.
Step 2 − Open your browser, replica paste the following URL on your deal with bar http://localhost:7474/. This will provide you with the built-in browser app of Neo4j with a greenback prompt as shown inside the following screenshot.
Step 3 − Copy and paste the favored query inside the greenback activate and press the play button (to execute the question) highlighted inside the following screenshot.
Result
On executing, you'll get the subsequent result.
Verification
To verify the creation of the node type, execute the following question in the dollar set off.
MATCH (n) RETURN n
This question returns all the nodes in the database (we can discuss this question in element inside the coming chapters).
On executing, this question suggests the created node as shown inside the following screenshot.
Creating Multiple Nodes
The create clause of Neo4j CQL is likewise used to create more than one nodes at the equal time. To achieve this, you need to pass the names of the nodes to be created, separated with the aid of a comma.
Syntax
Following is the syntax to create multiple nodes the use of the CREATE clause.
CREATE (node1),(node2)
Example
Following is a pattern Cypher Query which creates multiple nodes in Neo4j.
CREATE (sample1),(sample2)
To execute the above query, perform the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the integrated browser app of Neo4j using the URL http://localhost:7474/ as shown inside the following screenshot.
Step 2 − Copy and paste the favored query in the dollar set off and press the play button (to execute the question) highlighted inside the following screenshot.
Result
On executing, you'll get the following result.
Verification
To affirm the advent of the node, kind and execute the subsequent question within the dollar prompt.
MATCH (n) RETURN n
This question returns all the nodes within the database (we can discuss this question in element in the coming chapters).
On executing, this question indicates the created node as shown in the following screenshot.
Creating a Node with a Label
A label in Neo4j is used to organization (classify) the nodes the usage of labels. You can create a label for a node in Neo4j using the CREATE clause.
Syntax
Following is the syntax for growing a node with a label using Cypher Query Language.
CREATE (node:label)
Example
Following is a sample Cypher Query which creates a node with a label.
CREATE (Dhawan:player)
To execute the above query, carry out the subsequent steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the integrated browser app of Neo4j the use of the URL http://localhost:7474/ as shown within the following screenshot.
Step 2 − Copy and paste the desired question within the dollar set off and press the play button (to execute the query) highlighted within the following screenshot.
Result
On executing, you may get the following end result.
Verification
To verify the introduction of the node, type and execute the following query within the dollar spark off.
MATCH (n) RETURN n
This question returns all of the nodes in the database (we are able to discuss this question in element in the coming chapters).
On executing, this query suggests the created node as proven within the following screenshot.
Creating a Node with Multiple Labels
You can also create multiple labels for a single node. You need to specify the labels for the node by way of setting apart them with a colon “ : ”.
Syntax
Following is the syntax to create a node with more than one labels.
CREATE (node:label1:label2:. . . . labeln)
Example
Following is a pattern Cypher Query which creates a node with multiple labels in Neo4j.
CREATE (Dhawan:person:player)
To execute the above question, perform the subsequent steps −
Step 1 − Open the Neo4j computer App and start the Neo4j Server. Open the integrated browser app of Neo4j the usage of the URL http://localhost:7474/ as proven within the following screenshot.
Step 2 − Copy and paste the desired query within the dollar set off and press the play button (to execute the question) highlighted inside the following screenshot.
Result
On executing, you'll get the following end result.
Verification
To verify the introduction of the node, type and execute the subsequent query within the dollar prompt.
MATCH (n) RETURN n
This question returns all the nodes in the database (we can discuss this query in element in the coming chapters).
On executing, this query suggests the created node as proven within the following screenshot.
Create Node with Properties
Properties are the key-cost pairs using which a node stores information. You can create a node with houses using the CREATE clause. You want to specify those houses separated through commas within the flower braces “ ”.
Syntax
Following is the syntax to create a node with houses.
CREATE (node:label { key1: value, key2: value, . . . . . . . . . })
Example
Following is a pattern Cypher Query which creates a node with properties.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
To execute the above question, perform the subsequent steps −
Step 1 − Open the Neo4j computing device App and start the Neo4j Server. Open the built-in browser app of Neo4j the usage of the URL http://localhost:7474/ as shown within the following screenshot.
Step 2 − Copy and paste the favored query inside the dollar prompt and press the play button (to execute the question) highlighted inside the following screenshot.
Result
On executing, you may get the subsequent result.
Verification
To verify the creation of the node, kind and execute the subsequent query inside the dollar prompt.
MATCH (n) RETURN n
This query returns all of the nodes inside the database (we are able to speak this query in element within the coming chapters).
On executing, this question suggests the created node as proven in the following screenshot.
Returning the Created Node
Throughout the chapter, we used the MATCH (n) RETURN n query to view the created nodes. This query returns all the present nodes inside the database.
Instead of this, we are able to use the RETURN clause with CREATE to view the newly created node.
Syntax
Following is the syntax to go back a node in Neo4j.
CREATE (Node:Label{properties. . . . }) RETURN Node
Example
Following is a pattern Cypher Query which creates a node with residences and returns it.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"}) RETURN Dhawan
To execute the above question, perform the subsequent steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the integrated browser app of Neo4j using the URL http://localhost:7474/ as proven within the following screenshot.
Step 2 − Copy and paste the favored query within the dollar set off and press the play button (to execute the query) highlighted within the following screenshot.
Result
On executing, you will get the subsequent result.