Experience

  1. Home
  2. Experience

Graph Database vs RDBMS

Graph Databases and RDBMS (Relational Database Management System) have different approaches to data storage and manipulation. Graph Database, with its graphical representation, provides advantages in more natural data modeling, schema flexibility, efficient queries, and graph search analysis.

Graph Database

1. Create User Nodes

Cypher
CREATE (:User {id: 1, name: 'Alice'})
CREATE (:User {id: 2, name: 'Bob'})
CREATE (:User {id: 3, name: 'Charlie'})
CREATE (:User {id: 4, name: 'David'})

This is the query to create user nodes in the graph database. Each CREATE statement creates one node with a User label and id and name properties.

2. Add friend relationships

Cypher
MATCH (alice:User {name: 'Alice'}), (bob:User {name: 'Bob'})
CREATE (alice)-[:FRIENDS]->(bob)

MATCH (bob:User {name: 'Bob'}), (charlie:User {name: 'Charlie'})
CREATE (bob)-[:FRIENDS]->(charlie)

MATCH (bob:User {name: 'Bob'}), (david:User {name: 'David'})
CREATE (bob)-[:FRIENDS]->(david)

MATCH (charlie:User {name: 'Charlie'}), (david:User {name: 'David'})
CREATE (charlie)-[:FRIENDS]->(david)

This is the query to create a friend relationship between user nodes. Each MATCH is used to find nodes with certain conditions, and then CREATE is used to create an edge with the label FRIENDS connecting the two user nodes.

3. Show Data

Cypher
MATCH (n) RETURN n

This is a query to display all nodes and edges in the database. MATCH (n) matches all nodes and edges, and RETURN n returns all matching results.

Overall, this Cypher code creates four user nodes and adds friend relationships between them, then displays all the nodes and edges in the database.

RDBMS

1. Create a User Table and Insert Data

SQL
CREATE TABLE Users (
id INT PRIMARY KEY,
name VARCHAR(255)
);

INSERT INTO Users (id, name) VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie'), (4, 'David');

This is the query to create a Users table with two columns: id as the PRIMARY KEY and name as a character column with a maximum length limit of 255 characters. There is also a query to insert data into the Users table. Four rows of data are inserted with corresponding id and name values.

2. Create a Friend Relationship Table

SQL
CREATE TABLE Friends (
user_id1 INT,
user_name1 VARCHAR(255),
user_id2 INT,
user_name2 VARCHAR(255),
FOREIGN KEY (user_id1) REFERENCES Users(id),
FOREIGN KEY (user_id2) REFERENCES Users(id),
PRIMARY KEY (user_id1, user_id2)
);

INSERT INTO Friends (user_id1, user_name1, user_id2, user_name2) VALUES
(1, 'Alice', 2, 'Bob'),
(2, 'Bob', 3, 'Charlie'),
(2, 'Bob', 4, 'David'),
(3, 'Charlie', 4, 'David');

This is a query to create a Friends table with four columns: two pairs of user_id and user_name to represent the two users who are friends, and two FOREIGN KEYS that refer to the id column in the Users table. In addition, there is a composite PRIMARY KEY of the two columns user_id1 and user_id2. Then, there is a query to insert data into the Friends table. Four rows of data are inserted with pairs of users who are friends.

3. Return Data

SQL
SELECT * FROM Friends;

Overall, this SQL code creates four users in a table and adds friend relationships between them in another table, then displays all the Friends relationships (table) in the database.


Summary

  • Intuitive Query Structure
    • In Cypher queries, the creation and management of friend relationships is done with clear and easy-to-understand syntax. For example, **CREATE (alice)-[:FRIENDS]->(bob)** clearly states that Alice and Bob have a friend relationship.
    • In comparison, in SQL, the representation of friend relationships requires separate tables and JOIN operations, which may be less intuitive for simple relationship cases.
  • Direct Relationship Modeling
    • In Graph Database, entities and their relationships are represented as nodes and edges, creating a more direct model that matches the way humans think about relationships.
    • In RDBMS, relationship representation requires modeling through separate tables, which can make the data structure feel more complex.
  • Easier Relationship Creation and Management
    • In the Cypher example, friend relationship creation is done with a single CREATE statement for each relationship. This creates a more compact and manageable structure.
    • In SQL, each friend relationship requires a new row in a separate table, which may require more queries and data manipulation.

Graph Database

Add a New Property

Cypher
// Add a new property (e.g. 'email') to the user node
MATCH (user:User {name: 'Alice'})
SET user.email = 'alice@example.com'

In the Graph Database, adding a new property such as 'email' to a user node is easily done using the SET statement. This requires no global schema changes and can be done directly on the node in question without affecting other nodes or the overall database structure.

Add a New Relationship

Cypher
// Add a new realtionship 'LIKES' between users
MATCH (alice:User {name: 'Alice'}), (bob:User {name: 'Bob'})
CREATE (alice)-[:LIKES]->(bob)

