Neo4j with Cypher Query Language (CQL)

Neo4J may be queried using Cypher Query Language. CQL is a declarative pattern matching language and follows SQL-like syntaxes. CQL syntaxes are relatively simple and are in human readable format.

Neo4j CQL clauses

Table 2

(Table 2: CQL Read Clauses)

Table 3

(Table 3: CQL Write Clauses)

Table 4

(Table 4: CQL General Clauses)

Table 5

(Table 5: CQL Functions)

Neo4j CQL Data Types

There are some data types which are used in CQL such as Java data types. When defining properties, you will need to use these data types.

Table 6

(Table 6: CQL Data Types and uses)

CQL operators which are used as filters usually with ‘where’ clauses are given below:

Here is an example:

where n.age = 35 
Table 7

(Table 7: CQL Operator types)

fig 9

CRUD Operations

This section will help you to understand the basic Create, Read, Update and Delete operations using CQL on Neo4j.

Create

Creating a node:

          $ CREATE (n)
fig 18

(Fig 18: Creating a node using CQL)

Creating a Node with a Label:

          $ CREATE (node1: Test)
fig 19

(Fig 19: Creating a Node with a Label using CQL language on Neo4J)

Creating multiple Nodes with unique Labels simultaneously:

          $ CREATE (node1: test), (node2: Test2), (node: Test3)
fig 20

(Fig 20: Using CQL to create multiple Nodes with unique Labels simultaneously)

Using the verification clause ‘RETURN’ to see results:

         $ MATCH (n:Employee) RETURN n LIMIT 25

fig 21

(Fig 21: Using CQL commands to query a result)

Creating Nodes with Properties:

         $ CREATE (node1:Test {nodeId: 2, nodeName: ‘sample’, nodeDescription: ‘testing’})return node1

fig 22

(Fig 22: creating Nodes with Properties using CQL)

Setting Properties when creating:

         $ CREATE (node1: Test) set node1.name-’test’ return node1
Fig 22a-1

(Fig 23: setting Properties when creating Nodes on Neo4j using CQL)

Creating a Relationship:

         $ CREATE (emp:Employee), (pro:Project) ,(emp)-(ew:EMP_WORKS_FOR_PRO)->(pro) return emp, pro
fig 24a

(Fig 24: creating a Relationship between Nodes using CQL on Neo4j)

Read operations

The main read operation is the ‘MATCH’ clause which is the same as ‘SELECT’ with inner joins

Query to return employees who work for projects:

         $ MATCH (emp:Employee)-(ew:EMP_WORKS_FOR_PRO) return emp, pro

fig 25a

(Fig 25: Using a CQL query to view employees who work in Projects in Neo4j)

 

Matching a specific Node:

         $ MATCH (emp:Employee{name:’Namal’}) return emp

fig 26a

(Fig 26: Using CQL to view a specific Node in Neo4j)

Update operations

Values of Properties can be updated using the SET operator

Updating a given Property of a given Node:

         $ MATCH (emp:Employee{name:’Namal’}) set emp.name=’amal’ return emp

fig 27a

(Fig 27: Updating a Node using CQL on Neo4j)

Delete operations

Deleting a Node which has no relationship:

         $ MATCH (n:Test {nodeId: 2}) DELETE n

fig 28a

(Fig 28: Deleting a non related Node using CQL on Neo4j)

Deleting a relationship:

        $ MATCH (emp:Employee)-[ew:EMP_WORKS_FOR_PRO]

fig 29a

(Fig 29: Deleting a Relationship using CQL)

To delete a Node with is connected with another Node use the ‘DETACH DELETE’ command.

Summary

In this article we have attempted to explain the fundamentals of Neo4j to provide readers with an understanding to start practising with Neo4j. We hope this has been useful and if you have any questions let us know in the comments section.

Also, check out these links for more information about Neo4j:

https://neo4j.com/developer/example-project/

https://neo4j.com/news/facebookgroup-demo/