Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Thursday 29 February 2024

Modify in Neo4j

In Neo4j, to modify existing data in the graph, you typically use the CREATE, MERGE, SET, and REMOVE clauses. These clauses allow you to add, update, or remove nodes, relationships, or properties in the graph. Let's explore each of them with examples:


1. CREATE Clause:

The CREATE clause is used to add new nodes and relationships to the graph. It's typically used when you want to ensure that a new element is created without checking for existing elements.


Example:


Creating a new person node

CREATE (:Person {name: 'Alice', age: 30})


2. MERGE Clause:

The MERGE clause is used to find existing nodes or relationships that match a pattern or create them if they don't exist. It's commonly used to ensure that duplicates are not created.


Example:


Merge or create a person node with a specific name

MERGE (p:Person {name: 'Alice'})

ON CREATE SET p.age = 30


3. SET Clause:

The SET clause is used to update properties of nodes or relationships in the graph.


Example:


Set or update the age property of the person node named 'Alice'

MATCH (p:Person {name: 'Alice'})

SET p.age = 31


4. REMOVE Clause:

The REMOVE clause is used to remove properties from nodes or relationships.


Example:


Remove the age property from the person node named 'Alice'

MATCH (p:Person {name: 'Alice'})

REMOVE p.age


Example:

Suppose we have a person node in the graph with the name 'Alice' and age '30'. Now, we want to update Alice's age to '31' and add a new property 'city' to her.


Code:


Update Alice's age to 31 and add a city property

MATCH (p:Person {name: 'Alice'})

SET p.age = 31, p.city = 'New York'


In this code:

- We use the MATCH clause to find the person node with the name 'Alice'.

- The SET clause updates the age property to '31' and adds a new city property with the value 'New York' to the node.


This is how you can modify existing data in Neo4j using the SET clause. You can combine MATCH, SET, CREATE, MERGE, and REMOVE clauses to perform more complex modifications based on your requirements.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.