neo4j图数据库,如何删除一个节点或一个关系数据?
发布于 作者:苏南大叔 来源:程序如此灵动~
苏南大叔在本文中将要讲述neo4j图数据库的CRUD操作的最后一个部分D,如何删除一个节点或者一个关系。其实这个部分的内容,在之前的文章里面基本都是有提及的。本文还是再说一次,巩固一下基础知识。

大家好,这里是苏南大叔的“程序如此灵动”博客,这里记录苏南大叔和计算机代码的故事。本文描述,如何在neo4j图数据库中,如何删除节点或者是关系的数据呢?本文测试环境:win10,neo4j@4.4.5。
前置文章
如何选定节点:
如何选定关系:
如何删除节点标签及属性(更新节点)/关系类型及属性(更新关系):
删除节点
删除节点属性:
Match(n:Person{name:'sunan'}) remove n.born return n删除节点标签:
match(n:Person) remove n:人类删除节点:
Match(n:Person{name:'sunan'}) delete n删除节点带关系
注意:有关系存在的节点还删除不掉:报错如下:
Cannot delete node<166>, because it still has relationships. To delete this node, you must first delete its relationships.
删除节点及其关系:
Match(n:Person{name:'sunan'}) detach delete n
删除所有节点(完全不考虑关系):
MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r或者
match (n) detach delete n删除关系
删除关系类型:(删除不掉)
一条关系必须存在类型,单独删除类型定义,是不行的。但是可以替换类型定义:
MATCH (n:Person{name:'sunan'})-[r:Watch]->(m:Movie{title:'The Matrix'})
CREATE (n)-[r2:Learn]->(m)
SET r2 = r
WITH r
DELETE r删除关系属性:
match (n:Person{name:'sunan'})-[r]->(m:Movie{title:'The Matrix'})
remove r.aa
return r,type(r)删除关系:
MATCH (n:Person{name:'sunan'})-[r:Watch]->(m:Movie{title:'The Matrix'})
DELETE r参考文献
- https://newsn.net/say/neo4j-match-node.html
- https://newsn.net/say/neo4j-match-relationship.html
- https://newsn.net/say/neo4j-cypher-update.html
- https://newsn.net/say/neo4j-cypher-delete.html
- https://newsn.net/say/neo4j-rm-database.html
综述
neo4j图数据库的增删改查系列文章,暂时结束。当然,后续还有其它更高级的应用文章。敬请关注苏南大叔的neo4j及cypher系列文章: