Introduction to Neo4j
Published in
4 min readJan 29, 2021
This article focuses on basic overview of Neo4j and Graph Database.
What is Graph database?
- Graph database uses graph structures with nodes, edges and properties to represent and store data. The graph relates data items to a collection of nodes and edges, where edges represent relationships between nodes. Graph database is designed to treat the relationships between data as important as the data itself.
Why Graph database?
- Relational database uses ledger style structure and stores data in the form rows and columns. The relation between two tables is mentioned using foreign key constraint. This becomes complex in case of indirect relationship between lots of data because of many joins and normalization and it create data redundancy.
- In such cases, where relationships between data items has much more importance, graph databases becomes more powerful.
- Graph database answers the questions you didn’t expected. Suppose you want to know — how many people who bought a TV of particular company, lives in a particular city, used the offer coupon in yesterday’s paper? If you are using relational database, this question will take a lot of time and other resources to answer but with graph database it becomes much simpler case with available data.
Property Graph Model
- Nodes are the entities in graph. They can hold any number of attributes (key/value pairs) called properties. Nodes can be tagged labels, representing their different roles. Node labels also serve to attach metadata to certain nodes.
- Relationships provide directed, named, semantically-relevant connections between two node entities. A relationship always has a type, a direction, a start node and an end node. Relationships can also have properties.
What is Neo4j?
- Neo4j (Network Exploration and Optimization 4 Java) is a graph database management system. It is highly scalable, schema free (NoSQL) and supports ACID rules. Neo4j follows property graph model to store and manage its data.
- Neo4j provides a powerful declarative language known as Cypher. Cypher is human-friendly query language that uses ASCII-Art to represent visual graph patterns for finding or updating data in Neo4j.
Now we will look into basic CRUD operations that can be performed on data using Neo4j CQL.
CREATE
CREATE (node) — this will create a single node.CREATE (node1), (node2) — this will create multiple nodes.CREATE (node:label) — this will create a node with a label.CREATE (node:label {key: value}) — this will create node with given label and properties.CREATE (node1:label1)-[:RelationshipType]->(node2:label2) — this will first create two new nodes and than create a new relationship between them. If we need to create a relationship between existing node than first we need to find the nodes using MATCH clause and create a relationship using CREATE clause.CREATE (node1)-[label:relationshipType {properties}]->(node) — this will create a relationship with given label and properties.CREATE p = (Node1:Label1 {properties}) -[:RelationType]-> (Node2:Label2 {properties}) -[:RelationType]-> (Node3:Label3 {properties}) — this will create a complete path between nodes according to the relationship between them.
MATCH, WHERE, RETURN
MATCH (n) RETURN n — this will return all the nodesMATCH (n:Label) RETURN n — this will return all the nodes with a specific labelMATCH (node:label)<-[:Relationship]-(n) RETURN n - this will retrieve nodes based on the specified relationMATCH (node) WHERE node.property = "value" RETURN node - this will find a node whose property matches with a specified valueMATCH (node) WHERE node.name = "abc" AND node.city = "xyz" RETURN node - this will match multiple condition to retrieve a nodeMATCH (node) WHERE node-[:LIVES_IN]->(node2:label) RETURN node - this retrieve the node based on the relation
DELETE, REMOVE
MATCH (n) DETACH DELETE n - this will delete all the nodes and relationships in the databaseMATCH (node:label {properties}) DETACH DELETE node - this will delete a particular node with specified label and propertiesMATCH (node:label{properties}) REMOVE node.property - this will remove the specified property of a nodeMATCH (node:label{properties}) REMOVE node:label - this will remove the label of a node
SET, MERGE
MATCH (node:label{properties}) SET node.property = value - create a new node property or update the existing propertyMATCH (node:label {properties}) node.property = NULL - removes a property from a nodeMATCH (node {properties}) SET node:label - set label to a existing nodeMERGE (node:label) - first it will check whether "node" with given label exists, if not it will create a new nodeMERGE (node:label {properties}) - it searches for the specified node with given earphones and label, if doesn't exists new node will be createdMERGE (node:label {properties}) ON CREATE SET property.isCreated ="true" ON MATCH SET property.isFound ="true" - if the specified node is matched then "isFound" property will be set true and if it doesn't exists then new node will be created and "isCreated" property will be set trueMATCH (n1), (n2) WHERE n1.name="abc" AND n2.title="xyz" MERGE (n1)-[:DIRECTED]->(n2) - this will search the specified nodes and merge a "DIRECTED" relationship between them
Summary
In this article we discussed basics of Graph Database and Neo4j and have gone through the basic CRUD operations using Cypher.
References
[1] Neo4j Developer Guide,
https://neo4j.com/developer/get-started/