In Graph Database, adding new relationships such as 'LIKES' between two user nodes is done with a clear and direct CREATE statement. This increases flexibility and reduces complexity in handling new relationships.

RDBMS

Add a New Property

SQL
-- Add a new column (e.g. 'email') to the user table
ALTER TABLE Users
ADD COLUMN email VARCHAR(255);

-- Add a new value for the 'email' column to an existing row
UPDATE Users
SET email = 'alice@example.com'
WHERE name = 'Alice';

This is the query to create a Users table with two columns: id as the PRIMARY KEY and name as a character column with a maximum length limit of 255 characters.

Add a New Relationship

SQL
-- Create new table 'Likes' to represent a new relationship
CREATE TABLE Likes (
user_id1 INT,
user_id2 INT,
FOREIGN KEY (user_id1) REFERENCES Users(id),
FOREIGN KEY (user_id2) REFERENCES Users(id),
PRIMARY KEY (user_id1, user_id2)
);

-- Add a new relationship 'LIKES' between users
INSERT INTO Likes (user_id1, user_id2)
VALUES (
(SELECT id FROM Users WHERE name = 'Alice'),
(SELECT id FROM Users WHERE name = 'Bob')
);

In an RDBMS, adding a new relationship involves creating a new table (e.g. 'Likes') to represent the relationship, which requires more steps and can increase the complexity of the schema. Furthermore, the INSERT INTO statement is used to add a new row representing the relationship between two users.


Summary

Graph Database

Find the Shortest Path

Cypher
MATCH path = shortestPath((alice:User {name: 'Alice'})-[:FRIENDS*]-(david:User {name: 'David'}))
RETURN path
  • Cypher provides a special syntax shortestPath to find the shortest path between two nodes easily and clearly.
  • Shortest path search in Graph Database is implemented efficiently, utilizing graphical structures and algorithms optimized for the purpose.

Find Interconnected Nodes

Cypher
MATCH (charlie:User {name: 'Charlie'})-[:FRIENDS]->(commonFriend)<-[:FRIENDS]-(bob:User {name: 'Bob'})
RETURN commonFriend
  • Cypher allows the construction of queries that directly search for nodes that are connected through the 'FRIENDS' relationship.
  • The graphical structure allows for a simpler and more efficient implementation of queries to find connected nodes.

RDBMS

Find the Shortest Path

SQL
SELECT *
FROM Users AS u1, Friends AS f1, Users AS u2, Friends AS f2, Users AS u3
WHERE u1.name = 'Alice'
AND u1.id = f1.user_id1
AND f1.user_id2 = u2.id
AND u2.id = f2.user_id1
AND f2.user_id2 = u3.id
AND u3.name = 'David';
  • This SQL query uses a combination of multiple WHERE conditions to find the shortest path, which can make the query structure more complex and difficult to interpret, thus requiring longer execution time especially at large scale.

Find Interconnected Nodes

SQL
SELECT u3.name AS commonFriend
FROM Users AS u1
JOIN Friends AS f1 ON u1.id = f1.user_id1
JOIN Users AS u2 ON f1.user_id2 = u2.id
JOIN Friends AS f2 ON u2.id = f2.user_id1
JOIN Users AS u3 ON f2.user_id2 = u3.id
WHERE u1.name = 'Bob'
AND u2.name = 'Charlie'
AND u3.name <> 'Bob'
AND u3.name <> 'Charlie';
  • These SQL queries use a combination of JOIN and complex WHERE conditions, which can result in queries that are more difficult to understand and manage.

Summary

Graph Database

Finds All Connected Nodes

Cypher
MATCH (friend)-[:FRIENDS]->(david:User {name: 'David'})
RETURN friend
  • Intuitive Graphical Structure
    • This query creates a graphical pattern that directly illustrates the 'FRIENDS' relationship between the nodes 'friend' and 'David'.
    • A more direct and intuitive syntax describes the structure of the relationship, making it easy to understand.
  • Add friend relationships
    • Graph Database is designed for efficiency in graph traversing, and this query directly retrieves all nodes connected to 'David' through the 'FRIENDS' relationship.
    • This operation can be executed efficiently in the graph data structure.
  • Ease of Query Compilation
    • Composing queries using Cypher provides flexibility and ease in composing queries that reflect the relationship structure.

RDBMS

Finds All Connected Nodes

SQL
SELECT u1.name AS friend
FROM Users u2
JOIN Friends ON u2.id = Friends.user_id2
JOIN Users u1 ON Friends.user_id1 = u1.id
WHERE u2.name = 'David';
  • Complex JOIN Operations
    • This query involves multiple JOIN operations to join the Users and Friends tables, which can affect performance especially at scale.
  • More Complex Syntax
    • SQL syntax requires JOIN operations that require further understanding of relational structures.
    • The query structure is more complex and requires more steps to achieve the same result.

Summary