neo4j
  1. neo4j-set-clause

Set Clause

The SET clause is a write clause in Neo4j that is used to set values for properties of nodes and relationships in the graph database. This allows you to update the values of existing nodes and relationships.

Syntax

The basic syntax for the SET clause is as follows:

MATCH (node:Label)
SET node.property = 'new_value'

Where Label is the label of the node you want to update, property is the name of the property you want to update, and new_value is the new value you want to set for the property.

Example

Consider the following graph of a social network:

Social Network Graph

To update the name of the person with ID 2 from "Liam" to "William", the following Cypher query with a SET clause can be used:

MATCH (p:Person {id: 2})
SET p.name = 'William'

After running this query, the value of the name property of the person with ID 2 will be set to "William".

Explanation

In the example above, we use the MATCH clause to find the node with the ID of 2 and label Person. Then we use the SET clause to set the value of the name property to "William". This updates the existing node's property value in the graph.

Use

The SET clause is useful when you want to update the properties of a node or relationship in the graph database. This allows you to modify the data in the database without having to create new nodes or relationships.

Important Points

  • You can set values for multiple properties of a node or relationship in a single SET clause.
  • The SET clause can also be used to create new properties for a node or relationship that do not exist.
  • When setting a property value, you can use Cypher expressions to calculate the new value based on existing property values.

Summary

The SET clause is a write clause in Neo4j that allows you to set values for the properties of nodes and relationships in the graph database. It is useful when you want to update the data in the database without creating new nodes or relationships. The syntax of the SET clause includes a match pattern for finding the node or relationship to update and the property and value to set for the node or relationship.

Published on: