Graph
Databases
The problem
with
Relational
databases
• We normalize data to manage redundancy.
• Then there will be many tables. When
querying data we have to use more joins.
• When database have more data queries
take more time to execute.
• We use indexes to minimize this time.
(Indexed adjacency).
A Solution
• Nosql databases was invented.
• But there was a tradeoff.
• ACID was not supported for nosql.
Graph
Databases
• A graph database is any storage system
that uses graph structures with nodes and
edges, to represent and store data.
• Query using index free adjacency.
Therefore query is faster.
Relational Databases Vs Graph Databases
Relational Databases Vs Graph Databases
Neo4j
• A graph database.
• Fully ACID compliant.
• Uses Cypher query
language(CQL).
CRUD
• Create a node
• CREATE (:Person
{id:"P01",name:"John",age:"20"});
• Retrieve a node
• MATCH (p:Person {id:"P01"}) RETURN (p)
• Update node property
• MATCH (p:Person {id:"P01"}) SET
p.age="25";
• Delete a node
• MATCH (p:Person) WHERE id="P001"
DETACH DELETE (p);
Relationships
• Create relationship between existing
nodes.
• MATCH
(p:Person{id:"P01"}),(c:Car{id:"C01
"}) CREATE (p) -[:HAS]-> (c);
MERGE Vs CREATE
MERGE (c:Car{id:"C01"}) MERGE
(p:Person{id:"P02",name:"doe"}) -[:OWNS]-
> (c);
What is the correct query to
retrieve P01's car?
1. MATCH(c:Car) -[:OWNS]-> (p:Person{id:"P01"})
RETURN c;
2. MATCH (c:Car) <-[:OWNS]- (p:Person{id:"P01"})
RETURN c;
3. MATCH (c:Car) -[:OWNS]- (p:Person{id:"P01"})
RETURN c
4. MATCH (c:Car) -[]- (p:Person{id:"P01"}) RETURN c
5. MATCH(c:Car) -- (p:Person{id:"P01"}) RETURN c
6. MATCH (c:Car) <-- (p:Person{id:"P01"}) RETURN c
Constraints
Create
CREATE CONTRAINTS ON (p:PERSON) ASSERT
p.name IS UNIQUE;
Drop
DROP CONSTRAINT ON (p:Person) ASSERT
p.propertyName IS UNIQUE;
Aggregating functions
Owners count of the car(P01)
match (c:Car {id:"C01"}) <-
[:OWNS]- (p) return count(p) as
ownersCount;
Find the car that owns by P01 and P02
• MATCH (:Person{id:"P01"}) -[:OWNS]-> (c) <-
[:OWNS]- (:Person{id:"P02"}) RETURN c;
• MATCH (p1:Person{id:"P01"}),(p2:Person{id:"P02"})
MATCH (p1) -[:OWNS]-> (c) <-[:OWNS]- (p2) return
c
Find the car that owns by P01,P02 and P03
MATCH
(p3:Person{id:"P03"}),(p1:Person{id:"P01"}),(p2
:Person{id:"P02"})
MATCH (p1) -[:OWNS]-> (c:Car) <-[:OWNS]-
(p2)
MATCH (c) <-[:OWNS]- (p2) RETURN (c);
Regular Expressions =~
MATCH (p:Person) WHERE p.name =~"A.+"
RETURN p;

Graph databases

  • 1.
  • 2.
    The problem with Relational databases • Wenormalize data to manage redundancy. • Then there will be many tables. When querying data we have to use more joins. • When database have more data queries take more time to execute. • We use indexes to minimize this time. (Indexed adjacency).
  • 3.
    A Solution • Nosqldatabases was invented. • But there was a tradeoff. • ACID was not supported for nosql.
  • 4.
    Graph Databases • A graphdatabase is any storage system that uses graph structures with nodes and edges, to represent and store data. • Query using index free adjacency. Therefore query is faster.
  • 5.
    Relational Databases VsGraph Databases
  • 6.
    Relational Databases VsGraph Databases
  • 8.
    Neo4j • A graphdatabase. • Fully ACID compliant. • Uses Cypher query language(CQL).
  • 9.
    CRUD • Create anode • CREATE (:Person {id:"P01",name:"John",age:"20"}); • Retrieve a node • MATCH (p:Person {id:"P01"}) RETURN (p) • Update node property • MATCH (p:Person {id:"P01"}) SET p.age="25"; • Delete a node • MATCH (p:Person) WHERE id="P001" DETACH DELETE (p);
  • 10.
    Relationships • Create relationshipbetween existing nodes. • MATCH (p:Person{id:"P01"}),(c:Car{id:"C01 "}) CREATE (p) -[:HAS]-> (c);
  • 11.
    MERGE Vs CREATE MERGE(c:Car{id:"C01"}) MERGE (p:Person{id:"P02",name:"doe"}) -[:OWNS]- > (c);
  • 12.
    What is thecorrect query to retrieve P01's car? 1. MATCH(c:Car) -[:OWNS]-> (p:Person{id:"P01"}) RETURN c; 2. MATCH (c:Car) <-[:OWNS]- (p:Person{id:"P01"}) RETURN c; 3. MATCH (c:Car) -[:OWNS]- (p:Person{id:"P01"}) RETURN c 4. MATCH (c:Car) -[]- (p:Person{id:"P01"}) RETURN c 5. MATCH(c:Car) -- (p:Person{id:"P01"}) RETURN c 6. MATCH (c:Car) <-- (p:Person{id:"P01"}) RETURN c
  • 13.
    Constraints Create CREATE CONTRAINTS ON(p:PERSON) ASSERT p.name IS UNIQUE; Drop DROP CONSTRAINT ON (p:Person) ASSERT p.propertyName IS UNIQUE;
  • 14.
    Aggregating functions Owners countof the car(P01) match (c:Car {id:"C01"}) <- [:OWNS]- (p) return count(p) as ownersCount;
  • 15.
    Find the carthat owns by P01 and P02 • MATCH (:Person{id:"P01"}) -[:OWNS]-> (c) <- [:OWNS]- (:Person{id:"P02"}) RETURN c; • MATCH (p1:Person{id:"P01"}),(p2:Person{id:"P02"}) MATCH (p1) -[:OWNS]-> (c) <-[:OWNS]- (p2) return c
  • 16.
    Find the carthat owns by P01,P02 and P03 MATCH (p3:Person{id:"P03"}),(p1:Person{id:"P01"}),(p2 :Person{id:"P02"}) MATCH (p1) -[:OWNS]-> (c:Car) <-[:OWNS]- (p2) MATCH (c) <-[:OWNS]- (p2) RETURN (c);
  • 17.
    Regular Expressions =~ MATCH(p:Person) WHERE p.name =~"A.+" RETURN p;