Neo4j and Cypher Query

Neo4J

·

2 min read

Neo4j and Cypher Query

In this blog, we are going to work on Neo4j which is a graph database management system and transactional database with native graph storage and processing. So we use cipher queries to query the Neo4j database, so cipher queries are somewhat similar to traditional SQL-based queries (Cypher vs SQL). Through Neo4j Desktop you can create graphical visualization of the database by creating nodes and their relationship, you can set labels, properties, and relationships to these nodes. Some of the graphical visualization I recently worked on...

2.png

3.png

Why should we prefer the graph database (Neo4j) over structured query NEO4j vs SQL) as query processing is way faster in graph database than structured query and we can use cipher query to get relational data results in form of the table because graph database does not work on rows and columns but rather works on Indexes and constraints.

5.png Now we will directly jump on to cipher queries and get an overview of how to make basic node, relationship, and their properties:

  1. For creating a node we use the "CREATE" command and we can also use "MATCH" what it does is it will find if the node exists if doesn't then the match command will create a node. Now forming a relationship between this node we use the "MERGE" command now the relationship between the node cannot have a null label or cannot be unidirectional, the relationship has to be in a direction and node can have a relationship pointing toward itself or other nodes

CREATE (p:Person { name: "Tarun" }); MATCH (p:Person { name: "Shahrukh" }) MERGE (p)-[:knows]->(p)

2.Now "DELETE" command and "DETACH" we cannot directly delete a node with the relationship you have to first detach the relationship with the other node and then delete the node.

MATCH (p:Person{name: "Tarun"}) DETACH DELETE p

  1. We can also load data from an external file in the neo4j database like a CSV format file through the LOAD CSV FROM command.

LOAD CSV FROM '{csv-dir}/artists.csv' AS line

CREATE (:Artist { name: line[1], year: toInteger(line[2])})

  1. Now "WHERE" clause wherein we can set conditions on node properties.

    match (n:DateNode)where n.Name= "05-02-2019" return n;

  1. creating properties of a node with label example.

create (Asianpaint:company{name:"AsianPaint", country:"India",city:"Mumbai"});