Neo4j - CREATE

==================================================================================
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.

image.png

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)

image.png

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"})

image.png

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 
image.png

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


image.png

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


image.png

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 胡晓会 河南漯河 网络初级九期学员 2018.11.15坚持分享264天 上手才会做 之前刘老师说过,做着做着就会...
    胡晓会阅读 113评论 0 0
  • 此刻刚刚从老板的办公室里面出来,空气里充满离别的味道,走出公司,感觉很放松,同时又很沉重,心情很无奈 昨天中午的同...
    okcgogogo阅读 307评论 1 0
  • 同读一本书《59秒心理学》2016-5-4-73 正文: “团队思考”影响个人观点和看法的现象还不止极化。④ 另外...
    台东万达DDM一店张春燕阅读 615评论 1 0

友情链接更多精彩内容