注意项:在你电脑执行报错,有可能是未创建节点。
CREATE
- 创建无属性的节点
CREATE (emp:Employee)
# emp 是一个节点名。Employee 是 emp 节点的标签名称。
- 创建有属性的节点
CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })
# dept是一个节点名。Dept是emp节点的标签名称。大括号里是Dept的数据
- 创建多个标签到节点
CREATE (m:Movie:Cinema:Film:Picture)
# 这里m是一个节点名,Movie, Cinema, Film, Picture是m节点的多个标签名称
- 创建带有关系(无属性)的多节点(无属性)
CREATE (fb1:FaceBookProfile1)-[like:LIKES]->(fb2:FaceBookProfile2)
# 关系名称是“LIKES” 关系标签是“like”
- 创建带有关系(有属性)的多节点(有属性)
CREATE (video1:YoutubeVideo1{title:"Action Movie1",updated_by:"Abc",uploaded_date:"10/10/2010"})-[movie:ACTION_MOVIES{rating:1}]->(video2:YoutubeVideo2{title:"Action Movie2",updated_by:"Xyz",uploaded_date:"12/12/2012"})
MATCH搭配RETURN
- 查询节点内容
MATCH (dept:Dept) return dept
# 查询Dept下的内容
MATCH (p:Employee {id:123,name:"Lokesh"}) RETURN p
# 查询Employee标签下 id=123,name="Lokesh"的节点
MATCH (p:Employee) WHERE p.name = "Lokesh" RETURN p.id,p.name
# 查询Employee标签下name="Lokesh"的节点,使用(where命令,类比sql)
- 查询节点关系
MATCH (cust)-[r:DO_SHOPPING_WITH]->(cc) RETURN cust,cc
# 关系名称为“DO_SHOPPING_WITH” 关系标签为“r”。若报错,则可能未创建cust,cc
- 查询并创建节点关系
MATCH (e:Customer),(cc:CreditCard) CREATE (e)-[r:DO_SHOPPING_WITH ]->(cc)
# 关系名称为“DO_SHOPPING_WITH” 关系标签为“r”,关系为e->c,关系无属性
MATCH (cust:Customer),(cc:CreditCard) CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) RETURN r
# 关系名称为“DO_SHOPPING_WITH” 关系标签为“r”,关系为e->c,关系有属性shopdate、price。
WHERE
- 查询并创建节点关系
MATCH (cust:Customer),(cc:CreditCard) WHERE cust.id = "1001" AND cc.id= "5001" CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) RETURN r
DELETE
- 删除节点
MATCH (e: Employee) DELETE e
- DELETE节点和关系
MATCH (cc: CreditCard)-[rel]-(c:Customer) DELETE cc,c,rel
REMOVE
- 删除节点/关系
MATCH (book { id:122 }) REMOVE book.price RETURN book
# “价格”属性被删除
- 删除标签
MATCH (m:Movie) REMOVE m:Picture
SET
- 更新节点属性值
MATCH (book:Book) SET book.title = 'superstar' RETURN book
Sorting
- 按节点属性排序(升降序)
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno ORDER BY emp.name ASC
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno ORDER BY emp.name DESC
UNION
- 联合查询(UNION:将两组结果中的公共行组合并返回到一组结果中。 它不从两个节点返回重复的行。)
MATCH (cc:CreditCard) RETURN cc.id as id,cc.number as number,cc.name as name, cc.valid_from as valid_from,cc.valid_to as valid_to UNION MATCH (dc:DebitCard) RETURN dc.id as id,dc.number as number,dc.name as name,dc.valid_from as valid_from,dc.valid_to as valid_to
# 查询应具有相同的列名
- 联合查询(UNION ALL:将两组结果中的公共行组合并返回到一组结果中。 它从两个节点返回重复的行。)
MATCH (cc:CreditCard)RETURN cc.id as id,cc.number as number,cc.name as name,cc.valid_from as valid_from,cc.valid_to as valid_to UNION ALL MATCH (dc:DebitCard) RETURN dc.id as id,dc.number as number,dc.name as name, dc.valid_from as valid_from,dc.valid_to as valid_to
# 查询应具有相同的列名
LIMIT和SKIP
- 限制条数(LIMIT)
MATCH (emp:Employee) RETURN emp LIMIT 2
# 取前两行
- 限制条数(LIMIT)
MATCH (emp:Employee) RETURN emp SKIP 2
# 取末尾两行