Neo4j CQL grammar

Keywords: Database

1. Create nodes

CREATE (dept:Dept { deptno:10,dname:"Accounting",location:"Hyderabad" })

Where Dept is the node name and Dept is the label name (which can be understood as the table name)

CREATE (emp:Employee{id:123,name:"Lokesh",sal:35000,deptno:10})

2. search

Neo4j uses the CQL MATCH + RETURN command-

  • Retrieving some attributes of a node
  • Retrieving all attributes of a node
  • Retrieving some attributes of nodes and relationships
  • Retrieving all attributes of nodes and relationships
MATCH (dept: Dept) RETURN dept.deptno,dept.dname

 

3. Create relationships

In the following scenarios, we can use the Neo4j CQL CREATE command to create a relationship between two nodes. These situations apply to Uni and two-way relationships.

  • Create attributeless relationships between two existing nodes
MATCH (e:Customer) RETURN e

MATCH (cc:CreditCard) RETURN cc

MATCH (e:Customer),(cc:CreditCard) CREATE (e)-[r:DO_SHOPPING_WITH ]->(cc)
  • Create relationships with attributes between two existing nodes
MATCH (cust:Customer),(cc:CreditCard) CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) RETURN r

  • Create attributeless relationships between two new nodes
  • Create relationships with attributes between two new nodes
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"})
  • Creating/not using attributes between two exit nodes with a WHERE clause

4. Create labels

Use the Neo4j CQL CREATE command

  • Create a single label for a node
CREATE (google1:GooglePlusProfile)
  • Create multiple tags for nodes
CREATE (m:Movie:Cinema:Film:Picture)
  • Create a single label for a relationship
CREATE (p1:Profile1)-[r1:LIKES]->(p2:Profile2),

5. Where clause creates relationships

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

MATCH (a {name: "Laurence Fishburne"} - [r1: ACTED_IN] - > (m) < - [r2: DIRECTED] - (d) RETURN a,r1,r2,m,d and MATCH (a) - [r1: ACTED_IN] - > (m) < - [r2: DIRECTED] - (d) where a.name= "Laurence Fishe" RETURN a, r1, r2, m, where clauses are equivalent

6. Delete nodes and relationships

MATCH (e: Employee) DELETE e

MATCH (cc: CreditCard)-[rel]-(c:Customer) DELETE cc,c,rel

7. Remove deletes labels and attributes

Delete attributes

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

Tag deletion

MATCH (m:Movie) REMOVE m:Picture

8. Set adds attributes

MATCH (dc:DebitCard) SET dc.atm_pin = 3456 RETURN dc

9. Sorting sort

MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno ORDER BY emp.name

10. Union merger

<MATCH Command1> UNION <MATCH Command2>

11. Limit and skip

MATCH (emp:Employee) RETURN emp LIMIT 2

MATCH (emp:Employee) RETURN emp SKIP 2

Neo4j CQL has provided a "SKIP" clause to filter or limit the number of rows returned by the query. It trims the results at the top of the CQL query result set, skipping the first two lines.

12. Merge merger

MERGE = CREATE + MATCH

Used to check whether the node has been created, because the CQL MERGE command checks whether the node is available in the database. If it does not exist, it creates a new node. Otherwise, it does not create a new node.

13. In operation

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

14. Directional relations

Neo4j only supports directed relationships

15. String functions (upper, lower, substring, replace)

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

Upper, lower, substring (string subscript SUBSTRING(e.name,0,2), characters 0 to 2), replace

16. Aggregation functions (count, max, min, sum, avg)

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

17. Relational functions (start node, end node, id, type)

MATCH (a)-[movie:ACTION_MOVIES]->(b) RETURN ENDNODE(movie)

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

Internal ID of the system

18. Unique constraints

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

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

 

 

 

Reference: https://cloud.tencent.com/developer/article/1336299

Posted by pcjackson06 on Thu, 02 May 2019 10:50:38 -0700