3. Introduction to Neo4j query language Cypher related commands

In the previous article, we introduced the graph database Neo4j. In this article, let's take a look at its query language-Cypher. Cypher is a declarative graph query language that expresses efficient query and update of graph databases. Follow the column "Knowledge Graph Series" to learn more about related knowledge~


table of Contents

1. Introduction

1.1 Commonly used commands and functions

1.2 Data type

2. Commonly used commands

2.1 CREATE command

2.1.1 Create a node without attributes

2.1.2 Create a node with attributes

2.2 MATCH command

2.3 RETURN command

2.4 Relationship

2.4.1 Create a relationship without attributes

2.4.2 Create a relationship with attributes

2.5 WHERE command

2.5.1 Boolean operators

2.5.2 Comparison operators

2.5.3 Use the WHERE clause to create a relationship

2.6 DELETE command

2.6.1 Delete node 

2.6.2 Delete nodes and relationships

2.7 REMOVE command

2.8 SET command

2.9 ORDER BY command

2.10 UNION command

2.11 LIMIT and SKIP commands

2.12 MERGE command

2.13 NULL value

2.14 IN command


 

1. Introduction

1.1 Commonly used commands and functions

Cypher query language is also called CQL, and its commonly used commands are as follows:

The commonly used functions of CQL are as follows:

1.2 Data type

The data types of CQL are similar to the Java language, and they are used to define the attributes of nodes or relationships.

2. Commonly used commands

2.1 CREATE command

2.1.1 Create a node without attributes

The syntax is as follows:

CREATE (<node-name>:<label-name>)

Among them, node-name is the name of the node, and label-name is the label name of the node.

E.g:

CREATE(person:Person)

2.1.2 Create a node with attributes

The syntax is as follows:

CREATE (
   <node-name>:<label-name>
   { 	
      <Property1-name>:<Property1-Value>
      ........
      <Propertyn-name>:<Propertyn-Value>
   }
)

Among them, Propertyn-name is the name of the node property created, and Propertyn-Value is the value of the node property created.

E.g:

CREATE (person:Person { name:"xzw",sex:"m",location:"QD" })

2.2 MATCH command

The MATCH command is used to obtain data about nodes, relationships, and attributes from the database. The syntax is as follows:

MATCH 
(
   <node-name>:<label-name>
)

The MATCH command is usually used together with the RETURN command.

2.3 RETURN command

The RETURN command is used to retrieve the attributes of nodes and associations. The syntax is as follows:

RETURN 
   <node-name>.<property1-name>,
   ........
   <node-name>.<propertyn-name>

E.g:

CREATE (t: test {name:"xzw", sex:"m", loc: "QD"})
MATCH (t: test)
RETURN t.name,t.sex

2.4 Relationship

Based on directionality, Neo4j relationships are divided into two main types: one-way relationships and two-way relationships.

2.4.1 Create a relationship without attributes

The syntax is as follows:

CREATE  
   (<node1-label-name>:<node1-name>)-
   [<relationship-label-name>:<relationship-name>]->
   (<node1-label-name>:<node1-name>)
RETURN <relationship-label-name>

Among them, relationship-name is the name of the relationship, and relationship-label-name is the label name of the relationship. The return clause in the above grammar is not necessary.

E.g:

CREATE (f1:Favorite1)-[like:LIKES]->(f2:Favorite2)
RETURN like

2.4.2 Create a relationship with attributes

The syntax is as follows:

CREATE  
	(<node1-label-name>:<node1-name>{<define-properties-list>})-
	[<relationship-label-name>:<relationship-name>{<define-properties-list>}]
	->(<node1-label-name>:<node1-name>{<define-properties-list>})
RETURN <relationship-label-name>

E.g:

CREATE (v1:Video1{title:"Action1",updated_by:"A",uploaded_date:"10/10/2020"})
-[movie:ACTION_MOVIES{rating:1}]->
(v2:Video2{title:"Action2",updated_by:"X",uploaded_date:"12/12/2020"}) 

MATCH (v1:Video1)-[movie:ACTION_MOVIES]->(v2:Video2) 
RETURN movie

2.5 WHERE command

Like SQL, Neo4j CQL provides a WHERE clause in the CQL MATCH command to filter the results of the MATCH query. The syntax is as follows:

1、简单语法:WHERE <condition>
2、复杂语法:WHERE <condition> <boolean-operator> <condition>
其中,condition的语法为:<property-name> <comparison-operator> <value>

Among them, <comparison-operator> is the comparison operator of CQL.

E.g:

MATCH (p:People)
RETURN p.name,p.sex,p.location

MATCH (p:People) 
WHERE p.name = 'xzw'
RETURN p

 

