Create Nodes in Neo4j CQL
In Neo4j, CQL (Cypher Query Language) is used to create, read, update, and delete data. One of the most basic operations in CQL is creating nodes. Nodes are the fundamental building blocks of Neo4j graphs, and represent entities as well as their attributes.
Syntax
The syntax for creating nodes in CQL is as follows:
CREATE (node:Label {Prop1: Value1, Prop2: Value2, ...})
CREATE
is used to create a new node.node
is the name of the node. This can be any valid identifier.Label
represents a label that is assigned to the node. This is optional.Prop1: Value1, Prop2: Value2, ...
are the properties of the node and their values.
Example
Consider the following example where we want to create a node for a person.
CREATE (p:Person {name: 'John', age: 25, city: 'New York'})
This will create a Person
node with the properties name
, age
, and city
.
Output
After running the above command, you will see the following output in the Neo4j Browser:
(1 node created)
Explanation
In the example above, we have created a Person
node with the name
property set to 'John'
, the age
property set to 25
, and the city
property set to 'New York'
. The parentheses ( )
denote the node, the colon :
separates the node name from its label, and the curly braces { }
enclose the node’s properties.
Use
Creating nodes in Neo4j CQL is useful when we want to add new entities to the graph database. These entities can represent anything we want, such as people, products, or orders.
Important Points
- Labels are optional, but add power to graph queries by organizing nodes into groups.
- Node properties are key-value pairs that allow us to store data as properties of nodes.
- Nodes can have any number of properties, and property names must be unique within a node.
Summary
Creating nodes is a basic operation in Neo4j CQL, and is used to add new entities to the graph database. The CREATE
clause is used to create new nodes, and labels and properties can be added to customize the node. Labels are optional but allow us to organize nodes into groups, and properties are key-value pairs that allow us to store data as properties of nodes. Nodes can have any number of properties, and property names must be unique within a node.