Neo 4 J
Neo 4 J
Neo4j is the most famous database management system and it is also a NoSQL
database system. Neo4j is different from Mysql or MongoDB it has its own
features and it is designed to efficiently store and query highly interconnected
data that’s makes it special compared to other Database Management System.
Neo4j structure
Neo4j stores and present the data in the form of graph not in tabular format or not
in a Json format. Here the whole data is represented by nodes and there you can
create a relationship between nodes. That means the whole database collection
will look like a graph, that’s why it is making it unique from other database
management system.
What is a Graph Database?
A graph database uses graph theory to store, map, and query relationships. It
consists of nodes, edges, and properties, where:
Edges (or relationships) connect nodes and illustrate how entities are related.
This structure allows graph databases to model real-world scenarios more naturally
and intuitively than traditional relational databases.
The Neo4j has its own query language that called Cypher Language. It is similar to
SQL, remember one thing Neo4j does not work with tables, row or columns it
deals with nodes. It is more satisfied to see the data in a graph format rather than in
a table format.
Neo4j Syntax and Rules
Below is the syntax and explanation for the commands provided earlier:
Rules:
Node labels should start with an uppercase letter.
Properties should be defined as key-value pairs, with string values in quotes
(" ").
Multiple properties are separated by commas.
CREATE
...;
Example:
CREATE
CREATE (alias1)-[:RELATIONSHIP_TYPE]->(alias2);
CREATE (p1)-[:FRIEND]->(p2);
Rules:
Relationships are always directed.
Relationship type must be enclosed in [:RELATIONSHIP_TYPE].
MATCH is used to find existing nodes before creating the relationship.
Example:
Rules:
MATCH is used to locate the node.
SET adds or updates properties dynamically.
MATCH (alias1:Label1)-[alias2:RELATIONSHIP_TYPE]->(alias3:Label2)
Example:
Rules:
Properties on relationships are stored in the relationship object.
Use the relationship alias (r in the example) for referencing.
Example:
Rules:
Both nodes and the relationship are created in a single query.
No need for MATCH when creating new nodes.
RETURN n;
MATCH (alias1)-[alias2:RELATIONSHIP_TYPE]->(alias3)
Rules:
MATCH retrieves nodes or relationships.
RETURN specifies what to display.
DELETE alias;
Rules:
DELETE removes nodes without relationships.
DETACH DELETE removes a node along with all its relationships.
In the Neo4j to create node you will have to state CREATE statement. With the
help of cypher language it is easy to create nodes, properties and relation between
nodes.
CREATE
(Alice)-[:FRIEND]->(Bob),
(Alice)-[:COLLEAGUE]->(Charlie),
(Diana)-[:FRIEND]->(Alice);
1. Creating Nodes
3. Creating Relationships
Relationships in Neo4j are directional. You can use --> or <-- to define the
direction.
CREATE (p1)-[:FRIEND]->(p2);
CREATE (p1)-[:COLLEAGUE]->(p2);
CREATE (p3)-[:FRIEND]->(p1);
You can add properties to existing nodes using the SET command.
Example:
Example:
Example:
7. Viewing Data
MATCH (n)
RETURN n;
MATCH (p1)-[r]->(p2)
DELETE p;
DETACH DELETE p;
CREATE
(Alice)-[:FRIEND]->(Bob),
(Alice)-[:COLLEAGUE]->(Charlie),
(Diana)-[:FRIEND]->(Alice);