Neo4j Graph Database Fundamentals · درس

تحديث بيانات الرسم البياني وحذفها

تعلّم تعديل العُقد والعلاقات الحالية باستخدام SET وREMOVE، وحذف البيانات بأمان باستخدام DELETE وDETACH DELETE

الدرس 3 من 411 خطوة

تحديث بيانات الرسم البياني وحذفها درس مجاني في Neo4j Graph Database Fundamentals على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.age

Update 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.status

Add 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.occupation

Remove 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.occupation

Remove 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, b

Delete 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 u

Deletion Best Practices

Deleting data is permanent! Always exercise caution:

  • Test first: Run your MATCH clause with a RETURN to ensure you're selecting the correct elements before adding DELETE or DETACH 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!

البدء مجانًا

تعلم Neo4j Graph Database Fundamentals مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «تحديث بيانات الرسم البياني وحذفها» مجاني؟

نعم — نص درس «تحديث بيانات الرسم البياني وحذفها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Neo4j Graph Database Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Neo4j Graph Database Fundamentals 4 دروس في المجموع.

ماذا ستتعلم في «تحديث بيانات الرسم البياني وحذفها»؟

تعلّم تعديل العُقد والعلاقات الحالية باستخدام SET وREMOVE، وحذف البيانات بأمان باستخدام DELETE وDETACH DELETE تتمرن على Neo4j Graph Database Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Neo4j Graph Database Fundamentals؟

لا تُشترط خبرة سابقة. Neo4j Graph Database Fundamentals على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «تحديث بيانات الرسم البياني وحذفها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Neo4j Graph Database Fundamentals هذا؟

نعم. كل درس في Neo4j Graph Database Fundamentals يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تصفية النتائج وترتيبها
  2. التجميع والإسقاطات
  3. تحديث بيانات الرسم البياني وحذفها
  4. دمج الاستعلامات باستخدام WITH وUNION
← العودة إلى Neo4j Graph Database Fundamentals