Databases: the warehouse of our data. if you have used databases before, it will likely be a relational database, or a NoSQL database. Every use case has the data structure and database most suited for it. Today I would like to talk about the graph database. NEO4J to be precise.
I have always heard about graph databases and assumed it was a complex system. That made me postpone learning it. Thanks to an interview requirement, I am forced to learn and apply it. It is a different paradigm, but not as complex as I thought it would. Let's discuss some core concepts about NEO4J:
NODES
Everything is a node. Well not everything. The way rows are added in relational database, or documents in NoSQL database, we add nodes to graph databases. a node represents a single entity of a Neo4j database. a way to recognize a node in a query is with “()”. anything between the “(“ and “)“ is to classify the node. let write a query that helps create a node in the database.
CREATE (n)
The “n” in the query like a variable name for that node to use in the query. we not current using it. so we can also do
CREATE ()
The same way we have tables in a relational database, we also have LABELS. We use the labels to categories nodes to a group. This grouping enable us structure our data, and fetch nodes belonging only to a particular group.
Let see a snippet to create a labelled node.
CREATE (:School)
With the above snippet, we created a node and labeled it “School”. we can also use the variable names which can result in the following query
CREATE (s:School) RETURN s
This query is the same as the one above it. the only difference is that it uses the variable name to return the node after it's created.
Another special thing about the Neo4j, is that we can assign many label to a node, making it polymorphic. let take an example.
CREATE (s:School :Building) RETURN s
This query will create one node and assign it with two labels “School” and “Building”.
Each node in Neo4j has properties, the same way relational database has columns. You can set the properties or leave them empty. The query below shows how to create an node with properties
CREATE (s:School { name: 'University Of Nigeria', location: 'Enugu' }) RETURN s
The following nodes has two properties, “name” and “location“. you can add more properties as you choose.
Relationship is how each node relates to each other. It’s kinda like the way tables have relationships in a relational database. But in graph database, relationships are with each nodes. “[]” is a way to recognize relationship in a query. Let denote a relationship below
[:GOES_TO]
The above denotes a relationship with a label of “GOES TO“.
Now lets write a query that create a relationship between two nodes. To make the query complete, let create two nodes and then create a relationship between them.
CREATE (school:SCHOOL { name: 'UNILAG' })
CREATE (student:STUDENT { name: 'John' })
CREATE (student) -[:GOES TO] -> (school)
The query above, creates two nodes with labels “SCHOOL“ and “STUDENT“ .
And then goes ahead to create a relationship with label “GOES_TO“. One thing to also remember is that, relationships are always directed. this brings us to the usage of “-“ and “→” in the query. I hope from the query it seems self explanatory, but if it is not, the relationship is from the student node to the school node.
The graph database is an interesting data paradigm where there is no table or collection. each node is an independent entity that can be classified to as many label as you want. it gives the freedom to structure that suites your design.