CREATE : Neo4j

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.

CREATE Node :
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 : simple-create-command-neo4j

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 : create-node-with-label-neo4j In above image you can find the label in blue color. Similarly we can create multiple labels for a Node by seperating them with :(colon).

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 : create-node-with-properties-neo4j

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 : multiple-nodes-creation-neo4j

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.

CREATE Relationships :
We can create relationships between Nodes, We have to use -- (two hypehns) to create relationship between nodes, Type of relationship is specified by [](square brackets). Also we have to give direction of the relationship, direction will be specified usinf < and >

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 : basic-relation-neo4j

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 : create-relationship-between-existing-nodes

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 : multiple-relationship-nodes

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 : duplicate-relationships-between-nodes-neo4j

Uniques Relationships In above we have created total 4 relationships between two Nodes, but if you notice we have created duplicate relationships. Creating duplicate relationship will be usefull in most of the cases, and sometimes we may want to have only Unique relationships between the Nodes.

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 : uniques-command-neo4j

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 : uniques-command-neo4j

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.

Self Relationship : Sometimes we may need to create relationship to the same Node from same Node, i.e create Relationship from Node1 to Node1.


										// create self relationship
										CREATE (d:Dog) -[:barks]->(d)
										RETURN d
										

Output : self-relationship-neo4j

MATCH Query in Neo4J

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 : match-all-return-all-neo4j

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 : match-based-on-label-neo4j

User also can retrieve a relationship without any criteria


										match(node1)--(node2) return node1, node2
										

Output : match-all-relationships-neo4j

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
										

match-all-relationships-neo4j

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

DELETE Neo4J : Graph database

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 : return-all-4-companies-neo4j

Now lets delete a Node which has 'bing' as label


										MATCH (n:bing)
										DELETE n
										

Output : delete-bing-neo4j

If we donot specify any criteri in match then Neo4J deletes all the nodes.


										MATCH (n)
										DELETE n
										

Output : delete-all-nodes-neo4j You can retrieve and verify whether any node is present or not after the deletion operation.

Delete Relationships : Let's learn about deletion of Relationships and nodes with relationshipsin Neo4J, We cannot delete Nodes which are having relationship with other Nodes. So we have to delete relationships and Nodes which are linked.

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 : delete-relationships-neo4j Lets try to delete all the Nodes without deleting the relationships. We will get below error if we try so.


										MATCH (nod)
										DELETE (nod)
										


Neo.ClientError.Schema.ConstraintValidationFailed
Cannot delete node, because it still has relationships. To delete this node, you must first delete its relationships.


Let's retrieve all the data using MATCH command
delete-nodes-without-deleting-relationships-neo4j 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 : delete-nodes-relationships-neo4j 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. not-deleted-node-neo4j

OPTIONAL MATCH DELETE when we donot know that whether an Node has any relationship are not, we go with just delete method as if we give Command to delete Node with relationship, it will not delete the Nodes without relationship.

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
										


DETACH and DELETE : DETACH keyword remove the relationship between node (detaches the nodes), further based on the DELETE command Neo4J deletes the nodes.

Below commandd deletes all the daa present in the Neo4J Graph Database.


										MATCH (nod)
										DETACH DELETE (nod)
										

SET : Neo4J

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 : node-create-set-properties-neo4j

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. set-name-ne4j

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 : update-newlabel-added-neo4j

REMOVE command : Neo4J

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 : remove-property-label-neo4j

WITH Command in Neo4J

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 : with-command-neo4j-dynamic

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 : dynamic-update-count-function-neo4j

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 :
Invalid use of aggregating function count(...) in this context (line 2, column 21 (offset: 53))
"SET n.no_relation = count(rel)"

 
Join My Facebook Group
Join Group
 

About Author

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.

Share this Article Facebook
You can also share knowledge By Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
 
Selenium-Webdriver.Com [Group]
Facebook Group · 801 members
Join Group
Learn Selenium Webdriver Concepts and Interview Questions Learn and code
 
Copyright © CherCher Tech