1、基本命令

1.1、创建

1.1.1、创建节点

  1. #emp是名称,可以不写,Employee表示节点标签
  2. CREATE (emp:Employee)
  3. # 创建后可以返回
  4. CREATE (dept:Dept)
  5. return dept
  6. # 创建标签为Dept的节点,并为其设置属性
  7. CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })
  1. CREATE (m:Movie:Cinema:Film:Picture)

不存在才创建
MERGE=CREATE+MATCH
如果我们观察上面的查询结果,它只显示一行,因为CQL MERGE命令检查该节点在数据库中是否可用。 如果它不存在,它创建新节点。 否则,它不创建新的。

  1. MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})

1.1.2、创建关系

  1. MATCH (e:Customer),(cc:CreditCard)
  2. CREATE (e)-[r:DO_SHOPPING_WITH ]->(cc)
  1. MATCH (cust:Customer),(cc:CreditCard)
  2. CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc)
  3. RETURN r
  1. CREATE (fb1:FaceBookProfile1)-[like:LIKES]->(fb2:FaceBookProfile2)
  2. return like
  1. CREATE (video1:YoutubeVideo1{title:"Action Movie1",updated_by:"Abc",uploaded_date:"10/10/2010"})
  2. -[movie:ACTION_MOVIES{rating:1}]->
  3. (video2:YoutubeVideo2{title:"Action Movie2",updated_by:"Xyz",uploaded_date:"12/12/2012"})

1.1.3、创建索引

  1. CREATE INDEX ON :Customer (name)

1.1.4、创建UNIQUE约束

  1. CREATE CONSTRAINT ON (cc:CreditCard)
  2. ASSERT cc.number IS UNIQUE

1.2、删除

1.2.1、删除节点和关系

  1. MATCH (e: Employee) DELETE e
  1. MATCH (cc: CreditCard)-[rel]-(c:Customer)
  2. DELETE cc,c,rel
  3. # 删除所有关系
  4. MATCH p=()-->() DELETE p
  5. MATCH p=()-[]->() DELETE p

1.2.2、删除节点/关系的属性

  1. MATCH (book { id:122 })
  2. REMOVE book.price
  3. RETURN book

1.2.3、删除节点/关系的标签

  1. MATCH (m:Movie)
  2. REMOVE m:Picture

1.2.4、删除索引

  1. DROP INDEX ON :Customer (name)

1.2.5、删除UNIQUE约束

  1. DROP CONSTRAINT ON (cc:CreditCard)
  2. ASSERT cc.number IS UNIQUE

1.3、修改

1.3.1、添加或更新属性值

  1. MATCH (book:Book)
  2. SET book.title = 'superstar'
  3. RETURN book

1.4、查询

MATCH 查询不能单独使用,配和return,表示返回

  1. #查询所有节点
  2. MATCH (n) return n
  3. # 查询Dept下的内容
  4. MATCH (dept:Dept) return dept

1.4.1、条件查询

  1. # 查询Employee标签下 id=123,name="Lokesh"的节点
  2. MATCH (p:Employee {id:123,name:"Lokesh"}) RETURN p
  3. # 查询Employee标签下name="Lokesh"的节点,使用(where命令)
  4. MATCH (p:Employee)
  5. WHERE p.name = "Lokesh"
  6. RETURN p
  7. # 查询Employee标签下name="Lokesh"和id=123的节点,使用(where命令)
  8. MATCH (p:Employee)
  9. WHERE p.name = "Lokesh" AND p.id=123
  10. RETURN p

支持:AND、OR、NOT、XOR,=、 <>、 >、 <、 >=、 <=;

1.4.2、排序

  1. # 默认升序
  2. MATCH (emp:Employee)
  3. RETURN emp.empid,emp.name,emp.salary,emp.deptno
  4. ORDER BY emp.name
  5. # 降序
  6. MATCH (emp:Employee)
  7. RETURN emp.empid,emp.name,emp.salary,emp.deptno
  8. ORDER BY emp.name DESC

1.4.3、合并

结果列类型和来自两组结果的名称必须匹配,这意味着列名称应该相同,列的数据类型应该相同。

  1. MATCH (cc:CreditCard) RETURN cc.id,cc.number
  2. UNION
  3. MATCH (dc:DebitCard) RETURN dc.id,dc.number
  1. MATCH (cc:CreditCard)
  2. RETURN cc.id as id,cc.number as number,cc.name as name,
  3. cc.valid_from as valid_from,cc.valid_to as valid_to
  4. UNION ALL
  5. MATCH (dc:DebitCard)
  6. RETURN dc.id as id,dc.number as number,dc.name as name,
  7. dc.valid_from as valid_from,dc.valid_to as valid_to

1.4.4、分页

  1. MATCH (emp:Employee)
  2. RETURN emp
  3. LIMIT 2
  4. MATCH (emp:Employee)
  5. RETURN emp
  6. SKIP 2
  7. MATCH (n) RETURN n SKIP 2 LIMIT 2

1.4.5、判空

  1. MATCH (e:Employee)
  2. WHERE e.id IS NOT NULL
  3. RETURN e.id,e.name,e.sal,e.deptno
  4. MATCH (e:Employee)
  5. WHERE e.id IS NULL
  6. RETURN e.id,e.name,e.sal,e.deptno

1.4.6、IN范围查询

  1. MATCH (e:Employee)
  2. WHERE e.id IN [123,124]
  3. RETURN e.id,e.name,e.sal,e.deptno

2、CQL函数

2.1、字符串函数

2.1.1、转大写

  1. MATCH (e:Employee)
  2. RETURN e.id,UPPER(e.name),e.sal,e.deptno

2.1.2、转小写

  1. MATCH (e:Employee)
  2. RETURN e.id,LOWER(e.name),e.sal,e.deptno

2.1.3、截取字符串

  1. MATCH (e:Employee)
  2. RETURN e.id,SUBSTRING(e.name,0,2),e.sal,e.deptno

2.2、聚合

2.2.1、计数

  1. MATCH (e:Employee) RETURN COUNT(*)

2.2.2、最大/小值

  1. MATCH (e:Employee)
  2. RETURN MAX(e.sal),MIN(e.sal)

2.2.3、求和/平均值

  1. MATCH (e:Employee)
  2. RETURN SUM(e.sal),AVG(e.sal)

2.3、关系函数

2.3.1、开始/结束节点

  1. # 关系的开始节点。
  2. MATCH (a)-[movie:ACTION_MOVIES]->(b)
  3. RETURN STARTNODE(movie)
  4. # 关系的结束节点。
  5. MATCH (a)-[movie:ACTION_MOVIES]->(b)
  6. RETURN ENDNODE(movie)

2.3.2、打印ID和关系类

  1. MATCH (a)-[movie:ACTION_MOVIES]->(b)
  2. RETURN ID(movie),TYPE(movie)