看到后面的案例再实操作
删除数据库中以往的图
MATCH (n) DETACH DELETE n
CREATE命令语法
Neo4j CQL“CREATE”命令用于创建没有属性的节点。 它只是创建一个没有任何数据的节点。
CREATE (
<node-name>:<label-name>
{
<Property1-name>:<Property1-Value>
........
<Propertyn-name>:<Propertyn-Value>
}
)
语法元素 | 描述 |
---|---|
<node-name> | 它是我们将要创建的节点名称,创建关系时使用到,注意命名 |
<label-name> | 它是一个节点标签名称 |
<Property1-name>...<Propertyn-name> | 属性是键值对。 定义将分配给创建节点的属性的名称 |
<Property1-value>...<Propertyn-value> | 属性是键值对。 定义将分配给创建节点的属性的值 |
1、Neo4j数据库服务器使用此
2、Neo4j数据库服务器创建一个
CREATE (cwt:Person { name:"陈王廷",generation:"创始人"})
CREATE (jf:Person { name:"蒋发",generation:"第二代"})
CREATE (sl:Person { name:"所乐",generation:"第二代"})
CREATE (rx:Person { name:"汝信",generation:"第二代"})
CREATE (zr:Person { name:"正如",generation:"第三代"})
CREATE (xr:Person { name:"恂如",generation:"第三代"})
CREATE (sr:Person { name:"申如",generation:"第三代"})
CREATE (dp:Person { name:"大鹏",generation:"第三代"})
CREATE (dk:Person { name:"大鹍",generation:"第三代"})
# 先查询,看是不是要删除的数据
MATCH (n:Drug) WHERE n.name="正如" RETURN n
# 没问题后,改成删除
MATCH (n:Drug) WHERE n.name="正如" DELETE n
MATCH命令
MATCH
(
<node-name>:<label-name>
)
注意事项
# 查询Dept下的内容
MATCH (dept:Dept) return dept
# 查询Employee标签下 id=123,name="Lokesh"的节点
MATCH (p:Employee {id:123,name:"Lokesh"}) RETURN p
## 查询Employee标签下name="Lokesh"的节点,使用(where命令)
MATCH (p:Employee)
WHERE p.name = "Lokesh"
RETURN p
MATCH & RETURN匹配和返回
MATCH (n:Person {name:"陈长兴"}) RETURN n
MATCH (n:Person {name:"陈长兴"}) RETURN n.generation
根据属性图模型,关系应该是定向的。 否则,Neo4j将抛出一个错误消息。
基于方向性,Neo4j关系被分为两种主要类型。
在以下场景中,我们可以使用Neo4j CQL CREATE命令来创建两个节点之间的关系。 这些情况适用于Uni和双向关系。
CREATE (m:Movie:Cinema:Film:Picture)
CREATE (<node1-name>:<label1-name>)-
[<relationship-name>:<relationship-label-name>]
->(<node2-name>:<label2-name>)
# <node1-name> <节点1名> 它是From节点的名称。
# <node2-name> <节点2名> 它是To节点的名称。
# <label1-name> <LABEL1名称> 它是From节点的标签名称
# <label2-name> <LABEL2名称> 它是To节点的标签名称。
# <relationship-name> <关系名称> 它是一个关系的名称。
# <relationship-label-name> <相关标签名称> 它是一个关系的标签名称。
只删除关系
MATCH (p:Person)-[r:APPRENTICE]-(h:Hierarchy) where p.name='蒋发' and h.title='第二代传承人'
DELETE r
删除节点和关系
DELETE <node1-name>,<node2-name>,<relationship-name>
MATCH (p:Person)-[r:APPRENTICE]-(h:Hierarchy) where p.name='蒋发' and h.title='第二代传承人'
DELETE p,h,r
# 删除所有关系
MATCH ()-[r]-() DELETE r
# 清数据
MATCH (n) DETACH DELETE n
创建传承人
CREATE (cwt:Person { name:"陈王廷",generation:"创始人"})
CREATE (jf:Person { name:"蒋发",generation:"第二代"})
CREATE (sl:Person { name:"所乐",generation:"第二代"})
CREATE (rx:Person { name:"汝信",generation:"第二代"})
CREATE (zr:Person { name:"正如",generation:"第三代"})
CREATE (xr:Person { name:"恂如",generation:"第三代"})
CREATE (sr:Person { name:"申如",generation:"第三代"})
CREATE (dp:Person { name:"大鹏",generation:"第三代"})
CREATE (dk:Person { name:"大鹍",generation:"第三代"})
CREATE (jx:Person { name:"继夏",generation:"第四代"})
CREATE (jb:Person { name:"敬伯",generation:"第四代"})
CREATE (jue:Person { name:"爵",generation:"第四代"})
CREATE (zs:Person { name:"善志",generation:"第四代"})
CREATE (st:Person { name:"善通",generation:"第四代"})
CREATE (dx:Person { name:"大兴",generation:"第五代"})
CREATE (yz:Person { name:"耀兆",generation:"第五代"})
CREATE (gz:Person { name:"公兆",generation:"第五代"})
CREATE (bw:Person { name:"秉旺",generation:"第五代"})
CREATE (br:Person { name:"秉壬",generation:"第五代"})
CREATE (bq:Person { name:"秉奇",generation:"第五代"})
CREATE (ccx:Person { name:"陈长兴",generation:"第六代"})
CREATE (yb:Person { name:"有本",generation:"第六代"})
CREATE (yh:Person { name:"有恒",generation:"第六代"})
CREATE (ylc:Person { name:"杨露禅",generation:"第七代"})
CREATE (gy:Person { name:"耕耘",generation:"第七代"})
CREATE (ybh:Person { name:"杨班候",generation:"第八代"})
CREATE (yx:Person { name:"延熙",generation:"第八代"})
CREATE (yn:Person { name:"延年",generation:"第八代"})
CREATE (cfk:Person { name:"陈发科",generation:"第九代"})
CREATE (wy:Person { name:"王雁",generation:"第九代"})
CREATE (cbz:Person { name:"陈宝璩",generation:"第九代"})
CREATE (wep:Person { name:"王二平",generation:"第九代"})
CREATE (wfl:Person { name:"王福礼",generation:"第十代"})
CREATE (zlh:Person { name:"朱老虎",generation:"第十代"})
CREATE (wcj:Person { name:"王长江",generation:"第十代"})
CREATE (cez:Person { name:"曹二柱",generation:"第十代"})
CREATE (zl:Person { name:"朱路",generation:"第十一代"})
CREATE (zf:Person { name:"朱峰",generation:"第十一代"})
CREATE (zc:Person { name:"朱超",generation:"第十一代"})
CREATE (zbl:Person { name:"朱堡垒",generation:"第十一代"})
CREATE (zzl:Person { name:"张中林",generation:"第十一代"})
查出现有的节点。创建关系
# 运行时删除注释
# t 是师傅(陈王廷),s1,s2,s3 是弟子
match(t:Person),(s1:Person),(s2:Person),(s3:Person) where t.name='陈王廷' and s1.name='蒋发' and s2.name='所乐' and s3.name='汝信'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t),
(s3)-[:APPRENTICE]->(t)
# t 是师傅(汝信),s1,s2,s3 是弟子
match(t:Person),(s1:Person),(s2:Person) where t.name='汝信' and s1.name='大鹏' and s2.name='大鹍'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person) where t.name='大鹏' and s1.name='善志'
CREATE
(s1)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person),(s2:Person) where t.name='善志' and s1.name='秉旺' and s2.name='秉壬'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person) where t.name='秉旺' and s1.name='陈长兴'
CREATE
(s1)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person),(s2:Person) where t.name='陈长兴' and s1.name='杨露禅' and s2.name='耕耘'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person),(s2:Person) where t.name='耕耘' and s1.name='延熙' and s2.name='延年'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person),(s2:Person),(s3:Person),(s4:Person) where t.name='延熙' and s1.name='陈发科' and s2.name='王雁' and s3.name='陈宝璩' and s4.name='王二平'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t),
(s3)-[:APPRENTICE]->(t),
(s4)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person),(s2:Person),(s3:Person),(s4:Person) where t.name='王雁' and s1.name='王福礼' and s2.name='朱老虎' and s3.name='王长江' and s4.name='曹二柱'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t),
(s3)-[:APPRENTICE]->(t),
(s4)-[:APPRENTICE]->(t)
match(t:Person),(s1:Person),(s2:Person),(s3:Person),(s4:Person),(s5:Person) where t.name='朱老虎' and s1.name='朱路' and s2.name='朱峰' and s3.name='朱超' and s4.name='朱堡垒' and s5.name='张中林'
CREATE
(s1)-[:APPRENTICE]->(t),
(s2)-[:APPRENTICE]->(t),
(s3)-[:APPRENTICE]->(t),
(s4)-[:APPRENTICE]->(t),
(s5)-[:APPRENTICE]->(t)
使用新节点创建关系
# 创建辈份-- 执行时要去掉注释
CREATE (FirstGeneration:Hierarchy {title:'陈氏太极创始人'})
# 分开执行
match(t:Hierarchy),(s1:Person) where t.title='陈氏太极创始人' and s1.name='陈王廷'
CREATE
(s1)-[:BELONG_TO]->(t)
# 创建辈份 -- 执行时要去掉注释
CREATE (SecondGeneration:Hierarchy {title:'第二代传承人'})
# 创建关系 -- 执行时要去掉注释
match(t:Hierarchy),(s1:Person),(s2:Person),(s3:Person) where t.title='第二代传承人' and s1.name='蒋发' and s2.name='所乐' and s3.name='汝信'
CREATE
(s1)-[:BELONG_TO]->(t),
(s2)-[:BELONG_TO]->(t),
(s3)-[:BELONG_TO]->(t)
新节点创建关系
CREATE (FirstGeneration:Hierarchy {title:'陈氏太极创始人'})
CREATE (cwt:Person { name:"陈王廷",generation:"创始人"})
CREATE
(cwt)-[:BELONG_TO]->(FirstGeneration)
CREATE (SecondGeneration:Hierarchy {title:'第二代传承人'})
CREATE (jf:Person { name:"蒋发",generation:"第二代"})
CREATE (sl:Person { name:"所乐",generation:"第二代"})
CREATE (rx:Person { name:"汝信",generation:"第二代"})
CREATE
(jf)-[:BELONG_TO]->(SecondGeneration),
(sl)-[:BELONG_TO]->(SecondGeneration),
(rx)-[:BELONG_TO]->(SecondGeneration),
(jf)-[:APPRENTICE]->(cwt),
(sl)-[:APPRENTICE]->(cwt),
(rx)-[:APPRENTICE]->(cwt)
CREATE (Thirdly:Hierarchy {title:'第三代传承人'})
CREATE (zr:Person { name:"正如",generation:"第三代"})
CREATE (xr:Person { name:"恂如",generation:"第三代"})
CREATE (sr:Person { name:"申如",generation:"第三代"})
CREATE (dp:Person { name:"大鹏",generation:"第三代"})
CREATE (dk:Person { name:"大鹍",generation:"第三代"})
CREATE
(zr)-[:BELONG_TO]->(Thirdly),
(xr)-[:BELONG_TO]->(Thirdly),
(sr)-[:BELONG_TO]->(Thirdly),
(dp)-[:BELONG_TO]->(Thirdly),
(dk)-[:BELONG_TO]->(Thirdly),
(zr)-[:APPRENTICE]->(sl),
(xr)-[:APPRENTICE]->(sl),
(sr)-[:APPRENTICE]->(sl),
(dp)-[:APPRENTICE]->(rx),
(dk)-[:APPRENTICE]->(rx)
CREATE (Fourth:Hierarchy {title:'第四代传承人'})
CREATE (jx:Person { name:"继夏",generation:"第四代"})
CREATE (jb:Person { name:"敬伯",generation:"第四代"})
CREATE (jue:Person { name:"爵",generation:"第四代"})
CREATE (zs:Person { name:"善志",generation:"第四代"})
CREATE (st:Person { name:"善通",generation:"第四代"})
CREATE
(jx)-[:BELONG_TO]->(Fourth),
(jb)-[:BELONG_TO]->(Fourth),
(jue)-[:BELONG_TO]->(Fourth),
(zs)-[:BELONG_TO]->(Fourth),
(st)-[:BELONG_TO]->(Fourth),
(zs)-[:APPRENTICE]->(dp),
(st)-[:APPRENTICE]->(dk)
CREATE (Fifth:Hierarchy {title:'第五代传承人'})
CREATE (dx:Person { name:"大兴",generation:"第五代"})
CREATE (yz:Person { name:"耀兆",generation:"第五代"})
CREATE (gz:Person { name:"公兆",generation:"第五代"})
CREATE (bw:Person { name:"秉旺",generation:"第五代"})
CREATE (br:Person { name:"秉壬",generation:"第五代"})
CREATE (bq:Person { name:"秉奇",generation:"第五代"})
CREATE
(dx)-[:BELONG_TO]->(Fifth),
(yz)-[:BELONG_TO]->(Fifth),
(gz)-[:BELONG_TO]->(Fifth),
(bw)-[:BELONG_TO]->(Fifth),
(br)-[:BELONG_TO]->(Fifth),
(bq)-[:BELONG_TO]->(Fifth),
(bw)-[:APPRENTICE]->(zs),
(br)-[:APPRENTICE]->(zs),
(bq)-[:APPRENTICE]->(st)
CREATE (Sixth:Hierarchy {title:'第六代传承人'})
CREATE (ccx:Person { name:"陈长兴",generation:"第六代"})
CREATE (yb:Person { name:"有本",generation:"第六代"})
CREATE (yh:Person { name:"有恒",generation:"第六代"})
CREATE
(ccx)-[:BELONG_TO]->(Sixth),
(yb)-[:BELONG_TO]->(Sixth),
(yh)-[:BELONG_TO]->(Sixth),
(ccx)-[:APPRENTICE]->(bw)
CREATE (Seventh:Hierarchy {title:'第七代传承人'})
CREATE (ylc:Person { name:"杨露禅",generation:"第七代"})
CREATE (gy:Person { name:"耕耘",generation:"第七代"})
CREATE
(ylc)-[:BELONG_TO]->(Seventh),
(gy)-[:BELONG_TO]->(Seventh),
(ylc)-[:APPRENTICE]->(ccx),
(gy)-[:APPRENTICE]->(ccx)
CREATE (Eighth:Hierarchy {title:'第八代传承人'})
CREATE (ybh:Person { name:"杨班候",generation:"第八代"})
CREATE (yx:Person { name:"延熙",generation:"第八代"})
CREATE (yn:Person { name:"延年",generation:"第八代"})
CREATE
(ybh)-[:BELONG_TO]->(Eighth),
(yx)-[:BELONG_TO]->(Eighth),
(yn)-[:BELONG_TO]->(Eighth),
(yx)-[:APPRENTICE]->(gy),
(yn)-[:APPRENTICE]->(gy)
CREATE (Ninth:Hierarchy {title:'第九代传承人'})
CREATE (cfk:Person { name:"陈发科",generation:"第九代"})
CREATE (wy:Person { name:"王雁",generation:"第九代"})
CREATE (cbz:Person { name:"陈宝璩",generation:"第九代"})
CREATE (wep:Person { name:"王二平",generation:"第九代"})
CREATE
(cfk)-[:BELONG_TO]->(Ninth),
(wy)-[:BELONG_TO]->(Ninth),
(cbz)-[:BELONG_TO]->(Ninth),
(wep)-[:BELONG_TO]->(Ninth),
(cfk)-[:APPRENTICE]->(yx),
(wy)-[:APPRENTICE]->(yx),
(cbz)-[:APPRENTICE]->(yx),
(wep)-[:APPRENTICE]->(yx)
CREATE (Tenth:Hierarchy {title:'第十代传承人'})
CREATE (wfl:Person { name:"王福礼",generation:"第十代"})
CREATE (zlh:Person { name:"朱老虎",generation:"第十代"})
CREATE (wcj:Person { name:"王长江",generation:"第十代"})
CREATE (cez:Person { name:"曹二柱",generation:"第十代"})
CREATE
(wfl)-[:BELONG_TO]->(Tenth),
(zlh)-[:BELONG_TO]->(Tenth),
(wcj)-[:BELONG_TO]->(Tenth),
(cez)-[:BELONG_TO]->(Tenth),
(wfl)-[:APPRENTICE]->(wy),
(zlh)-[:APPRENTICE]->(wy),
(wcj)-[:APPRENTICE]->(wy),
(cez)-[:APPRENTICE]->(wy)
CREATE (Eleventh:Hierarchy {title:'第十一代传承人'})
CREATE (zl:Person { name:"朱路",generation:"第十一代"})
CREATE (zf:Person { name:"朱峰",generation:"第十一代"})
CREATE (zc:Person { name:"朱超",generation:"第十一代"})
CREATE (zbl:Person { name:"朱堡垒",generation:"第十一代"})
CREATE (zzl:Person { name:"张中林",generation:"第十一代"})
CREATE
(zl)-[:BELONG_TO]->(Eleventh),
(zf)-[:BELONG_TO]->(Eleventh),
(zc)-[:BELONG_TO]->(Eleventh),
(zbl)-[:BELONG_TO]->(Eleventh),
(zzl)-[:BELONG_TO]->(Eleventh),
(zl)-[:APPRENTICE]->(zlh),
(zf)-[:APPRENTICE]->(zlh),
(zc)-[:APPRENTICE]->(zlh),
(zbl)-[:APPRENTICE]->(zlh),
(zzl)-[:APPRENTICE]->(zlh)
姓:surname 或者 family name或者 last name
名:first name 或者 forename 或者 given name
字:style name,有时也作courtesy name
号:pseudonym,有时也做(hao)
人称:一般可以翻译成 nicknames
谥号:posthumous title