Getting started with Graph database using Neo4j
Tags: neo4j, graphdb, beginners
Published: 2022-11-04 12:20:45

A graph database stores information as relationships between entities and represents this data using nodes and edges. It allows you to easily store data and analyze the relationships among them.

Neo4j is a highly scalable, schema free graph database management system. It stores and displays data in the form of graph and represents data using nodes and edges and relationships. A major feature of Neo4j is creating interconnection between various entities (nodes). It provides a declarative query language known as Cypher (CQL) for querying data.

In this session, we will learn Neo4j basics and try basic clauses such as CREATE, MATCH, WHERE, RETURN, SET, DELETE etc.

## Getting Started
Relational database stores data in the form rows and columns and relations are mentioned using foreign key constraint. This becomes complicated when it involves a lot of indirect relationships between the data. The connections between the data becomes equally important as the data itself.

![neo4j graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkba06bdfhv7f8ogdqik.png)
Image credits: neo4j.com

Graph database uses graph structures with nodes, relationships and properties to store and visualize data.

The fundamental components of a graph database are:

- **Nodes** — the entities in a graph.
- **Relationships** — the connections between the entities, similar to foreign keys in a relational database.
- **Labels** — attributes that group similar nodes together.
- **Properties** — key/value pairs stored within nodes or relationships.

![neo4j](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2h3hit08jv3thv40h5n.png)
![neo4j](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/106m19o1yw6cgl4qyfig.png)
Image credits: neo4j.com

In relational databases, we can store relationships as tables and navigate them using JOIN operations or cross-lookups which is time consuming, not cost-effective method. Whereas in graph databases, relationships are stored as data elements (the nodes) in a much more flexible format. This enables the user to navigate deep hierarchies, find hidden connections between distant nodes, and discover inter-relationships between nodes.

![neo4j](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnexpctf2x6ac9bbzldw.png)

## Neo4j
Neo4j is a schema free graph database management system. It is a NoSQL based ACID complaint transactional database. It uses Cypher Query Language (CQL) for creating, modifying, interconnecting and deleting data (nodes).

### Features
- **Flexible data model** − Neo4j provides a flexible, simple and powerful data model, which can be easily changed according to the requirements.
- **ACID properties** − Neo4j supports ACID (Atomicity, Consistency, Isolation, and Durability) rules unlike other databases.
- **Scalability and reliability** − You can scale the database by increasing the number of reads/writes, and the volume without affecting the query processing speed or data integrity.
- **Cypher Query Language** − Neo4j provides a declarative query language known as Cypher. It can be used to create and retrieve relations between data without using complex queries like JOINS.
- **Built-in web application** − Neo4j provides a built-in Neo4j Browser web application to create and query your graph data.
- **Drivers** − Neo4j supports REST API to work with programming languages and Java Script to work with UI MVC frameworks.

### Installation
Install Neo4j in your machine using the following steps:

