We have to use CREATE statement to create Nodes and relationships between Nodes, The CREAETE statement consists of the CREATE Keyword followed by Node and relationships details.
Nodes are specified by () (paranthesis) Let's Create a simple node and return it, we use variableName in the query just to use the created node again in the query, variableName will be destroyed after the query execution.
CREATE (variableName) RETURN variableName
Output :
In above CQL query we have not mentioned any label for the Node. Let's write the CREATE command with label in CQL.
CREATE (variableName:karthiQ) RETURN variableName
Output :
In above image you can find the label in blue color. Similarly we can create multiple labels for a Node by seperating them with
The id shown in the above image wil be different for every nodes based on their creation time, if we delete a node the id of that deleted node may be given to some different node in future.
Properties of a Node defines the Node attributes, properties important things of a Node. We have to mention or write the properties of a Node inside the Curly{} braces.
Let's Create a Node with Lables and properties (height and weight).
CREATE (variableName:karthiQ:StupidGuy{height:'164 cm', weight:'67 kg'})
RETURN variableName
Output :
We can create multiple Nodes in a single query by seperating the nodes with ',' (comma).
CREATE (person:ElonMusk), (firm:Tesla) // creates two Nodes
RETURN person, firm
Output :
I used RETURN command as I wanted to show you the Node created, we will discuss about RETURN command in later part of the Neo4j tutorials.
We can create relationships between Nodes, We have to use
For example : we can say 'Elon Must owns Tesla' but we cannot say 'Tesla owns Elon Musk'. In this example Elon Musk, Tesla are Nodes and 'owns' is a relationship. So we can write like Elon Musk -[:relationshipName-> Tesla.
Below example shows how to create a new Nodes and relationship between new Nodes.
CREATE (variableName:ElonMusk) -[:owns]-> (firm:Tesla)
RETURN variableName, firm
Output :
We can also create relationship between already existing nodes
Lets create two new nodes without any relationship.
CREATE (author:karthiQ), (site:CherCherTech) // creates two Nodes
Now lets create the relationship between two Nodes which are already existing in the Graph database.
MATCH(author:karthiQ), (site:CherCherTech)
CREATE(author)<-[:ownedBy]-(site)
RETURN author, site
Output :
We can also create more than one relationship between two Nodes, let's create one more relationship between karthiQ and CherCherTech
MATCH(author:karthiQ), (site:CherCherTech)
CREATE(author)-[:owns]->(site)
RETURN author, site
Output :
We can also create multiple relationships between the Nodes in one single query, lets create two more relationships between the karthiQ and CherCherTech
MATCH(author:karthiQ), (site:CherCherTech)
CREATE(author)<-[:ownedBy]-(site), (author)-[:owns]->(site)
RETURN author, site
Output :
We can create only Unique relationships using the UNIQUE keyword in the query, Lets create new Nodes and new relationships.
// create two nodes
CREATE (readr:You), (content:Neo4J)
// create UNIQUE relationship between the Nodes
MATCH(reader:You), (content:Neo4J)
CREATE UNIQUE (reader)<-[:ownedBy]-(content), (reader)-[:owns]->(content)
RETURN reader, content
Output :
Now again try to create a same relationships like above using UNIQue keyword.
// create UNIQUE relationship between the Nodes
MATCH(reader:You), (content:Neo4J)
CREATE UNIQUE (reader)<-[:ownedBy]-(content), (reader)-[:owns]->(content)
RETURN reader, content
Output :
Note : However if you try to create duplicate relationship without UNIQUE, the Neo4J lets you to create one irrespective of how you have created the first relationships (whether using UNIQUE keyword or not).
UNIQUE keyword only applicable for current Query and it will not affect the earliar queries or future queries.
// create self relationship
CREATE (d:Dog) -[:barks]->(d)
RETURN d
Output :
MATCH command helps the Neo4J user to retrieve the data from the graph database
MATCH (variableName:Criteria) RETURN variableName
Return statement is must for MATCH command, if we donot provide the RETURN keyword then Neo4J throws error. Create few set of data for the first time, so that we can retrieve. In below example we are creating dataset of search engines.
CREATE (google:Company{name:'google',type:'searchEngine',founder:'Larry page'}),
(yahoo:Company{name:'yahoo',type:'searchEngine',founder:'Jerry Yang'}),
(bing:Company{name:'bing',type:'searchEngine',founder:'Bill Gates'}),
(software:SearchEngines{name:'SearchEngine',type:'searchEngine'}),
(software)<-[:typeOf]-(google),
(software)<-[:typeOf]-(yahoo),
(software)<-[:typeOf]-(bing)
We can retrieve any kind of Node or Relationship without specifying the details of the Node or Relationship, but we need to pass the variable name to store the retrieved data and we should return the variable.
n- Variable name
MATCH (n) RETURN n
Output :
If we want to retrieve a specific data based on a Label of a node we can fetch those values as well.
MATCH (result:SearchEngines) RETURN result
Output :
User also can retrieve a relationship without any criteria
match(node1)--(node2) return node1, node2
Output :
To get very specific relationship, user have to mention what is the relationship between the Nodes, and If user want more specific then user can also mentions about the direction of relationship.
Below Query retrieves all the nodes which has typeOf relationships between nodes based on the direction of relationship.
match(node1)-[:typeOf]->(node2) return node1, node2
In above Neo4J tutorial, we have seen how to retrieve value, now lets retrieve the values based on the optional match.
For matching values Otionally we have to use OPTIONAL MATCH Keyword
Deleting Nodes and relationships are as important as creating them, In this Neo4J tutorial we will learn how to delete Nodes and Relationships.
Before deleting the Node, you must find the Node which you are going to delete using MATCH command.
Lets create couple of Nodes for deleteing purpose.
CREATE (google:google{type:'searchEngine',founder:'Larry page'}),
(yahoo:yahoo{type:'searchEngine',founder:'Jerry Yang'}),
(bing:bing{type:'searchEngine',founder:'Bill Gates'}),
(cherchertech:cherchertech{type:'TutorialSite',founder:'karthiQ'})
RETURN google, yahoo, bing, cherchertech
Output :
Now lets delete a Node which has 'bing' as label
MATCH (n:bing)
DELETE n
Output :
If we donot specify any criteri in match then Neo4J deletes all the nodes.
MATCH (n)
DELETE n
Output :
You can retrieve and verify whether any node is present or not after the deletion operation.
Let's create few Nodes with relationships
CREATE (google:google{name:'google', type:'searchEngine',founder:'Larry page'}),
(yahoo:yahoo{name:'yahoo', type:'searchEngine',founder:'Jerry Yang'}),
(bing:bing{name:'bing', type:'searchEngine',founder:'Bill Gates'}),
(cherchertech:cherchertech{name:'cherchertech', type:'TutorialSite',founder:'karthiQ'}),
(yahoo)$lt-[:typeOf]-(google),
(google)<-[:typeOf]-(bing)
Output :
Lets try to delete all the Nodes without deleting the relationships. We will get below error if we try so.
MATCH (nod)
DELETE (nod)
Cannot delete node
Let's retrieve all the data using MATCH command
From above image, we can get to know that no Node is deleted from the database.
Now lets try to delete the Nodes and relationships, with below code we are trying to delete the Nodes and reltionships without any criteria.
MATCH (nod)-[rel]-()
DELETE nod, rel
Output :
Above command only deleted the Nodes with relationships so if there is a node which is not related any other node/self will not be deleted. To confirm it you can write MATCH Command.
So we use the OPTIONAL MATCH method to delete the Nodes and Nodes with relationships.
Before performing please do populate the Database using queries mentioned above.
MATCH (nod)
OPTIONAL MATCH (nod)-[rel]-()
DELETE nod, rel
Below commandd deletes all the daa present in the Neo4J Graph Database.
MATCH (nod)
DETACH DELETE (nod)
In this Neo4J SET Tutorial we are going to learn how to SET, REMOVE properties of Nodes and Relationships, Also how to add Label adn how to remove labels from Nodes.
SET in Neo4J command helps user to Update the properties and lebles of Nodes and Relationships.
1. First lets create a Node and edit the properties.
CREATE(n:Node1) RETURN n
Output :
For above node we have not created any name, lets update the name using SET Command.
MATCH(n:Node1)
SET n.name = "karthiQ"
RETURN n
Output : Property got added to the Node.
We can add new label to the existing Node by using the SET command in Neo4J. Lets Add new Lable to the karthiQ Node (above created)
MATCH(n:Node1)
SET n:NewLabel
RETURN n
Output :
We can Create new label and properties using CREATE or SET command, but if we want to remove a label or a property from a NOde or a Relationship then we should use REMOVe command.
REMOVE command removes the name or the label based on our query, below example removes the label and property both that we have updated in SET example.
MATCH(n:Node1)
REMOVE n:NewLabel
REMOVE n.name
RETURN n
Output :
Sometimes we may be in a position, where we have to set a property of a Node or Relationship dynamically, in such cases we have to use WITH command along with set Command.
Neo4J allows user to set the values dynamically but we have to separate the statement using WITH command, WITH command acts as a pipe concept in Unix.
Lets Create a Nodes With few Contact for dynamic calculation purpose.
CREATE(n1:NodeOne),(n2:NodeTwo),(n3:NodeThree),
(n1)-[:nearBy]->(n2),
(n2)-[:nearBy]->(n3)
RETURN n1, n2, n3
Output :
From above image we can get to know that NodeTwo have nearby relation with NodeOne and NodeThree, so NodeTwo has two nearBy relationships.
We can use COUNT method to count the results of a query.
MATCH(n:NodeTwo)-[rel:nearBy]-()
WITH n, COUNT(rel) AS no_of_rel
SET n.no_relation = no_of_rel
RETURN n
Output :
If we try to execute above Query without using WITH then it will not work also throw folloowing exception.
MATCH(n:NodeTwo)-[rel:nearBy]-()
SET n.no_relation = count(rel)
Output :
"SET n.no_relation = count(rel)"
Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.