更新与删除图数据
学习使用 SET 和 REMOVE 修改现有节点与关系,并使用 DELETE 和 DETACH DELETE 安全删除数据
更新与删除图数据 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Modifying Your Graph Data
In this lesson, you'll learn how to update and delete existing nodes and relationships in your Neo4j graph using Cypher. Modifying data is crucial for keeping your graph accurate and up-to-date.
We'll cover the essential clauses: SET for changing properties, REMOVE for taking properties or labels away, and DELETE / DETACH DELETE for removing graph elements entirely.
Update Node Properties with SET
The SET clause is used to modify existing properties on nodes or relationships, or to add new ones. Let's start by updating a node's property.
This example first creates a 'Person' node, then updates their age.
CREATE (p:Person {name: 'Alice', age: 30})
RETURN p
MATCH (p:Person {name: 'Alice'})
SET p.age = 31
RETURN p.name, p.ageUpdate Relationship Properties
Just like nodes, relationships can also have properties. You can use SET to update these properties too. First, we create a relationship, then we update one of its properties.
CREATE (p1:Person {name: 'Bob'})-[:KNOWS {since: 2019}]->(p2:Person {name: 'Charlie'})
RETURN p1, p2
MATCH (p1:Person {name: 'Bob'})-[r:KNOWS]->(p2:Person {name: 'Charlie'})
SET r.since = 2020, r.status = 'Good Friends'
RETURN r.since, r.statusAdd New Properties with SET
If you use SET with a property name that doesn't exist on a node or relationship, Cypher will simply add that new property. This is a handy way to extend your data model on the fly.
MATCH (p:Person {name: 'Alice'})
SET p.city = 'New York', p.occupation = 'Engineer'
RETURN p.name, p.city, p.occupationRemove Properties with REMOVE
Sometimes you need to get rid of a property entirely. The REMOVE clause is designed for this. It can delete one or more properties from a node or a relationship.
Remember, REMOVE is for properties and labels, not for deleting entire nodes or relationships.
MATCH (p:Person {name: 'Alice'})
REMOVE p.occupation
RETURN p.name, p.age, p.city, p.occupationRemove Node Labels
The REMOVE clause isn't just for properties; you can also use it to remove labels from a node. This can change the node's type or classification within your graph.
After removing the 'Person' label, the node still exists but is now just a generic node without that specific type.
MATCH (p:Person {name: 'Alice'})
REMOVE p:Person
RETURN p, labels(p)Delete Relationships with DELETE
To remove a relationship, you use the DELETE clause. You must first MATCH the relationship you want to delete and then pass its variable to DELETE.
Note: DELETE can only remove relationships or nodes that have no relationships.
CREATE (a:User {id: 1})-[:LIKES]->(b:Product {id: 10})
RETURN a, b
MATCH (a:User {id: 1})-[r:LIKES]->(b:Product {id: 10})
DELETE r
RETURN a, bDelete Nodes with DETACH DELETE
What if you want to delete a node that has relationships connected to it? A simple DELETE won't work and will give an error.
The solution is DETACH DELETE. This powerful clause deletes the node and all its incoming and outgoing relationships in one go.
CREATE (u:User {name: 'David'})-[:FOLLOWS]->(o:User {name: 'Eve'})
RETURN u, o
MATCH (u:User {name: 'David'})
DETACH DELETE u
RETURN uDeletion Best Practices
Deleting data is permanent! Always exercise caution:
- Test first: Run your
MATCHclause with aRETURNto ensure you're selecting the correct elements before addingDELETEorDETACH DELETE. - Backup: For critical data, ensure you have backups.
- Transaction awareness: In applications, deletions are often part of larger transactions.
Deletion Challenge
You have a (:Movie {title: 'Inception'}) node that is connected to many (:Actor) nodes via [:ACTED_IN] relationships. You want to remove the 'Inception' movie node and all its connections.
Lesson Summary
Great job! You've mastered the core Cypher clauses for modifying and deleting graph data:
SET: Updates existing properties or adds new ones to nodes and relationships.REMOVE: Deletes specific properties or labels from nodes and relationships.DELETE: Removes relationships or nodes that have no connections.DETACH DELETE: The safest way to delete a node and all its associated relationships in one go.
Always be careful when deleting data, and verify your MATCH patterns first!
常见问题解答
「更新与删除图数据」课时是免费的吗?
是的 — 「更新与删除图数据」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
「更新与删除图数据」这节课中我会学到什么?
学习使用 SET 和 REMOVE 修改现有节点与关系,并使用 DELETE 和 DETACH DELETE 安全删除数据 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Neo4j Graph Database Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「更新与删除图数据」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?
能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 筛选与排序结果
- 聚合与投影
- 更新与删除图数据
- 使用 WITH 和 UNION 组合查询