visit
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)
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.
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.
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“ .