Neo4j is a graph database management system developed by Neo4j. Neo4j is implemented in Java and accessible from software written in other languages using the Cypher query language .
In this file let's see how to Create
, Read
, Update
, Delete
data in graph database through Neo4J.
To create a new node in database:
create(n:"Label Name")
Example
create(n:"employee")
Create and Return new node in database.
create(n:"Label Name") return n
Examplecreate(n:"employee") return n
Create Node with multiple labels:
create(n:"First Name:Second Label:Third Label:....") return n
Example
create(n:"employee:proctor") return n
Creating Multiple Node with Single Command having same or different labels:
create(n:"Label Name"),(n1:"Label Name") return n,n1
Example
create(n:employee),(n1:proctor) return n,n1
Creating Nodes with Properties:
create(n:"Label Name{propertyName:"PropertyValue",...."}"),(n1:"Label Name{propertyName:"PropertyValue",...."}") return n,n1
Example
create(n:employee{name:'ram', branch:'cse'}),(n1:proctor{name:''someone}) return n,n1
Creating multiple nodes of same and different Label having different properties.
CREATE(e:Label Name{propertyName:"PropertyValue",...."}),(n1:"Second Label{propertyName:"PropertyValue",...."}")return n,n1 e
Example
CREATE(e:employee{name:"ram",branch:"cse"}),(f:employee{name:"sam",branch:"ece"}),(h:professor{name:"satish",designation:"ap1"}),(j:professor{name:"ramesh",designation:{"ap2"}}) return e,f,h,j e
Match all nodes inside database but return none.
Match(n)
Match all nodes inside database and return all nodes.
Match(n) return n
Finding Nodes of specific labels and returning their properties.
MATCH(e:employee),(d:department) return e.name,d.name
Using Where
clause to find specific person from selected label/tale.
MATCH(e:employee) where e.name="HaseebAhmad" return e
Finding specific person without using Where Clause.
MATCH(e:employee{name:"HaseebAhmad"}) return e
Using set
clause to update the property.
match(e:employee{name:’satish’}) set e.salary = 20000
This will set the salary property to 20000, this property was not included before, it is added now.
Removing property using remove
clause.
match(e:employee{name:’satish’}) remove e.salary
This will remove the salary property from node.
Adding and Removing Properties using the single command.
match(e:employee{name:’satish’}) remove e.name set e.firstName=’satish’ return e
This will return node of satish with ‘name’ property being removed and firstName is added.
Using ‘Set’ and ‘Remove’ clauses to change the label
/Type
of a node.
match(e:employee{name:”satish”}) remove e:employee set e:clerk return e
This will remove ‘e’ from employee and set it to ‘clerk’ label.
Using ‘Delete’ clause to delete the node.
match(e:employee) delete e
match(n) delete n
This “IN” keyword search for value to fall in specific set of values like in above array.
match(e:employee)wheree.namein['satihs','Haseeb']return e
Use of ‘AND’ operator.
match(e:employee)wheree.name='Haseeb'ande.salary=20000 return e
Use of ‘OR’ operator.
match(e:employee)where e.name='Haseeb'or e.name='satish' return e
In this we are first matching/finding the required nodes, then we are creating a link between two nodes by specifying the Label
of relationship and then returning all ‘nodes’ and their relation
.
match(e:employee{name:’satish’}), (d:department{name:’scope’}) create (e) –[w:worksFor] -> (d) return e,w,d
This will find the all employees which have workfor
relationship with department named as scope
.
match(e:employee)-[w:worksfor]->(d:department{name:’scope’}) return e,w,d
It will return all departments in which satish
works.
match(e:employee{name:’satish’})-[w:worksfor]->(d:department) return e,w,d
Updating Relations between Nodes
To add more values/information to label attaching the two nodes.
match(e:employee{name:"Haseeb"})-[s:studiesIn]->(d:department{name:"SEECS"}) set s.studyingDepartment="DOC" return e,s,d
To remove the new value added on label.
match(e:employee{name:"Haseeb"})-[s:studiesIn]->(d:department{name:"SEECS"}) remove s.studyingDepartment return e,s,d
To delete the relationship between the nodes and set them free of each other.
match(e:employee{name:"Haseeb"})-[s:studiesIn]->(d:department{name:"SEECS"}) delete s return e,d
When we have relationship between two nodes, we can’t delete those nodes. The command for this will be, match(n) detach delete n
.
For deleting a specific node we can first
select it using match() clause and then detach it and at the end delete it
.
To create nodes and their relationship at the same time.
create(d:driver{name:'Haseeb'})-[r:drives]->(c:car{name:"Kia"}) return d,r,c
To select all nodes irrespective of their labels, which have incoming/outgoing “specific label” relationship attached with them.
match(e)-[r:drives]->(d) return e,r,d
This will add more values/information in label attaching the two nodes.
match(e:employee{name:"Haseeb"})-[s:studiesIn]->(d:department{name:"SEECS"}) set s.studyingDepartment="DOC" return e,s,d
This will remove the new value added on label as studyingDepartment
.
match(e:employee{name:"Haseeb"})-[s:studiesIn]->(d:department{name:"SEECS"}) remove s.studyingDepartment return e,s,d
When we have relationship between two nodes, we can’t delete those nodes. The command for this will be
match(n) detach delete n
.
For deleting a specific node we can
select it using match() clause and then detach it and at the end delete it
.
This will create both nodes and their relationship at the same time.
create(d:driver{name:'Haseeb'})-[r:drives]->(c:car{name:"Kia"}) return d,r,c
This will select all nodes irrespective of their labels, which have incoming/outgoing “drives” relationship attached with them.
match(e)-[r:drives]->(d) return e,r,d
This query will add two nodes in the same query time to a single department.
create(e:employee{name:"Waleed"})-[s:studiesIn]->(d:department{name:"SEECS"}) <-[s1:studiesIn]-(e1:employee{name:"Kalasra"}) return e, e1, s, s1, d
This will create three Node in which one is working in two departments and other two are working in same department along with the departments in which they are working. This operation is completed using a single command.
CREATE(e:employee{name:"satish"})-[w:worksFor]->(d:department{name:"SMME"})<-[w1:workFor]-(e1:employee{name:"sam"})-[m:manages]->(d1:department{name:"sbst"})<-[m1:manages]-(e2:employee{name:"tom"}) return e, w, d, w1, e1, m,d1, m1,e2
This will return all incoming relationships from to “SMME” department, from any other node.
match(d:department{name:"SMME"})<--(e) return d,e
This return all out-going nodes of all employees, like to which other node they are attached with.
match(d:employee)-->(e) return d,e
This query will return nodes which have “manages”, ”workFor” relationships between. Each node can have both or any one relationship.
match(e)-[m:manages|worksFor]->(d) return m,d,e
This will return employee names in ascending order.
match(e:employee) return e.name order by e.name
This will return names of employees ordered by multiple properties.
match(e:employee) return e.name, e.salary order by e.name, e.salary
This command will skip the first “Integer” value from result list and return the remaining list.
match(e:employee) return e.name skip {Any Integer Value}
This returns only top 3 results from the result list.
match(e:employee) return e.name limit 3
This command will return unique result by joining two sets described in our query labels.
match(e:employee) return e.name UNION match (e:employee) return e.name
This command will return all values irrespective of rather they occur single time or twice or more, that is the main difference between “UNION” and “UNION ALL” clause.
match(e:employee) return e.name UNION ALL match (e:employee) return e.name
This query creates a new node if it does not exist otherwise return it as it is, with its value.
merge(e:driver) return e
This command will create a new node if it does not exist and if it is created by this command then it will set its property “Joined” on creating this node.
merge(e:clerk) on create set e.Joined="Today" return e
This command will create a new node if it does not exist and if it existed then this command will change the “Joined” property to a new value.
merge(e:clerk) on match set e.Joined="Yesterday" return e
Single command to run above both cases.
merge(e:clerk) on match set e.Joined="Yesterday" on create set e.Joined="Today" return e