在 SQL 中:
Delete From Person Where ID = 1;
在 Cypher 中,通过 ID 删除一个节点的脚本是什么?
(编辑说明:ID = Neo4j 的内部节点 ID)
在 SQL 中:
Delete From Person Where ID = 1;
在 Cypher 中,通过 ID 删除一个节点的脚本是什么?
(编辑说明:ID = Neo4j 的内部节点 ID)
MATCH (p:Person) where ID(p)=1
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
如果你指的是节点上自己属性的'id': MATCH (p:Person {id:1})
OPTIONAL MATCH (p)-[r]-() //drops p's relations
DELETE r,p
MATCH (n) where id(n) = x
DETACH DELETE n
https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-id
这是一个旧问题并已有答案,但是当需要删除具有关系的节点时,请使用DETACH
。
MATCH (n) where ID(n)=<your_id>
DETACH DELETE n
否则您会得到以下结果:
Neo.ClientError.Schema.ConstraintValidationFailed: Cannot delete node<21>, because it still has relationships. To delete this node, you must first delete its relationships.
这就像SQL中的CASCADE
Start n=node(1)
Delete n;
在@saad-khan提供的链接中,下面是一个获取节点和关系ID的示例代码。
MATCH (node)-[relation:HAS]->(value)
where ID(node)=1234
RETURN ID(instance), ID(value), ID(r)
注:":HAS"是关系的示例。