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.
sqlMATCH (n) WHERE EXISTS(n.Gender)Merge (gender:Gender{Gender:n.Gender})return gender
Merge Command - ON CREATE
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.
sqlMERGE (Yuki:Person{Name:"Yuki"})ON CREATE SET Yuki.Gender = "Female"RETURN Yuki.Name,Yuki.Gender
Merge Command - ON MATCH
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.
sqlMerge (person:Person)ON MATCH SET person.Is_Person = TRUERETURN 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
Merge Command - Relationships
sqlMATCH (girl:Person),(boy:Person)WHERE girl.Name = "Yuki" AND boy.Name = "mika"MERGE (girl)-[relationship:Has_A_Boy_Friend]->(boy)RETURN girl,boy
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.
sqlMATCH (Yuki:Person)WHERE Yuki.Name = "Yuki"SET Yuki.School = "Tokyo University"SET Yuki.Is_Person = NULLRETURN 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.
sqlMATCH (Yuki:Person)WHERE Yuki.Name = "Yuki"SET Yuki:Student:GrilRETURN Yuki
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.
DELETE all
sqlMATCH (node)DETACH DELETE node
DETACH is to firstly delete the relationships between the nodes that we are going to delete.
Delete selection
It is quite similar but still notice that when deleting a node, we should delete the relationships first.
sqlMATCH (mika:Person)WHERE mika.Name = "mika"DETACH DELETE mika