使用 Cypher CREATE 创建数据
学习使用 CREATE 子句向 Neo4j 图数据库添加新节点和关系
使用 Cypher CREATE 创建数据 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Welcome to Cypher CREATE!
In this lesson, you'll learn how to add new data to your Neo4j graph database using the CREATE clause in Cypher, the query language for Neo4j.
We'll cover creating individual nodes, adding properties, and defining relationships between them. Let's get started!
Creating Your First Node
The simplest way to add a node is with CREATE followed by parentheses (). Inside the parentheses, you can give your node a variable name (like n) for later reference in the same query.
Try running this basic example:
CREATE (n)Adding Labels to Nodes
Nodes usually represent real-world entities, like a 'Person' or a 'Movie'. We use labels to categorize nodes. Labels are defined after the node variable using a colon :.
It's good practice to always use labels for better organization and querying.
CREATE (p:Person)Nodes with Properties
Nodes often have attributes that describe them. These are called properties. Properties are key-value pairs enclosed in curly braces {} after the label.
- Keys are strings (like
name). - Values can be strings, numbers, booleans, or lists.
CREATE (m:Movie {
title: 'The Matrix',
releaseYear: 1999
})Creating Multiple Nodes
You can create several nodes in a single CREATE statement by separating them with a comma ,. This is efficient when adding related data.
Here, we're creating two different types of nodes at once:
CREATE (a:Actor {name: 'Keanu Reeves'}),
(d:Director {name: 'Lana Wachowski'})Introducing Relationships
Relationships define how nodes are connected. They are directional and always connect two nodes. Their syntax looks like an arrow ()-[]->().
()for nodes.-[]->for a directed relationship.-[]-for an undirected relationship (less common).
Creating Relationships Between Existing Nodes
To create a relationship between nodes that already exist, you first need to MATCH them. Then, you can use CREATE to define the relationship.
Relationships also have a type (e.g., ACTED_IN) and can have properties.
MATCH (a:Actor {name: 'Keanu Reeves'})
MATCH (m:Movie {title: 'The Matrix'})
CREATE (a)-[:ACTED_IN]->(m)Relationships with Properties
Just like nodes, relationships can also have properties. These properties describe the relationship itself, not the nodes it connects.
For example, an ACTED_IN relationship might have a role property.
MATCH (a:Actor {name: 'Keanu Reeves'})
MATCH (m:Movie {title: 'The Matrix'})
CREATE (a)-[:ACTED_IN {role: 'Neo'}]->(m)Creating Nodes and Relationships Together
One of the most powerful features of CREATE is the ability to define new nodes and their relationships in a single statement. This helps build complex graph patterns efficiently.
Here, we create a new 'Person' and a new 'City', connecting them with a LIVES_IN relationship.
CREATE (p:Person {name: 'Samantha'})
-[:LIVES_IN]->
(c:City {name: 'London', country: 'UK'})Putting It All Together
Let's combine what we've learned to build a small graph for a fictional 'Project' and 'Team' members:
- Create a Project node.
- Create two Person nodes.
- Connect the Persons to the Project with
WORKS_ONrelationships, each with aroleproperty.
CREATE (p:Project {name: 'CoddyKit App', budget: 50000})
CREATE (dev:Person {name: 'Alex', email: 'alex@example.com'})
CREATE (qa:Person {name: 'Jamie', email: 'jamie@example.com'})
CREATE (dev)-[:WORKS_ON {role: 'Developer'}]->(p)
CREATE (qa)-[:WORKS_ON {role: 'QA Engineer'}]->(p)Quick Check on CREATE
Which of the following Cypher statements correctly creates a new node labeled Product with a name property of 'Laptop' and a price of 1200?
Recap & Next Steps
Great job! You've learned the fundamentals of creating data in Neo4j using the Cypher CREATE clause.
- You can create nodes with labels and properties.
- You can define directional relationships between nodes.
- You can even create nodes and relationships in a single statement.
Next, you'll learn how to find and retrieve this data using the MATCH clause!
常见问题解答
「使用 Cypher CREATE 创建数据」课时是免费的吗?
是的 — 「使用 Cypher CREATE 创建数据」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。
「使用 Cypher CREATE 创建数据」这节课中我会学到什么?
学习使用 CREATE 子句向 Neo4j 图数据库添加新节点和关系 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Neo4j Graph Database Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Cypher CREATE 创建数据」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?
能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 节点、关系与属性
- 使用 Cypher CREATE 创建数据
- 使用 Cypher MATCH 匹配模式
- 使用 RETURN 返回并塑造结果