【问题标题】:What's the Cypher script to delete a node by ID?按 ID 删除节点的 Cypher 脚本是什么?
【发布时间】:2015-03-24 13:10:38
【问题描述】:

在 SQL 中:

Delete From Person Where ID = 1;

在 Cypher 中,通过 ID 删除节点的脚本是什么?

(已编辑:ID = Neo4j 的内部节点 ID)

【问题讨论】:

    标签: neo4j nosql cypher graph-databases


    【解决方案1】:

    假设您指的是 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
    

    【讨论】:

    • 您可以使用 DETACH DELETE 代替可选匹配
    • 在这种情况下,“人”是什么?它是“名称”属性吗?
    • 这只有在你不想删除()中的内容时才有效。
    【解决方案2】:

    当节点是孤儿时。

    Start n=node(1)
    Delete n;
    

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      按照@saad-khan 提供的链接,这是获取节点和关系 ID 的示例。 下面的代码显示了 ID,因此您可以确保删除与给定 ID 相关的所有内容。

      MATCH (node)-[relation:HAS]->(value) where ID(node)=1234 RETURN ID(instance), ID(value), ID(r)

      Ps.:“:HAS”是关系的一个例子。

      【讨论】:

        【解决方案5】:

        老问题和答案,但要在有关系时删除节点,请使用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

        【讨论】:

          猜你喜欢
          • 2016-01-06
          • 2012-07-30
          • 1970-01-01
          • 2022-12-12
          • 1970-01-01
          • 2022-12-30
          • 1970-01-01
          • 1970-01-01
          • 2019-03-27
          相关资源
          最近更新 更多