top of page
  • Writer's pictureRevanth Reddy Tondapu

Part 9: Mastering CRUD Operations in Cypher: A Comprehensive Guide


Mastering CRUD Operations in Cypher
Mastering CRUD Operations in Cypher

Welcome to a step-by-step guide on performing CRUD operations in Cypher! CRUD stands for Create, Read, Update, and Delete—essential operations for managing your data in Neo4j. In this demo, we'll walk through each operation, providing practical examples to help you understand and apply these concepts effectively.


Creating Nodes and Relationships

Creating nodes and relationships is the first step in building your graph database.


Example: Create a New Movie Node

Let's start by creating a new movie node with properties such as title, release year, and duration. The newly created node will be returned.

CREATE (m:Movie {title: 'Inception', released: 2010, duration: 148})
RETURN m

This query creates a movie node labeled Movie with the specified properties.


Example: Create an Actor Node and a Relationship

Next, we'll create an actor node and establish a relationship between the actor and the movie "Inception."

CREATE (a:Person {name: 'Leonardo DiCaprio'})
WITH a
MATCH (m:Movie {title: 'Inception'})
CREATE (a)-[:ACTED_IN]->(m)
RETURN a, m

This query first creates an actor node labeled Person with the name "Leonardo DiCaprio." The WITH clause carries the created node forward. Then it matches the "Inception" movie node and creates an ACTED_IN relationship between the actor and the movie.


Reading Data

Reading data from the database allows you to retrieve and analyze your stored information.


Example: Retrieve All Movies and Their Release Years

We'll match all movie nodes and return their title and release year properties.

MATCH (m:Movie)
RETURN m.title, m.released

This query retrieves the title and release year of all movie nodes in the database.


Example: Retrieve Movies Released After 2000

Let's read data with a specific filter to find all movies released after the year 2000.

MATCH (m:Movie)
WHERE m.released > 2000
RETURN m.title, m.released

This query matches movie nodes with a release year greater than 2000 and returns their title and release year properties.


Updating Data

Updating nodes allows you to modify existing data in your graph.


Example: Change the Duration of "Inception" to 150 Minutes

We'll match the "Inception" movie node and update its duration property to 150 minutes.

MATCH (m:Movie {title: 'Inception'})
SET m.duration = 150
RETURN m

This query updates the duration of the "Inception" movie node and returns the updated node.


Example: Add a Genre Property to "Inception"

We can also add a new property to an existing node. Let's add a genre property to the "Inception" movie.

MATCH (m:Movie {title: 'Inception'})
SET m.genre = 'Science Fiction'
RETURN m

This query matches the "Inception" movie node and adds a new genre property with the value "Science Fiction." The updated node is then returned.


Deleting Data

Deleting nodes and relationships is necessary for maintaining and cleaning your graph database.


Example: Delete the "Inception" Movie Node

We'll delete the "Inception" movie node along with any relationships it has using the DETACH DELETE clause.

MATCH (m:Movie {title: 'Inception'})
DETACH DELETE m

This query matches the "Inception" movie node and deletes it, along with all its relationships.


Conclusion

In this demo, we've covered the fundamental CRUD operations in Cypher: creating nodes and relationships, reading data, updating nodes, and deleting nodes and relationships. These operations are essential for managing your graph data in Neo4j.

By mastering these CRUD operations, you'll be well-equipped to handle and manipulate your graph data efficiently. Happy querying!

8 views0 comments

Comments


bottom of page