1. Introduction:
In this section, we are going to learn and summarize the most recent used CRUD operations in Neo4j.We are going to use SET, Remove, Delete and Merge(Create and MATCH) clause.
2 Merge Command
Merge commend is used to either create a node with labels or properties if the node does not exist in the database or select the nodes with particular conditions if the nodes are already presents in the database.
2.1 Using Merge command with labels and properties.
Example
$ Merge(Jeson:Man) return Jeson, labels(Jeson)
Since it does not exists in the database, it will create a new node and a new label Man and return the node and labels of that specific node.
$ Merge(Jack:Man{Name:"Jack",Gender:"male"}) return Jack
When running this merge command, it will firstly look at if there exists a node jack with corresponding key-value pairs, if it does not exits, it will create a new node and return the node.
$ Merge(Jack:Man{Name:"Jack",Age:"30"}) return Jack
It will seperately create a node with the same name as before but differnt properties.
Then, if we run the following comand
$ Merge(Man{Name:"Jack"}) return Man.Name, Man.Gender, Man.Age
It will find all the nodes with name Jack and label Man, and the properties related to jack
2.2 Using Merge command to create Node with existing node properties.
MATCH (n) WHERE EXISTS(n.Gender)
Merge (gender:Gender{Gender:n.Gender})
return gender
We could see that we have created all gender with matching all the nodes with Gender properties.
2.3 Merge command with on create clause.
When creating a new node, we would like to set the properties for this node at the same time, ON CREATE clause will allows the user to set properties at creation time.
Merge (Yuki:Person{Name:"Yuki"})
ON CREATE SET Yuki.Gender = "Female"
RETURN Yuki.Name,Yuki.Gender
2.4 Merge command with on match clause.
When traversing from the graph database to find some nodes with some conditions, we need to set some properties to those node at the same time for updating the information regarding to this node.
Merge (person:Person)
ON MATCH SET person.Is_Person = TRUE
RETURN person.Name, person.Is_Person
Using merge command we could find all the node with label Person and add a new property Is_Person to be true
2.4 Merge command for relationships
Merge (mika:Person{Name:"mika"})
ON CREATE SET mika.Gender = "Male"
RETURN mika.Name,mika.Gender
Suppose Yuki has a boyfriend mika. we want to create relationships between them.We could run the following commands
MATCH (girl:Person),(boy:Person)
WHERE girl.Name = "Yuki" AND boy.Name = "mika"
MERGE (girl)-[relationship:Has_A_Boy_Friend]->(boy)
RETURN girl,boy
3 SET Command
SET Clause is to set and remove properties of nodes. If Yuki goes to Tokyo University, we could run the following command. We could use set clause to add school properties to Yuki, and remove the Is_Person property we have set before.
MATCH (Yuki:Person)
WHERE Yuki.Name = "Yuki"
SET Yuki.School = "Tokyo University"
SET Yuki.Is_Person = NULL
RETURN Yuki
We could also use SET Clause to set multiple labels for some nodes,e.g,add two labels to Yuki which are Student and Gril.
MATCH (Yuki:Person)
WHERE Yuki.Name = "Yuki"
SET Yuki:Student:Gril
RETURN Yuki
4 DELETE Command
DELETE command is to delete specific nodes or relationships between nodes. Here we will learn some of the basic usage of delete clause in CQL.
4.1 DELETE ALL NODES
MATCH (node)
DETACH DELETE node
DETACH is to firstly delete the relationships between the nodes that we are going to delete.
4.2 DELETE GROUPS OF NODES WITH CONDITIONS
It is quite similar but still notice that when deleting a node, we should delete the relationships first.
MATCH (mika:Person)
WHERE mika.Name = "mika"
DETACH DELETE mika
Next, I will go through an example to have a overall understanding of CRUD By SQL.
TO BE CONTINUED