Work with Graph database (GraphDB)
Create
You must first create a Graph database and fill it with data.
This is done in two stages:
- On the Graph page, click the + icon and fill in the fields. This creates a database Card. Only the Name field is mandatory for filling, however, the Description of the database is important and will use it in the AI generation. The Description field should contain key information that tells us what the database is all about. This might include the type of information it contains, its purpose, who uses it, and so on. The more details, the better.
- Using the Cypher query language, create a query, which will run in the Cypher query field and fill the database with data
About cypher queries
Creating a Graph database primarily involves defining your data entities, and the relationships between them, and querying the data using a database query language. Here we are using the Cypher query language to explain the process:
Identify your data entities: the first step in creating a graph database is to identify the different types of information, or "entities", you'll be dealing with (such as "User", "Product", "Category", etc.). These entities will form the nodes of your graph.
Define Relationships: the second step is defining how these entities relate to one another. This will form the Relations of the graph.
Creating Nodes: nodes are the entities in your database. Using the Cypher query language, you can create a node as follows:
CREATE (:User {name:'John Doe', email:'johndoe@example.com'})
CREATE (:User {name:'John Doe', email:'johndoe@example.com'})
and
CREATE (:Product {name:'Product 1'})
CREATE (:Product {name:'Product 1'})
This creates a node of type user
with two properties: 'name' and 'email', and node of type product
with the property 'name'.
- Establishing relationships: relationships connect nodes together. You can create a relationship between nodes like this:
MATCH (a:User),(b:Product)
WHERE a.name = 'John Doe' AND b.name = 'Product 1'
CREATE (a)-[r:BOUGHT]->(b)
RETURN type(r)
MATCH (a:User),(b:Product)
WHERE a.name = 'John Doe' AND b.name = 'Product 1'
CREATE (a)-[r:BOUGHT]->(b)
RETURN type(r)
This first finds the nodes that satisfy certain conditions (a user named 'John Doe' and a product named 'Product 1') and then creates a 'BOUGHT' relationship from the user to the product.
Get data
Once your data is imported into the database, you can perform complex querying operations to extract the required information.
For example, you can find and display individual nodes that have been added to the database:
MATCH (n:`User`) RETURN n
MATCH (n:`User`) RETURN n
Also, you can find all the products bought by a particular user like this:
MATCH (a)-[r:`BOUGHT`]->(b) RETURN a, r, b
MATCH (a)-[r:`BOUGHT`]->(b) RETURN a, r, b
This finds all nodes of the type 'product' that have a 'BOUGHT' relationship with a 'user' node where the name is 'John Doe', and then returns the names of these products.
While creating your Graph database, keep in mind that the advantage of a Graph database comes from its ability to handle complex relationships between data points effectively. So, carefully consider your data and potential relationships when designing your database.
Update data
In terms of updating data within a Graph database, the Cypher language offers the SET keyword, which is used to modify the values of properties and add new ones.
- Updating node properties
Did you make a mistake in some data? Or do you need to update something? No problem. You can change the properties of a node using the SET
keyword.
Here's how:
MATCH (n:User {name:'John Doe'})
SET n.email = 'johnupdated@example.com'
RETURN n
MATCH (n:User {name:'John Doe'})
SET n.email = 'johnupdated@example.com'
RETURN n
This finds the node of type 'user' with the name 'John Doe', sets a new 'email' for this node, and then returns the updated node.
- Adding a new property
In addition to updating properties, you also can add new properties to a node:
MATCH (n:User {name:'John Doe'})
SET n.address = '123 Sesame Street'
RETURN n
MATCH (n:User {name:'John Doe'})
SET n.address = '123 Sesame Street'
RETURN n
This code adds an 'address' property to the 'user' node for 'John Doe' and returns the updated node.
- Updating relationship properties
Relationships can have properties too! To update a property on a relationship, you will again use the SET
clause.
MATCH (p:Person)-[r:KNOWS]->(n:Person {name:'John Doe'})
SET r.since = '2023'
RETURN r
MATCH (p:Person)-[r:KNOWS]->(n:Person {name:'John Doe'})
SET r.since = '2023'
RETURN r
This finds the 'KNOWS' relationship from any 'person' node to 'John Doe' and sets a new property 'since' on this relationship, then returns the updated relationship.
A key thing to remember is that updating the same property will overwrite the existing value, so always double-check the state of your graph data before performing updates.
TIP
Backups are strongly recommended in case of unintentional changes. With regular backups, you can roll back to a previous state if needed.
Delete data
Deleting data from a Graph database using the Cypher query language, involves the use of the DELETE
or DETACH DELETE
keywords. Here is how they work:
- Deleting node with no relationships:
If a node doesn't have any relationships, you can delete it directly:
MATCH (n:User {name: 'John Doe'})
DELETE n
MATCH (n:User {name: 'John Doe'})
DELETE n
This finds the node of type 'user' with the name 'John Doe' and deletes it.
- Deleting relationships:
To delete a relationship, you must first match it and then delete it:
MATCH (a:User)-[r:BOUGHT]->(b:Product)
WHERE a.name = 'John Doe' AND b.name = 'Product 1'
DELETE r
MATCH (a:User)-[r:BOUGHT]->(b:Product)
WHERE a.name = 'John Doe' AND b.name = 'Product 1'
DELETE r
This finds the 'BOUGHT' relationship between a user named 'John Doe' and a product named 'product 1' and deletes it.
- Deleting nodes with relationships:
If a node is still connected to other nodes by relationships, trying to delete it directly will cause an error, because this would leave dangling relationships. Here, the DETACH DELETE
keyword comes in handy:
MATCH (n:User {name: 'John Doe'})
DETACH DELETE n
MATCH (n:User {name: 'John Doe'})
DETACH DELETE n
This deletes the 'user' node with the name 'John Doe' and all relationships attached to it.
DANGER
Be cautious when deleting data. Deleting data will permanently remove it from the database. It's always an excellent practice to back up the data before deleting it, especially with large datasets or complex databases.