You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Now that you can read data with MATCH and WHERE, it is time to learn how to create, update, and delete data in Neo4j using Cypher.
The CREATE clause adds new nodes and relationships to the graph:
CREATE (p:Person {name: "Alice", age: 30})
RETURN p
CREATE (a:Person {name: "Alice", age: 30})
CREATE (b:Person {name: "Bob", age: 25})
RETURN a, b
MATCH (a:Person {name: "Alice"})
MATCH (b:Person {name: "Bob"})
CREATE (a)-[:FRIENDS_WITH {since: 2022}]->(b)
CREATE (a:Person {name: "Alice"})-[:WORKS_AT {role: "Engineer"}]->(c:Company {name: "Acme"})
RETURN a, c
Important:
CREATEalways creates new data. If a node with the same properties already exists, it will create a duplicate.
MERGE is the idempotent alternative to CREATE — it only creates data if it does not already exist:
MERGE (p:Person {name: "Alice"})
RETURN p
If a :Person node with name: "Alice" exists, it is returned. If not, it is created.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.