-- 创建节点语句
CREATE (
<node-name>:<label-name>
{
<Property1-name>:<Property1-Value>
........
<Propertyn-name>:<Propertyn-Value>
}
);
其中的node-name 是节点名称
label-name 是标签名称
propert1-name是属性名称和property-value是属性值
例如
单节点单标签
create (p:Person{name:"alicy"});
create (p:Person{name:"Tom", sex:"男"});
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129141357460-1602773755.png)
单节点多标签
CREATE (m:Movie:Cinema:Film:Picture)
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129141341059-1255495751.png)
-- match 语法
方法一:match (x:Person{name:"Tom"}) return x;
方法二:
match (p:Person)
where p.name="Tom"
return p;
这两种是等价的,都是查找到Person下的名字叫Tom的节点
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129141440676-1267441676.png)
返回特定节点的特定属性
返回名字叫Tom的性别属性:
match (p:Person{name:"Tom"}) return p.sex;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129141501187-1382484157.png)
创建单标签到关系
关系都是有方向的,从开始到结束
CREATE (<node1-name>:<label1-name>)-
[<relationship-name>:<relationship-label-name>]
->(<node2-name>:<label2-name>)
node1-name:是from的节点名称、label1-name是标签名称
node2-name:是to的节点名称 label2-name是标签名称
relationship-name:关系名称
relationship-label-name:关系标签名称
为alicy和Tom创建恋人关系
match(a:Person{name:"alicy"}),(b:Person{name:"Tom"})
create (a)
-[rl:love]
->(b);
match(a:Person{name:"Tom"}),(b:Person{name:"alicy"})
create (a)
-[rl:love]
->(b);
相当于查出alicy和Tom,并分别叫上别名a,b ,为a,b建立关系
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129144145719-518245582.png)
-- 移除关系,让alicy变心
match(a:Person{name:"alicy"})-[lv]->(b:Person{name:"Tom"}) delete lv;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129144726123-2005117209.png)
-- alicy 爱上了laowang
match(a:Person{name:"alicy"}),(b:Person{name:"LaoWang"})
create (a)
-[lv:love]
->(b);
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129145213275-2071542546.png)
-- Tom 发现后自动解除关系,并退出
match(a:Person{name:"Tom"})
-[lv]->(b:Person{name:"alicy"})
delete a,lv
;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129145613006-1055524843.png)
-- 删除节点
match (a:Movie) delete a;
-- 删除Tom节点以及它的所有关系
match (p:Person{name:'Tom'}) -[lv]-(m) delete p,lv;
-- 为alicy 添加性别属性
match (p:Person{name:"alicy"}) set p.sex='女' return p;
match (p:Person{name:"alicy"}) set p.job='主播' return p;
-- 把alicy的职业移除
match (p:Person{name:"alicy"}) remove p.job return p;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129191247948-1212902347.png)
-- 按照名字排序
match (p:Person) return p.name ORDER BY p.name desc;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129191323117-935162411.png)
-- union
match (a:Person{name:"alicy"})return a.name union all match(b:Person{name:"Tom"}) return b.name;
这种写法是不行的
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129191852100-1236939928.png)
从报错中可以得到必须要相同的列名,虽然我们看着相同,但是编辑器不认识,所以加上一个别名
match (a:Person{name:"alicy"}) return a.name as name union all match(b:Person{name:"Tom"}) return b.name as name;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129192526276-1450969597.png)
-- limit
match (a:Person) return a LIMIT 1;
-- skip
match (a:Person) return a skip 1;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129192942205-199934873.png)
从结果看alicy被跳过去了
-- merge 语法 无则添加有则不动
match(a:Person{name:"Tom"}),(b:Person{name:"LaoWang"}) merge (a)-[lv:love]->(b) return a, b ;
-- 加上唯一约束
CREATE CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUE;
![](https://img2023.cnblogs.com/blog/3306499/202311/3306499-20231129200246332-619897540.png)
-- in 语句不能使用()而是[]
match(p:Person) where p.name in ["Tom","test"] return p;