Let’s talk about aliases in Neo4j Cypher.
Aliases are just variable names you assign to nodes, relationships, or returned values so you can refer to them later in the query.
🔹 1. Aliases for Nodes
Here:
-
u
is the alias (variable) for eachUser
node. -
You can access its properties as
u.name
,u.role
.
🔹 2. Aliases for Relationships
Here:
-
u
→ alias forUser
-
p
→ alias forProject
-
r
→ alias forWORKS_ON
relationship
🔹 3. Aliases in RETURN
You can rename columns in the output (like SQL AS
).
✅ The result will show columns userName
and userRole
instead of u.name
and u.role
.
🔹 4. Aliases in Aggregations
✅ The output columns are projectName
and teamSize
.
🔹 5. Aliases with WITH (for chaining queries)
-
First part: create aliases
projectName
andteamSize
-
Then filter using those aliases.
⚡ Key Idea:
-
Inside patterns
(u:User)
→ alias for node -
Inside RETURN/WITH → alias for results
MERGE
vs CREATE
Perfect 👌 this is one of the most important differences to understand in Neo4j. Let’s break it down step by step with examples.
🔹 CREATE
-
Always creates new nodes or relationships, even if an identical one already exists.
-
It never checks for duplicates.
Example
👉 You now have two different Alice nodes, even though they look the same.
🔹 MERGE
-
Think of it as "match if exists, otherwise create".
-
It searches for a node/relationship that matches the entire pattern.
-
If it finds one → reuses it.
-
If it doesn’t → creates it.
Example
👉 Only one Alice node exists, because the second MERGE
matches the first one.
🔹 Important Detail
MERGE matches the exact properties you give.
So this can create duplicates if you’re not careful:
👉 Two Alice nodes get created:
-
First has
{name: "Alice"}
-
Second has
{name: "Alice", role: "Developer"}
✅ To avoid this, you can match on fewer properties and then set others:
Let’s go over how to update data in Neo4j using SET
and REMOVE
.
🔹 1. SET – Add or Update Properties
-
SET
is used to add new properties or update existing ones on nodes/relationships. -
If the property exists → it’s updated.
-
If it doesn’t exist → it’s created.
Example – Update a User’s role
👉 If Alice has no role
, it will be created. If she already has one, it will be replaced.
Example – Add multiple properties
Example – Copy all properties from a map
👉 Adds department
and level
without removing existing ones.
🔹 2. REMOVE – Delete Properties or Labels
-
REMOVE
is used to delete a property from a node/relationship. -
It can also remove labels from nodes.
Example – Remove a property
👉 Deletes the location
property.
Example – Remove a label
👉 Alice is no longer a User
node (but still exists as a node in the graph).
🔹 3. SET vs REMOVE – Quick Comparison
Operation | Command | Effect |
---|---|---|
Add/update property | SET u.role = "Admin" | Adds or updates role |
Add multiple props | SET u += {a:1, b:2} | Adds both a and b |
Remove property | REMOVE u.role | Deletes role property |
Remove label | REMOVE u:User | Removes User label |
⚡ Tip:
-
Use
SET
when you want to add or update information. -
Use
REMOVE
when you want to delete properties or labels but keep the node/relationship itself.
In Cypher, when you use
it works like a merge (upsert) of properties, not like replacing the whole property map.
🔹 What actually happens:
-
If a property does not exist → it gets added.
-
If a property already exists → its value gets overwritten (updated).
-
Other properties stay unchanged.
Example:
Suppose Alice
has this node:
After running:
👉 Result:
🔹 Key difference vs direct SET
If you did:
👉 Then Alice’s node would keep only those two properties, and name
+ location
would be lost.
So +=
is like safe update/merge, while =
is replace everything.
deleting in Neo4j has two flavors, and the difference is very important.
🔹 DELETE
-
Removes nodes or relationships, but only if the node has no relationships.
-
If a node is still connected, Neo4j will throw an error.
Example
👉 ❌ Error: Cannot delete node <id>
because it still has relationships.
Because Alice is linked to ProjectX
.
🔹 DETACH DELETE
-
Deletes a node and all its relationships in one step.
-
Think of it as force delete.
Example
👉 ✅ Alice is deleted, and her relationships to projects, apps, etc. are also removed automatically.
🔹 Deleting Relationships
You can delete relationships separately too:
👉 Only the relationship is deleted, Alice and ProjectX remain.
⚡ Quick Summary
Command | What it does |
---|---|
DELETE | Deletes a node/relationship if no other relationships exist |
DETACH DELETE | Deletes a node and all its connected relationships |
DELETE r | Deletes a relationship only |
Comments
Post a Comment