==================================================================================
Node:在括号里写( )
Label: 在Node或relationship后用冒号:写
Property:在大括号中以key:value的键值对表明{key:value}
Relationship: 在破折号-和箭头→中间用方括号[ ]括起来,里面可以写r:Label
==================================================================================
1. Create Node
Creating a Single node
CREATE (node_name);
Verification
MATCH (n) RETURN n
This query returns all the nodes in the database (we will discuss this query in detail in the coming chapters).
On executing, this query shows the created node as shown in the following screenshot.
Creating Multiple Nodes
The create clause of Neo4j CQL is also used to create multiple nodes at the same time. To do so, you need to pass the names of the nodes to be created, separated by a comma.
CREATE (node1),(node2)
Creating a Node with a Label
A label in Neo4j is used to group (classify) the nodes using labels. You can create a label for a node in Neo4j using the CREATE clause.
CREATE (node:label)
eg.CREATE (Dhawan:player)
Creating a Node with Multiple Labels
You can also create multiple labels for a single node. You need to specify the labels for the node by separating them with a colon “ : ”.
CREATE (node:label1:label2:. . . . labeln)
eg. CREATE (Dhawan:person:player)
Create Node with Properties
Properties are the key-value pairs using which a node stores data. You can create a node with properties using the CREATE clause. You need to specify these properties separated by commas within the flower braces “{ }”.
CREATE (node:label { key1: value, key2: value, . . . . . . . . . })
eg. CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
Returning the Created Node
Throughout the chapter, we used the MATCH (n) RETURN n query to view the created nodes. This query returns all the existing nodes in the database.
Instead of this, we can use the RETURN clause with CREATE to view the newly created node.
To return a node in Neo4j: CREATE (Node:Label{properties. . . . }) RETURN Node
eg. CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"}) RETURN Dhawan
2. Create Relationships
We can create a relationship using the CREATE clause. We will specify relationship within the square braces “[ ]” depending on the direction of the relationship it is placed between hyphen “ - ” and arrow “ → ” as shown in the following syntax.
CREATE (node1)-[:RelationshipType]->(node2)
eg.创建一个关系(以下几行要一起输入才能return)
# First of all, create two nodes Ind and Dhawan in the database, as shown below.
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
CREATE (Ind:Country {name: "India"})
# Now, create a relationship named BATSMAN_OF between these two nodes as −
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)
# Finally, return both the nodes to see the created relationship.
RETURN Dhawan, Ind
Creating a Relationship Between the Existing Nodes
You can also create a relationship between the existing nodes using the MATCH clause.
Following is the syntax to create a relationship using the MATCH clause.
MATCH (a:LabeofNode1), (b:LabeofNode2)
WHERE a.name = "nameofnode1" AND b.name = " nameofnode2"
CREATE (a)-[: Relation]->(b)
RETURN a,b
eg
MATCH (a:player), (b:Country)
WHERE a.name = "Shikar Dhawan" AND b.name = "India"
CREATE (a)-[r: BATSMAN_OF]->(b)
RETURN a,b
Creating a Relationship with Label and Properties
CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2)
eg
MATCH (a:player), (b:Country)
WHERE a.name = "Shikar Dhawan" AND b.name = "India"
CREATE (a)-[r:BATSMAN_OF {Matches:5, Avg:90.75}]->(b)
RETURN a,b
**Label是BATSMAN_OF,Properties是 Matches:5 和 Avg:90.75
Creating a Complete Path
In Neo4j, a path is formed using continuous relationships. A path can be created using the create clause.
CREATE p = (Node1 {properties})-[:Relationship_Type]->
(Node2 {properties})[:Relationship_Type]->(Node3 {properties})
RETURN p
eg