What is Graph Database?
A graph database stores nodes and relationships instead of tables, or documents.
In Neo4j, information is organized as nodes, relationships, and properties.
Building blocks of the property graph model
Nodes are the entities in the graph.
Nodes can be tagged with labels, representing their different roles in your domain.
(For example, Person).
Nodes can hold any number of key-value pairs, or properties. (For example, name)
Node labels may also attach metadata (such as index or constraint information) to
certain nodes.
Relationships provide directed, named, connections between two node entities
(e.g. Person LOVES Person).
Relationships always have a direction, a type, a start node, and an end node, and
they can have properties, just like nodes.
Nodes can have any number or type of relationships without sacrificing
performance.
Although relationships are always directed, they can be navigated efficiently in any
direction.
If you’d like to learn more about any of these, you can read more about Graph
Data Modeling.
In Neo4j, information is organized as nodes, relationships, and properties.
Building blocks of the property graph model
Nodes are the entities in the graph.
Nodes can be tagged with labels, representing their different roles in your domain.
(For example, Person).
Nodes can hold any number of key-value pairs, or properties. (For example, name)
Node labels may also attach metadata (such as index or constraint information) to
certain nodes.
Relationships provide directed, named, connections between two node entities
(e.g. Person LOVES Person).
Relationships always have a direction, a type, a start node, and an end node, and
they can have properties, just like nodes.
Nodes can have any number or type of relationships without sacrificing
performance.
Although relationships are always directed, they can be navigated efficiently in any
direction.
How Does a Graph Database Differ from a Relational Database?
The main difference is the way relationships between entities
are stored. In a graph database, relationships are stored at the
individual record level, while a relational database uses
predefined structures, a.k.a. table definitions.
Query:
Query on the basis of realtionship
MATCH (player:Player) -[:PLAYS_FAR] →(team:Team)
WHERE team.name=’LA LAKERS’ OR team.name=’another’
RETURN player
(PLAYS_FOR is here relationship between node player and
teams and relationship has also properties)
Query on the basis of relationship property salary
MATCH (player:Player) -[contract:PLAYS_FOR] →[team:Team]
WHERE contract.salary >3500000
RETURN player,team
we want to find the salary of teammate of a player ali
MATCH (ali:Player {name:’ali’}) -
[:teammates]→[teammate:Team]
MATCH (teammate) -[contract:PLAYS_FOR] → (team:TEAM)
Where contract.salary >400000
return teammate
AGGREGATING:
MATCH (player:Player) -[gamePlayed:PLAYED_AGAINST] →
(team :Team)
return player.name, COUNT(gamePLayed)
TO DELETE A NODE: (we have to delete relationships first)
MATCH (n {name :’ali’}) DETACH DELETE n
TO DELETE ONLY ONE RELATIONSHIP
MATCH (n {name:’naqash’}) -[rel:PLAYS_FOR] → (:TEAM)
DELETE rel
now to create a node:
CREATE (:PLAYER{name:'Babar',age:26}) -[:PLAYS_FOR
{salaray:340000}] -> (:TEAM {name :'Kings'})