- Download and install Java JDK 1.8 from the [official site](https://www.oracle.com/in/java/technologies/javase/javase8-archive-downloads.html).
- Download the installer from the [official site](https://neo4j.com/download-center/). For which, open the link [Neo4j Download Center — Neo4j Graph Data Platform](https://neo4j.com/download-center/) and click on the Download Neo4j Desktop button.
![download1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ahljo871h8psn6nxqyo.png)
- Provide the necessary information in the next page and click on the Download Desktop button.
![download2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwq95hvp6bgkbyn9be6h.png)
- Copy the Neo4j activation key from the final page, it is required in the software registration step.
![download3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/es345kh73vocnavy8pen.png)
- Double click on the installer and select all users option from the window. Provide the destination folder path and click on the Install button to start the installation process.

![download4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsb2i973wtnn18lsyib9.png)
![download5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/103kcwwuuks2kdzp78gc.png)
- Run the Neo4j Desktop application. Accept the terms and conditions, provide the data path (keep as defaults), provide name, email and software key (neo4j activation key) in the next window to complete the neo4j initial configuration.
![installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6svqbqel373ltknem2ub.png)

- You can see the following window after the successful configuration.
![config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjmcmyrmhnb6x141lrkw.png)

## Understand the IDE
The following is the screenshot of the Neo4j Desktop in the first use. It provides an Example Project and the basic CQL commands on the about-movies.neo4j-browser-guide file to introduce the CQL commands.
![understand the ide](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t6j5gehghx8k2q9wltsg.png)

Let’s understand the basic Neo4j Desktop components,

- **Project Hierarchy Window** — It shows the project names, the user can switch to another project by clicking on the project folder name. The user can delete the project if required.
- **Active DBMS** — It shows the currently running DBMS name, option to open the Neo4j browser and Stop button to terminate the DBMS.
- **Project Info Window** — It shows the project related information such as project name and DBMS info. It provides an Add button to add new DBMS locally, via remote connection and a file, the Stop button can be used to terminate the DB, the Open button to open the CQL window and the Refresh button to refresh the database.
- **Detail Window** — It provides information such as DBMS name, version, HTTP port, Bolt port and so on.
- **File Window** — It shows the File list which contains the files associated with the project.

## Explore Example Project
As a new user, you will get an example project with movie DBMS containing details of Movies and Actors. You can click on the Start button to start the database if it is not automatically started.
![explore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lzcx3224686bbf6vgyt1.png)

- Click on the Open button located with the browser guide file in the File window. It will open a new neo4j browser like below,
![neo4j](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82yzy112i2usetswa206.png)

- Click on the Next arrow button to see more information on the Movie database. You can also execute the CQL commands listed in the description. For which, click on the command. It will paste the command into the input box.
![neo4j](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xc8cwanjmxch3oqqmjp0.png)

- Click on the Play button to execute the command. You can use the http://localhost:7474/browser/ link to open neo4j browser in a browser to execute the commands.
![neo4j](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/as46pwxz0czeefkp7bf1.png)

- You can click on the node to expand and view the properties. You can visualize the CQL query result as Graph, Table, Text and Code.
![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6y9k9mcapagurhk1i3l.jpg)

## Understand the Neo4j CQL Clauses
Cypher is a graph query language (CQL) used in Neo4j that enables you to create, modify, interconnect, delete and retrieve data from the graph. It provides a way to visualize the data in the form of nodes and relationships. It uses an ASCII-art type of syntax where `(nodes)-[:ARE_CONNECTED_TO]->(otherNodes)` using rounded brackets for circular (nodes), and `-[:ARROWS]->` for relationships.

**Read clauses:**
![read](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2gmxahiou1b05brsze2.png)

**Write clauses:**
![write](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zk1mhen8mmzy6jkbfdlx.png)

**General Clauses:**
![general](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tzhpcg9u0otnfwfmok3.png)

## Create your first Neo4j project
In this section, we will create a Neo4j project for managing person related information. Let’s create the below graph using Neo4j.

![project image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbl3heq06ynm6zy8gfg4.png)

### Create new project
Follow the steps to create a new project in Neo4j.

- Click New button and select Create project option from the project window.
![project1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z907gtotvkxrseo5kg44.png)

- Click the edit icon to provide a valid project name.
![project2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21diqwp3fjpu06ycvhxs.png)

- Click Add button and select Local DBMS option.
![project3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8714y29c6udrs7suw2c.png)

Provide name, password and version of the DBMS. Click the Create button to create the project.
![project4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5hr8n724l2d5d5oi511k.png)

- Start the project by clicking on the Start button in the DBMS list.
![project5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tphm6rjtya5yxxy67ene.png)

- Click the Open button to switch to the Neo4j browser window.
![project6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aviv53t33o1g68l5fpzj.png)

### Write CQL commands
Let’s begin with the person node creation.

- Execute the following statements to create 4 person nodes.

```
CREATE (a:Person {name:'Ajith', born: 'Apr 10, 1996', linkedin:'@ajith'}) RETURN a
CREATE (a:Person {name:'Ishani', born: 'Mar 8, 1998', linkedin:'@ishani'}) RETURN a
CREATE (a:Person {name:'Gowri', born: 'Oct 16, 1997', linkedin:'@gowri'}) RETURN a
CREATE (a:Person {name:'Vishnu', born: 'Dec 12, 1994', linkedin:'@codemaker2015'}) RETURN a
```
![cql commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxe45fn8yaujr4vev8zl.jpg)

- Execute the following statements to create 2 game nodes.

```
CREATE (a:Sports {name:'Chess'}) RETURN a
CREATE (a:Sports {name:'Cricket'}) RETURN a
```

![cql commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qsna08bs0ir3we6a4mva.png)
![cql commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyzzsuysktgyik04ut3f.png)

- Execute the following statement to create a company node.

```
CREATE (a:Company {name:'ABC Company', location: "Ernakulam"}) RETURN a
```
![company](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhvoiah5ew75ssm15sx2.png)

- Execute the following statement to create an institution node.

```
CREATE (a:Institution {name:'XYZ College', location: "Kochi"}) RETURN a
```
![institution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yd6niiamca5vdd9m08mm.png)

Let’s list the node details using the MATCH clause.

```
MATCH (n) RETURN n LIMIT 25
```
![match](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdtiaygznm27o43z2898.png)

Get the node details of the person node, ‘Vishnu’, by clicking on the node.

```
MATCH (a {name: 'Vishnu'}) RETURN a
```
![match](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qo0m5oqdiuisousbk69.png)

Now, we can create relationship between the nodes as shown in the figure (fig: person management).

- Execute the following statement to create a PLAYS relationship between person nodes and game nodes.

```
MATCH (a:Person), (b:Sports) WHERE (a.name = 'Vishnu' OR a.name = 'Ajith') AND b.name = 'Cricket' CREATE (a)-[r:PLAYS]->(b)
MATCH (a:Person), (b:Sports) WHERE (a.name = 'Vishnu' OR a.name = 'Gowri' OR a.name = 'Ishani') AND b.name = 'Chess' CREATE (a)-[r:PLAYS]->(b)
```
![cql commands](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhizuccdo3ls8abtzkfq.png)

- Create a relationship between institution and person nodes using the following statement.

```
MATCH (a:Person), (b:Institution) WHERE (a.name = 'Vishnu' OR a.name = 'Ishani') AND b.name = 'XYZ College' CREATE (a)-[r:ACADEMICS]->(b)
```
![relationship](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4981go4r64julrz1c41r.png)

- Create a relationship between company and person nodes using the following statement.

```
MATCH (a:Person), (b:Company) WHERE (a.name = 'Vishnu' OR a.name = 'Gowri') AND b.name = 'ABC Company' CREATE (a)-[r:WORKS]->(b)

```
![relationships](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe7j1o1vde07piwscke3.png)

- View the nodes and relationships using the following statement.

```
MATCH (n) RETURN n
```
![match](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwdf6mfyluvcx9yudbti.png)

- Delete the PLAYS relationship between Ajith and Cricket using the following command.

```
MATCH (n {name: 'Ajith'})-[r:PLAYS]->() DELETE r
```
![delete1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jave404ehwjron6fntlu.png)
![delete2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jhop3jan70efcpho8ut.png)

- Delete a node with property name as Ajith.

```
MATCH (n {name: 'Ajith'}) DELETE n
```

![delete3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dowmek9vxk10akzn6ecw.png)
![delete4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcb6du213jxutsnyrn60.png)

Thanks for reading this article.

Thanks Gowri M Bhatt for reviewing the content.

If you enjoyed this article, please click on the heart button ♥ and share to help others find it!

The article is also available on [Medium](https://blog.devgenius.io/getting-started-with-graph-database-using-neo4j-d549d74cc94b).

The full source code for this tutorial can be found here,

[GitHub - codemaker2015/neo4j-examples: Neo4j example project codes](https://github.com/codemaker2015/neo4j-examples)

Useful Links:

- [What is a Graph Database? | www.oracle.com](https://www.oracle.com/in/autonomous-database/what-is-graph-database/)

- [Getting Started with Neo4j - Developer Guides | neo4j.com](https://neo4j.com/developer/get-started/)

- [Neo4j Desktop User Interface Guide - Developer Guides | neo4j.com](https://neo4j.com/developer/neo4j-desktop/)

- [Neo4j Examples | github.com](https://github.com/neo4j-examples)

- [GitHub - neo4j-graph-examples/recommendations: Neo4j Graph Example Movies Recommendation | github.com](https://github.com/neo4j-graph-examples/recommendations)