2.5.1 Boolean operators

2.5.2 Comparison operators

2.5.3 Use the WHERE clause to create a relationship

The syntax is as follows:

MATCH (<node1-label-name>:<node1-name>),(<node2-label-name>:<node2-name>) 
WHERE <condition>
CREATE (<node1-label-name>)-[<relationship-label-name>:<relationship-name>
       {<relationship-properties>}]->(<node2-label-name>) 

E.g:

CREATE (c:Customer {id:"1", name: "xzw", sex:"m"})
CREATE (f:Fav {id:"1", favor: "sleep"})

MATCH (c:Customer)
RETURN c.id,c.name,c.sex

MATCH (f:Fav)
RETURN f.id, f.favor

MATCH (c:Customer),(f:Fav) 
WHERE c.id = "1" AND f.id= "1" 
CREATE (c)-[r:Relat{location:"QD"}]->(f) 
RETURN r

2.6 DELETE command

Delete nodes and related nodes and relationships. 

2.6.1 Delete node 

The syntax is as follows:

DELETE <node-name-list>

E.g:

MATCH (p:People)
RETURN p.name

MATCH (p:People) DELETE p

MATCH (p:People)
RETURN p.name

2.6.2 Delete nodes and relationships

The syntax is as follows:

DELETE <node1-name>,<node2-name>,<relationship-name>

In order to save space, there is no special explanation below, and no examples are given, because these grammars are relatively simple, and interested friends can test it by themselves.

It is worth noting here that you can delete all nodes and relationships in the database through the following command:

MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r

2.7 REMOVE command

Delete the attributes or labels of an existing node or relationship. It is worth noting that both DELETE and REMOVE commands should be used together with the MATCH command. The REMOVE command syntax is as follows:

REMOVE <property-name-list>/<label-name-list> 

其中,<property-name-list>是一个属性列表,其用法如下:

<node-name>.<property1-name>,
<node-name>.<property2-name>, 
.... 
<node-name>.<propertyn-name> 

<label-name-list>它是一个标签列表,其用法如下:

<node-name>:<label2-name>, 
.... 
<node-name>:<labeln-name> 

2.8 SET command

The SET command is used to add or update attribute values ​​to existing nodes or relationships. Its syntax is as follows:

SET  <property-name-list>

其中,<property-name-list>是一个属性列表,其语法如下:

<node-label-name>.<property1-name>,
<node-label-name>.<property2-name>, 
.... 
<node-label-name>.<propertyn-name> 

2.9 ORDER BY command

Neo4j CQL provides an "ORDER BY" clause in the MATCH command to sort the results returned by the MATCH query. By default, it sorts the rows in ascending order. If we want to sort them in descending order, we need to use the DESC clause. The syntax is as follows:

ORDER BY  <property-name-list>  [DESC]	 

其中,<property-name-list>是属性列表,其语法如下:

<node-label-name>.<property1-name>,
<node-label-name>.<property2-name>, 
.... 
<node-label-name>.<propertyn-name> 

2.10 UNION command

It combines the common rows in the two sets of results and returns them to a set of results. It does not return duplicate rows from the two nodes. Similar to SQL, UNION ALL returns all data. The syntax is as follows:

<MATCH Command1>
   UNION
<MATCH Command2>

2.11 LIMIT and SKIP commands

LIMIT command to filter or limit the number of rows returned by the query, it trims the results at the bottom of the CQL query result set. If we want to trim the results at the top of the CQL query result set, then we should use the SKIP command. Their syntax is as follows:

LIMIT <number>

SKIP <number>

2.12 MERGE command

The MERGE command is a combination of the CREATE command and the MATCH command. It searches for a given pattern in the graph, and returns the result if it exists, and if it does not exist in the graph, it creates a new node/relation and returns the result. The syntax is as follows:

MERGE (<node-name>:<label-name>
{
   <Property1-name>:<Pro<rty1-Value>
   .....
   <Propertyn-name>:<Propertyn-Value>
})

2.13 NULL value

Neo4j CQL treats null values ​​as missing or undefined values ​​for the attributes of nodes or relationships. When we create a node with an existing node label name but do not specify its attribute value, it will create a new node with a NULL attribute value.

2.14 IN command

Like SQL, Neo4j CQL provides an IN operator to provide a collection of values ​​for CQL commands. The syntax is as follows:

IN[<Collection-of-values>]

 

This article has come to an end. This article mainly describes some of the common commands of Neo4j CQL. The next article will talk about the common function commands of CQL. What problems did you encounter in the process, welcome to leave a message, let me see what problems you all encountered~

Guess you like

Origin blog.csdn.net/gdkyxy2013/article/details/109489063