neo4j
  1. neo4j-write-clauses

Write Clauses - (Neo4j Write Clauses)

Neo4j is a graph database management system that allows users to create and manage graph databases. In Neo4j, write clauses allow users to add, update, and delete data in a graph database. Write clauses are used to create, update, merge, set property, or delete nodes and relationships in a graph database.

Syntax

// Create node(s)
CREATE (n:Label {property: value})

// Merge node(s)
MERGE (n:Label {property: value})

// Update node(s)
MATCH (n:Label {property: value}) SET n.property = newValue

// Create a relationship
CREATE (a)-[r:RELATIONSHIP {property: value}]->(b)

// Delete a relationship
MATCH (a)-[r:RELATIONSHIP]->(b) DELETE r

// Delete a node and all its relationships
MATCH (n:Label {property: value}) DETACH DELETE n

Example

// Create a node
CREATE (n:Person { name: 'John Doe', age: 25, city: 'New York'})

// Merge a node
MERGE (n:Person { name: 'Jane Doe'})
  ON CREATE SET n.age = 30, n.city = 'San Francisco'
  ON MATCH SET n.city = 'San Francisco'

// Update a node
MATCH (p:Person) WHERE p.name = 'John Doe'
SET p.city = 'Washington DC'

// Create a relationship between nodes
MATCH (p1:Person { name: 'John Doe'}), (p2:Person { name: 'Jane Doe' })
CREATE (p1)-[:KNOWS { since: 2012}]->(p2)

// Delete a relationship
MATCH (p1:Person)-[r:KNOWS]->(p2:Person)
WHERE p1.name = 'John Doe' AND p2.name = 'Jane Doe'
DELETE r

// Delete a node and all its relationships
MATCH (n:Person { name: 'John Doe'})
DETACH DELETE n

In this example, we created a node with the label Person and the properties name, age, and city. We then merged a new Person node with the name Jane Doe, setting its age and city properties if it did not already exist, or updating its city property if it already existed. We then updated a Person node with the name John Doe, setting its city property to Washington DC. After that, we created a relationship between John Doe and Jane Doe with the label KNOWS and the property since. We then deleted that relationship. Finally, we deleted the Person node with the name John Doe and all its relationships.

Output

The output of using write clauses in Neo4j is the creation, updating, merging, or deletion of nodes and relationships in a graph database.

Explanation

Write clauses in Neo4j allow users to create, update, merge, or delete nodes and relationships in a graph database. The specific syntax used will depend on the operation being performed, such as creating a node with the CREATE clause, updating a node with the SET clause, or deleting a node with the DELETE clause.

Use

Using write clauses in Neo4j allows users to add, update, or delete data in a graph database. This is useful for updating data in real-time or performing batch updates.

Important Points

  • Neo4j write clauses include CREATE, MERGE, SET, and DELETE clauses.
  • Write clauses allow users to create, update, merge, or delete nodes and relationships in a graph database.
  • Care should be taken when deleting data to ensure that the correct nodes and relationships are being deleted.

Summary

In summary, write clauses in Neo4j allow users to add, update, or delete data in a graph database. Examples of write clauses include CREATE, MERGE, SET, and DELETE. Using write clauses in Neo4j allows data to be updated in real-time or in batches. It is important to take care when deleting data to ensure that the correct nodes and relationships are being deleted.

Published on: