Neo4j Graph Database Practical:
Movie Database Example Using Cypher Queries
Graph databases are
becoming very popular for handling connected
data. One of the most powerful graph databases is Neo4j, which uses the Cypher query language to create and manage
graph structures.
In this tutorial, we will
learn how to create a Movie Database Graph
using Cypher queries. We will create nodes for movies, actors, directors, and
people, then establish relationships between them.
This practical example is
useful for computer engineering students,
database learners, and graph database beginners.
1. Create Nodes for Movies, Actors, Directors, and Persons
First, we create nodes
representing movies, actors, directors, and
people.
CREATE (:Movie28 {title: "Pushpa", release_year: 2010}),
(:Movie28 {title: "KGF", release_year: 2004}),
(:Movie28 {title: "ANIMAL", release_year: 1994}),
(:Actor28 {name: "Ranbir Kapoor"}),
(:Director28 {name: "Faran Akhatar"}),
(:Person28 {name: "Meet"}),
(:Person28 {name: "Parth"}),
(:Person28 {name: "Hit"}),
(:Person28 {name: "Harshil"});
This command creates
multiple nodes at once.
Node types used:
·
Movie28
·
Actor28
·
Director28
·
Person28
2. Create Actor–Movie Relationships
Next, we create a
relationship showing which actor acted in which movie.
MATCH (a:Actor28 {name: "Ranbir Kapoor"}), (m:Movie28 {title: "ANIMAL"})
CREATE (a)-[:ACTED_IN]->(m);
Another relationship
example:
MATCH (a:Actor28), (m:Movie28 {title: "Pushpa"})
WHERE a.name IN ["Ranbir Kapoor"]
CREATE (a)-[:ACTED_IN]->(m);
Relationship used:
ACTED_IN
3. Assign Director to a Movie
We can also link a
director with a movie.
MATCH (d:Director28 {name: "Faran Akhatar"}), (m:Movie28 {title: "ANIMAL"})
CREATE (d)-[:DIRECTED]->(m);
Relationship used:
DIRECTED
4. Create Relationships for People Liking Movies
Here we create a
relationship where a person likes a movie.
MATCH (p:Person28 {name: "Meet"}), (m:Movie28 {title: "ANIMAL"})
CREATE (p)-[:LIKES]->(m);
Relationship used:
LIKES
5. Create Ratings for Movies
We can also allow
people to rate movies.
MATCH (p:Person28), (m:Movie28)
WHERE p.name IN ["Parth", "Hit", "Harshil"] AND m.rating1 > 4
CREATE (p)-[:RATED {rating: m.rating1}]->(m);
Relationship used:
RATED
Property used:
rating
6. Retrieve Actors Who Acted in Movies Released
After 2000
This query finds actors
who acted in movies released after the year 2000.
MATCH (a:Actor28)-[:ACTED_IN]->(m:Movie28)
WHERE m.release_year > 2000
RETURN DISTINCT a.name;
7. Update a Movie's Release Year
Example: Update release
year of a movie.
MATCH (m:Movie28 {title: "The Matrix"})
SET m.release_year = 1999;
This modifies an
existing node property.
8. Delete a Relationship Between a Person and a
Movie
We can remove
relationships from the graph.
MATCH (p:Person28)-[r:LIKES]->(m:Movie28)
WHERE p.name = "Meet" AND m.title = "ANIMAL"
DELETE r;
9. Retrieve Directors of Movies with Rating
Higher Than 8
This query finds
directors of highly rated movies.
MATCH (d:Director28)-[:DIRECTED]->(m:Movie28)
WHERE m.rating > 8
RETURN DISTINCT d.name;
10. Create a New Actor Node
Example: Add a new
actor.
CREATE (:Actor28 {name: "Tom Hanks"});
Actor example: Tom Hanks
11. Find All Pairs of Actors Who Acted Together
This query finds actors
who worked in the same movie.
MATCH (a1:Actor28)-[:ACTED_IN]->(m:Movie28)<-[:ACTED_IN]-(a2:Actor28)
WHERE id(a1) < id(a2)
RETURN DISTINCT a1.name, a2.name;
12. Retrieve Users Who Rated a Movie Higher Than
4
MATCH (p:Person28)-[:RATED]->(m:Movie28)
WHERE m.rating > 4
RETURN DISTINCT p.name;
13. Connect Alice to a Movie with a Rating
Example: Add a rating
from a person to a movie.
MATCH (p:Person28 {name: "Alice"}), (m:Movie28 {title: "The Shawshank Redemption"})
CREATE (p)-[:RATED {rating: 5}]->(m);
Example movie: The Shawshank Redemption
14. Retrieve Movies Released Between 2010 and
2020
MATCH (m:Movie28)
WHERE m.release_year >= 2010 AND m.release_year <= 2020
RETURN m.title;
This query filters
movies within a specific release year range.
Conclusion
In this tutorial, we
learned how to use Neo4j and the Cypher query language to:
·
Create nodes
·
Create relationships
·
Update data
·
Delete relationships
·
Query graph data efficiently
Graph databases are
extremely useful for applications like:
·
Movie recommendation systems
·
Social networks
·
Fraud detection systems
·
Knowledge graphs
Learning graph
databases will be an important skill for modern
database developers and data engineers.
0 Comments