Neo4j Cypher Clauses | Neo4j Tutorial | Lecture 6
Summary
TLDRこのビデオスクリプトでは、Neo4jデータベースでのデータ操作のためのCypher言語の基本について学びます。スクリプトは、データの読み書きに必要なCypherの様々な句(Clause)を説明し、実際のサンプルデータセットを使ってクエリの練習を通じて理解を深めます。MATCH、OPTIONAL MATCH、CREATE、DELETE、SET、REMOVEなどの主要な操作について解説され、データベースのノードやリレーションシップの追加・削除・更新方法がわかりやすく説明されています。
Takeaways
- 📘 Neo4jは、データベース内のデータの読み書きにCypher言語を使用している。これはインタラクティブなクエリで、データの読み書きやトランザクションのコミットなどを行うための様々な節を提供している。
- 🔍 MATCH節は、グラフデータベース内で検索するパターンを指定する基本的な機能であり、ノードとリレーションシップの構造に基づいている。
- 👁 OPTIONAL MATCHは、特定のパターンがデータベースに存在しない場合でも、NULLを返してクエリの実行を継続できる。
- 🌐 Neo4jデスクトップを使用して、サンプルデータセットをインポートし、Cypherシェルを開始してデータを操作することができる。
- 🎬 データセットの例として、映画の推薦システムが使用されており、ノードとリレーションシップのタイプ(acted in, directed, genreなど)が定義されている。
- 📊 DBMSスキーマの可視化手続きを使用することで、データベースのスキーマを理解し、グラフの構造を分析することができる。
- 🔄 WITH節は、クエリの結果を次のクエリに渡すために使用され、不要なノードやリレーションシップをフィルタリングするのに役立つ。
- 🛠 CREATE節を使用して、グラフに新しいノードやリレーションシップを作成することができる。
- ❌ DELETE節は、ノードやリレーションシップを削除する際に使用され、関連するリレーションシップも明示的に削除する必要がある。
- 🔄 UNWINDはリストを行に展開する機能で、複雑なCypherクエリでリストを操作する際に役立つ。
- 🛂 SET節は、ノードやリレーションシップのラベルやプロパティを更新するために使用され、REMOVE節はそれらを削除する際に使用される。
Q & A
Neo4jとはどのようなデータベースですか?
-Neo4jはグラフデータベースであり、ノードとリレーションシップを使ってデータをモデル化します。これはリレーショナルデータベースとは異なるデータ構造を持っており、特にネットワークや関係を中心としたデータ分析に適しています。
Cypher言語はなぜ必要ですか?
-Cypher言語はNeo4jでデータを読み書きするためのインタラクティブなクエリ言語です。データの検索、書き込み、トランザクションのコミットなどを行うために使用されます。
MATCH節はどのような役割を持っていますか?
-MATCH節はデータベース内で検索するパターンを指定するために使用されます。ノードとリレーションシップの構造に基づいてデータの検索条件を定義することができます。
OPTIONAL MATCH節とMATCH節の違いは何ですか?
-OPTIONAL MATCH節は検索パターンがデータベースに存在しない場合でも、NULL値を返してクエリの実行を継続します。一方、MATCH節ではパターンが存在しない場合、クエリは終了します。
Neo4j Desktopの使い方について教えてください。
-Neo4j Desktopはグラフデータベースの管理と操作を行うためのツールです。サンプルプロジェクトをインポートし、データベースを開始してブラウザで操作することができます。Cypherクエリを実行し、データの隠れたパターンを見つけることができます。
グラフデータベースのスキーマはどのように取得できますか?
-グラフデータベースのスキーマは、DBMSの手続き呼び出しを使用して取得できます。この手続き呼び出しは、データベースのラベル、リレーションシップタイプ、およびそれらのプロパティを視覚化して理解を深めるのに役立ちます。
CREATE節はどのような操作を行いますか?
-CREATE節はグラフデータベースに新しいノードやリレーションシップを作成するために使用されます。既存のノードに新しいリレーションシップを追加したり、新しいノードを作成して既存のノードに接続したりすることができます。
DELETE節はどのようにデータを削除するのですか?
-DELETE節はノードやリレーションシップ、またはパスを削除するために使用されます。ノードを削除する前に、そのノードに関連するすべてのリレーションシップを明示的に削除する必要があります。
DETACH DELETEとDELETEの違いは何ですか?
-DETACH DELETEはノードを削除する際に、そのノードに関連するすべてのリレーションシップも自動的に削除されます。これは単にノードを削除するDELETEとは異なり、よりクリーンなデータ削除を可能にします。
SET操作はどのようにデータを更新するのですか?
-SET操作はノードやリレーションシップのラベルやプロパティを更新するために使用されます。新しいラベルを追加したり、既存のノードに新しいプロパティを設定したりすることができます。
REMOVE操作はどのような目的を持っていますか?
-REMOVE操作はノードやリレーションシップからラベルやプロパティを削除するために使用されます。不要なラベルやプロパティを削除することで、データの整理や更新を行います。
UNWIND節はどのような役割を持っていますか?
-UNWIND節はリストを行のシーケンスに展開するために使用されます。collectで集めた値をUNWINDで行ごとに分割し、個々の値として扱うことができます。
WITH節の機能は何ですか?
-WITH節はクエリの各パートをチェーン状に繋げ、前の結果を次のクエリに渡すために使用されます。必要に応じて特定のノードやリレーションシップ、または変数のみをフィルタリングして次のステップに渡すことができます。
FOREACH節はどのような状況で使用されますか?
-FOREACH節は複雑なCypherクエリで使用され、リスト内の各要素に対して操作を実行するために使われます。データのローディングや基本的なパターンの取得に役立ちます。
Outlines
😀 Neo4jとCypher言語の基本操作
この段落では、Neo4jというグラフデータベースとCypherというインタラクティブなクエリ言語について解説しています。Cypher言語を使うことで、グラフデータベース内のデータを読み書き、トランザクションをコミットすることができると説明されています。特に、データの読み込みに使用されるMatch節や、データの検索パターンを定義するOptional Match節について学ぶことができました。また、Neo4jデスクトップの使い方や、サンプルデータセットを使って実際のデータ操作を体験する手引きも紹介されています。
🎬 グラフデータベースの構造とデータの可視化
第二段落では、グラフデータベースの構造について学びます。ノードとリレーションシップがどのようにデータベースにモデル化されるかを理解することが重要です。ノードは異なるラベルで表現され、リレーションシップはそれらをつなぐもので、特定の種類を持つことができます。段落では、映画データセットを使って実際にデータベースの構造を可視化する方法を説明しており、映画のアクター、監督、ジャンル、評価などの要素がどのように関連しているかを具体的に例に挙げています。
🔍 Cypherクエリの基本構文とデータの読み方
この段落では、Cypher言語を使ってグラフデータベースを照会する方法について学びます。Match節を使ってデータベース内の特定のパターンを検索する方法や、Return節を使って検索結果を定義する方法が解説されています。また、With節を使ってクエリの結果を次のクエリに渡し、Unwind節を使ってリストを行に展開するテクニックも紹介されています。これらの節を組み合わせることで、複雑なデータ構造を効果的に照会することができると理解できます。
🛠️ グラフデータの操作: ノードとリレーションシップの作成
第四段落では、Cypher言語を使ってグラフデータベースにノードとリレーションシップを作成する方法について説明されています。Create節を使って新しいノードやリレーションシップを追加する方法を学び、方向性を持つリレーションシップの定義方法も紹介されています。また、既存のノードに関連する新しいノードやリレーションシップを作成する例も示されています。
🗑️ ノードとリレーションシップの削除
この段落では、グラフデータベースからノードやリレーションシップを削除する方法について学びます。Delete節を使って特定のノードやリレーションシップを削除する方法と、その前に関連するリレーションシップを削除する必要性について説明されています。Detach Deleteという技法を使って、ノードとそのすべての関連リレーションシップを一度に削除する方法も紹介されています。
✏️ ノードとリレーションシップのプロパティの更新
第六段落では、Set節とRemove節を使ってグラフデータベース内のノードやリレーションシップのラベルやプロパティを更新する方法について説明されています。ラベルの追加や削除、プロパティの追加や更新、そしてRemove節を使って不要なラベルやプロパティを削除する方法が具体的に紹介されています。これらの操作は、データのメンテナンスや更新に役立ちます。
📚 Cypher言語の高度な機能と総括
最後の段落では、Cypher言語の高度な機能について触れています。ForEach節やSet節、条件式、パターンマッチング、数学関数、集約関数など、データの分析や操作に役立つ多くのオプションがあることを示しています。このセクションでは、これまでに学んだ基本的な節を復習し、次に進む前にデータ操作の基礎をしっかりと身につけることが推奨されています。また、次回の講座ではこれらの高度な節について詳しく説明する予定であることが示されています。
Mindmap
Keywords
💡Neo4j
💡Cypher言語
💡MATCH句
💡OPTIONAL MATCH
💡CREATE句
💡DELETE句
💡SET操作
💡REMOVE句
💡UNWIND操作
💡WITH句
💡プロジェクトデータセット
Highlights
Neo4j基础系列教程继续深入,介绍Cypher语言的必要性和基础用法。
Cypher语言是Neo数据库中用于读写数据的交互式查询语言。
介绍了Cypher中的不同子句,如数据读取、写入、事务提交等。
MATCH子句用于在图数据库中搜索特定模式。
OPTIONAL MATCH用于在搜索模式不存在时返回NULL,而不是结束查询。
Neo4j桌面应用程序的使用,包括如何导入数据集和启动Cypher shell。
通过Neo4j浏览器分析图数据库的结构和模式。
介绍了如何使用MATCH子句来查询特定节点和关系。
RETURN子句用于定义查询结果的输出格式。
WITH子句允许将一个查询部分的结果用作下一个查询的起点。
UNWIND操作用于将列表转换为多行数据。
CREATE子句用于在图中创建新的节点和关系。
DELETE子句用于删除图中的节点、关系或路径。
DETACH DELETE是删除节点及其所有关联关系的快捷方式。
SET操作用于更新节点和关系的标签和属性。
REMOVE操作用于从节点和关系中移除标签或属性。
介绍了如何使用Cypher进行复杂的数据操作,包括创建、删除和更新。
预告了下一讲将介绍更高级的Cypher子句,包括FOREACH、条件模式匹配等。
Transcripts
hello and welcome back to the channel we
are going to continue our neo4j basic
Series in the last lecture we have seen
all about the cipher language and why it
is necessary for reading and writing
data into Neo database so basically it
is a interactive query which allow us to
have different Clauses like reading the
data writing the data trans committing
the transactions and so on so basically
we have seen all about the cipher but
enough talking let's get our hands dirty
and learn all of these clauses and also
follow along with me and we'll practice
those on our sample data set so without
further any Ado let's get into it so the
first Clause is reading Clauses so
basically reading Clauses will allow you
to read the data which is already
present in your graph database so
basically you can say it as a select
clause which we usually use in the rdbms
so basically in select Clause we will be
having like the column names which we
want to F from our database table so
here as well we will be using the math
statement and the optional math
statement for reading the data and
defining how your output should look
like so here in the match Clause we will
specify the patterns to search for in
the database so basically graph
databases is not structured like the
rows and columns it is structured as per
the nodes and relationship you have to
Define your nodes and relationship and
the pattern of those nodes and
relationship so basically with that
you'll be able to f the required data
and in the optional match you have to
specify the pattern to search in the
database while also using the nulls for
missing parts of the pattern so
basically if the particular pattern is
not present in your database your query
will not return anything it will return
as a null so basically it helps you to
continue your query even though you
didn't find that pattern and if you use
math statement instead of the optional
match that query will definitely end so
here you have to understand the
difference between match an optional
match so optional match will allow you
to continue your execution and it will
return the nulls for missing parts of
the pattern so let's jump on to the Neo
4G desktop so the first so we have
already installed NE 4G desktop on your
PC so let's get our hands dirty and
start the cipher shell so that we'll
able to read the data using this match
and optional match okay so this is our
neo4j desktop so here as you can see in
the beginning you will not be having any
projects your database so here we can
import it using the predefined data sets
which is provided by Neo 4G itself so
here you can click on new and here you
can import the sample projects so there
are different projects that are readily
available such as the recommendation
system the fraud detection system so
that you can directly start working on
the data so here we have already have
some databases related to the
recommendation system which we have
imported so all you have to do is just
start the database and click on open
which will head you to the new forg
browser so once we open this here as you
can see this is where we can submit our
Cipher queries and it is very
interactive it is great tool for
analyzing your data which is present in
the graph and it allows business users
to look into the hidden patterns in your
data as well so to get the onology of
the graph because you have to first
understand the structure of the graph
and how the notes and lab s are defined
as well as the type of relationship
between the two labels so to allow you
here as you can see on the left hand
side we got the count of notes so
basically we have like 28k notes already
present in our sample data set and as
you can see we got the actor director
genre movie person and user so basically
it's a movie data set and by the name
you will able to understand that it's a
movie recommendation system and also in
the relationship type we have like the
acted
directed in genre and rated
relationships present and here are all
the properties which are taged to the
relationship or the nodes in your
database but you will not understand the
onology by just looking at this labels
and relationships and the properties
which are present so to get the data the
first thing you have to do is you have
to analyze and understand the structure
and the onology of your graph to do that
you just have a procedure which is
called
DB do schema. visualization so this
procedure will allow you to get the
schema of your database so as you can
see if we expand this we have like the
different labels here we got the
director actor when we have the user
which is also we have the person and the
genre so basically we have the actor
which can also be the person and it has
the relationship with the movie actor
has a relationship with movie as actor
in so any particular actor acted in the
movie as well as any particular actor
can also direct the movie so it also has
the directed relationship between these
two notes also we have the director node
so basically director can also act in a
movie so that's why we have both the
relationships tacked to this director
and the movie and also a particular
movie can have a genre so it is present
in a specific genre so it is pretty
readable graph so this is just like a
whiteboard approach if you want to build
your graph you already discussed about
how to model your graph and how to
convert the data into nodes and
relationships you can directly start
working on the Whiteboard and just
writing the labels and the relationships
and how and what will be the direction
of the relationships between these two
notes and also we got the user which
basically has the rate so basically the
particular user will rate the movie and
also we got the person as well since
actors are also person there could be
like two labels assigned to a single
record so you can relate it to the rdbms
so if you compared it to the relational
databases all these labels will have a
certain table in the rdbms so we will be
having like a actor table then the user
table the director genre person and the
movies table and we will be having like
foreign keys and primary Keys between
these tables which also represents the
relationship types in the graph
databases so this is how your data looks
like and to just speak into your data
you can just directly click on any node
to get some sample records so here as
you can see we will we have just click
on the actor and we got like 25 actors
present in a database so it will return
some 25 records for you to just see the
structure of the graph so here as you
can see here we have a particular actor
and as you can see we got the actor as
well as person label assigned to it so
you can say it as this particular record
is present in both table person table
and the ACT table if you're compared it
to the rdbms and as you can see we have
the actor which is born on 1879 and died
on 1917 also it has the IMDb ID so IMDb
is a great platform and I hope your
movie nerds will already know about it
so it is a Eric Campbell so basically
this particular actor also present as a
person label if you double click this
this will expand it and gives us the
nodes which are connected to this act
actor so as you can see this actor acted
in a film which is called as immigrant
so this Immigrant movie came in 1970 so
these are like all the properties which
are present on each node and here as you
can see in the relationship we also have
the role that particular user has played
in which is like the head waiter so this
head waiter role is played by the Eric
Campbell in the Immigrant movie which
came in 1970 so this is pretty
self-explanatory and also there is
another actor which played in The
Immigrant movie which is the Anna pan I
don't know how to pronounce that name
but this is what it is and also she
played a role as a immigrant in this
movie and also we have the Albert Al
Austin which played the diner and the
Immigrant role in this movie so this
graph is pretty self-explanatory but
this is not the point we have to learn
about all the Clauses of Cipher and how
to read the data using the cipher so
first one is as you can see we got the
match so match will basically allow you
to specify the patterns which you have
to search in your database so as you can
see the pattern means you have this
pattern we have the two nodes which are
connected using the acted in
relationship so to represent it we have
already seen how to represent a node as
well as the relationship in the cipher
so we will directly go over to the
command line and start writing our
Cipher query so here the first step will
be the match Clause then we have to
specify the nodes which is like the
starting point of our pattern so here
I'll assign a variable to our node which
is a and it should be actor so basically
this is the actor so if you want to just
get this particular actor as well as the
Immigrant you have to just search for a
property in that actor label so here we
can directly denote it in the curly
brackets and we have the name of that
actor so we can have like the name here
then we give the colon and in quotes
which is like a text format we can give
the Eric Campbell name here so right now
we just got the particular actor and we
hold it in the a variable and as you can
see we got the acted in relationship
which is going from actor to the movie
so here we can denote it in the square
brackets and the relationship type is
acted in so we have the acted in and the
acted in is further connected so as you
can see this is directed from the actor
node to the movie node and here we will
give our movie node so I'll assign it a
m variable and here is our movie so
basically this will give us all the
movies which are related to Eric
Campbell and again we can return this
pattern so our query will look out for
this pattern in a whole graph and give
us the result so the return Clause is
used to define how your output looks
like we are going to also see the return
clause in the subsequent topic but let's
say we'll just return all here
so as you can see we got the required
pattern we got the Eric Campbell and the
movie ID so this is how the match Clause
looks like so let's say if the
particular pattern is not present in the
database so what we can do is we can
just give like the directed in because
Chris Campbell didn't actually directed
in any movie right so we know that this
pattern doesn't exist let's say we will
return all here so if we return all here
you can see there is no output to this
query so if we use the optional match it
will look out for the pattern and it
will instead return the null instead of
returning nothing so this is very useful
if you have like long Cipher queries and
you have to check for any conditions
which and patterns which are present in
the database so if we just give optional
before the math statement then let's see
what happens so as you can see we got
the null value here so it will help you
to
continue the execution even though that
pattern is not available in the database
this is the basic difference between
match and the optional match and many of
the developers will struggle using this
optional match in the query because
optional match is pretty expensive in
some scenarios so you have to make sure
that you're understanding the difference
between match and optional match
carefully this is all about reading
Clauses let's talk about the projecting
Clauses now okay so the next one is the
projecting Clauses so as you can see
this will help us to project our graph
and how our end graph will really look
like so basically you can also get the
tabular output as well as the graphical
output which we just seen so graphical
output will return you the nodes and
relationships and the tabular output
will give you all the properties which
are present in a node or a relationship
so as you can see the first Clause we
have already seen before 1 minute so
return Clause so return Clause will
Define what to include in the query
result set so it will be either a
tabular form or it will be bunch of noes
and relationships so we have already
seen it so as you can see we are
returning the notes here but let's say
if you want to just display the name of
that actor as well as the birth date of
that actor that also we can do so we can
use the return Clause so let's say we
have to return the name of that actor
so we got the name in the name property
here so we will give like a do name
because a represents the variable of
actor and also we have like the a do BN
here which is like a birth date for that
particular actor so if you return it as
you can see we got a tabular output as
the name and bond date of that
particular actor this is very simple so
the next Clause is with Clause so as you
can see with Clause will allow the query
parts to be chained together so this
will also help you to piping the result
from one to be use as a starting point
or the criteria to the next so let's say
if you have a longer query here so let's
say if you have a longer query you got
like 20 to 30 lines of query and your
start statement will have like some
patterns which includes 10 15 nodes or
and 5 to 10 relationships but you don't
need that subsequently and those will be
reducing in each step so as you can see
here we are only returning the name and
the bond which is only present in the
actor node we're not even utilizing the
movie node so why we should return that
and keep that in the last return
statement so before that we can add like
width Clause here so let's say we have
the width clause and we are only needing
the actor node here so it filters out
the movie node and it will not be used
or we cannot use it in the subsequent
statements so if you just return return
this the query will not be hampered but
if you just get only the movie then this
will give you the error that variable a
is not defined because that variable we
are not passing it using the with Clause
so with Clause will really help you to
pass only the required variables or the
nodes or maybe relationships in the next
steps so let's discuss that with the
example which will really make sense
because this is not making much sense
and you will not able to understand it
correctly so let's say if you have the
actor node and the movie node then we
have another operation on the movie node
so here let's say we have to just then
match on this movie so we are just using
the math statement again and here we can
maybe remove this and just give it star
so here we are going to have like all
the nodes which are connected further to
this movie so we have all the
relationships so we can denote like Star
here and also here we can denote just
empty or maybe give some variable which
is like just node two right so here we
are getting all the nodes which are
connected to that particular movie so
here we are not at all using our actor
node so why we should keep that so to
only get the M node which is movie node
we can just directly filter it out using
the width M so
only you can use M variable which is
movie in the subsequent step where we
are matching it matching all the notes
and returning it in the last return
Clause so as you can see we got all the
connected notes here to that particular
movie so here if we just expand this we
got like the movie here we got like the
user then we have like another user
which rated this movie also we have like
the actor as well as the Director of
this movie and and all other three
actors and also Eric Amell is also
present here but we are not passing it
using the with Clause we are only
limiting it so with Clause will allow
you to just filter out and only pass the
certain information which is required
into the subsequent processes so this is
how you can use the with clause in the
cipher let's talk about the another
Clause now so next we have the unwind
here so unwind is basically expand list
into to the sequence of rows so it is
very easy to understand so here like
collect is used for converting some
nodes into into list and here we can use
unwind to just expand that list into a
sub sequence of rows so let's talk about
the unwind now so here let's say let's
create a list here so we have like two
users which are which are connected to
this movie so let's say we have like the
user here so no two will have the user
and if we just return this we got the
two user along with the movie so let's
say if we collect those users and just
collect note two as the nodes here and
let's return the nodes now so here we
got the nodes but collect will only make
sense when we are passing some
properties inside it so let's say if we
just pass the name of those users so
let's say we got the note 2. name name
here and once we return the names here
so let's re rename it as
names and return it as you can see we
got the list of the users that
particular movie has submitted ratings
to so basically we got the list but
let's say if you want to explore this
list and convert it into number of rows
then we can use the unwind operation
here so basically we have the list named
as names so in the next step let's
unwind those names here so let's unwind
it so we will unwind the names as like
the rows here and we will return the
rows now and if you return it as you can
see our components has converted into
the rows so you will say what is the
actual use of unwind here we are going
to see in the subsequent lectures where
we will see how to utilize this collect
and unwind operation in the complex
Cipher queries but for your basic
understanding collect will convert some
values into a list and the unwind will
unwind that list and convert it into the
individual values which you can return
it in different rows so this is very
simple to understand so this was all
about the projecting Clauses let's head
over to our next clause which is also
very important and used while
manipulating the data so now let's talk
about the writing Clauses here so here
we have like different WR writing
Clauses which you can use to manipulate
or create or delete the nodes in your
database so as you can see the first one
is the create so as the name suggest it
used to create the nodes and
relationships in your graph so let's
jump over to how to use a create Clause
so let's say if you want to create a
node in your graph so here we will use
the create clause and we will just
create another node so let's say we have
to create a person here so here we have
created a person so to create a person
you have to assign a particular label to
it it is very important because every
record will have some label in your
graph and you have to define the some
properties to that person so let's say
we have the name here then we have to
give the name so name will be let's say
I'll give my name and here our next is
like we have like some Edge so I'll be
having some ede here so this will create
a person node which has the label person
and also has the properties like name
and the age so this is how you can
create a person label and let's just
return it and see how it creates the
note so we'll return the a so as you can
see the node is created in our database
which has the label person which has the
age and the name so let's say and if you
want to create a relationship with this
node so if this person has acted in some
movie let's say so to to just do that
instead of create let's say we will just
match it so match will match that part
find that particular pattern which is
nothing but this particular node in our
database and after that we can just
create another node so here we will
create another node which is like the
movie here right and the movie will have
some certain name here so the name is
like the Hulk maybe for example so here
we also created a node here and to
create relationship we can also use like
a create Clause here so we can again use
the create clause and to define a
relationship we already have our
variables which is a which is tag to a
person which we just created and also we
have the movie which we are going to
create in this query so here a then we
got the relationship so relationship
type would be like acted in and then we
can give our newly created node which we
are going to create in this execution
which is M this is very simple but this
will throw an error because we don't
have any direction to our relationship
we should have some direction to our
relationship so here it will make sense
that person should act in a movie so the
direction will go from left to right so
just give that and we will return return
all the notes which is a comma M that's
it if you run this it will create the
movie node also it will create the actor
relationship between the person and the
movie node just return it and as you can
see we got the person node which was
already created so we have just grab
that using the match so this will look
out for this pattern which is in case
this person node and we got our movie
here which is the Hulk this was very
simple let's jump on to the next Clause
here so the next Clause is like delete
so delete basically as the name suggest
deletes the nodes or relationships or
the paths so any note to be deleted must
also have like Associated relationship
which has explicitly deleted so what it
means is before you can delete a node
you have to just delete all the
relationships which is assigned to it
otherwise it will throw an error let's
do that in our example so as you can see
we have already created the movie and
our user so let's just get this person
now and let's try to delete it so to
delete it we will just match that and
get the pattern which is nothing but the
node and we will try to delete this node
and let's see what happens if you
execute it as you can see we cannot
delete the node because it still has the
relationships so to delete this node you
must first delete its relationship so to
delete a relationship you just have to
grab this acted in and then delete it so
to delete a particular relationship you
have to write a match clause and then
you have to just go ahead and grab the
relationship which is taged to it and
then you have to delete the r here so
here you will able to delete this R and
then you can able to delete the note so
if you execute it as you can see we have
got like deleted one relationship here
and once you get this person and try to
delete it it will delete it right away
so if you delete this as you can see it
is giving as deleted one node so this is
how you can do that but there is another
work around or there is another way to
delete a particular node from your graph
which is that detach delete so with
detach delete you can delete a node or
set of notes and all the associated
relationships will automatically be
deleted this is pretty simple so here
let's say we got the actor node here and
let's jump on and get our IR Campbell
again so we will just match our person
so this node is not present and we will
just hold on to our Eric Campbell here
so we'll just give the Eric Campbell
don't have any H and let's return this
so if you return this node it has some
movie assigned to it so it has some
relationship and node connected to a
Campell node so if we just directly try
to delete it it will not allow us
because it still has the relationship
but we if use detach delete it will
delete the relationship including the
node which is nothing but the person
node so just detach and delete a and
there you go it deleted the node as well
as it deleted the relationship which we
got in the result here so this is how
you can use the create clause and delete
Clause to create and delete the nodes in
your graph database so the next one is
set operation so set operation is
nothing but updates the label on the
nodes as well as the properties on the
nodes and relationship so this is pretty
simple the set can add another label to
your node so if you have an actor node
which has like actor label and if you
want add like user label to it you can
easily do that using set and if you in
your actor if you want to add like
another property in that node you can
also use it using set let's do that now
so as you can see we got the Eric
Campbell here and it got deleted so
let's pick another actor here so we have
like the Anna Province here I don't know
the pronunciation of this so I'll just
paste it over here so as you can see we
got this particular person so we'll just
return the person a so let's say this
particular person has like two labels
actor and person but we also have to add
another label to it so our label could
be like director maybe so we can do that
using set operation so you have to just
use it between match and return so just
use set operation we'll get the variable
first that is the first step then do the
colon and you can add the next label so
here we will add like director here and
we will just return that node so if you
execute it as you can see we got the
director label added to our person node
or the actor node so here we can also
add another property let's say we have
to add like H property to it so we can
do that using the set operation so here
to just set some property you have to
you don't have to use colon you have to
use dot here so you can you give like
set a DOT and the property name so we
can give any property name here we can
maybe give like H and the H could be
maybe 30 right and if we return this it
will have the H mistakenly I have just
given the 300 here so I'll just rerun it
as you can see the H property is added
and the value is 30 this is so simple
and opposite to our set Clause there is
a remove Clause so remove Clause will be
used for removing a certain label or a
property from your node or the
relationship so for relationship it will
go same way you just have to assign a
variable to your relationships like
maybe you can give R we can just set
some property to it which is like R do H
is equal to 30 this is so simple as that
and remove will is the negation of the
set which removes any label or the
property from this note so let's remove
that director label and the H property
here so just instead of set just use
remove
here and you don't have to just give
equal to 30 because it already has the
value so this will remove the H property
from our person node or the actor node
so as you can see if you go into this
yeah the age is no longer there and also
just let's remove the director label as
well so we'll remove a here you have to
remember again you have to give like
colon because for for defining the label
you have to give colon and for defining
a property have to mention the dot here
so a
director and we will just return it
again and as you can see the director
label has been removed from our person
and actor note so this is how you can
easily remove the any property or label
in your graph and this will help you for
daily ingesting your nodes and
relationships or maybe you can say the
whole data into your graph so this is
like a 4 Clause so for each we update
the data within a list so whether it
could be like component of path or the
result of the aggregation so this is
part of complex Cipher so let's focus on
the basics first because we will not be
using for each very often while loading
the data it is only useful while loading
the data or getting the basic patterns
from your database so for easy
manipulation Reading Writing everything
this Clauses which we learned are very
much enough for you but in the next
lecture let's talk about some complex
Clauses like for each clauses also we
have the set Clauses we also have like
wear conditions pattern matching etc etc
there are so many options there are
different functions like mathematical
functions then we have the aggregation
functions to able to aggregate the
output and get the meaning out of your
data so this could be really fun but
let's stop here and in the next lecture
let's talk about the advanced Cipher
Clauses so I hope you understood this so
if you have any difficulties you can let
me know in the comments and we can talk
about it further that's it for today
I'll see you in the next lecture thanks
for watching
Browse More Related Video
5.0 / 5 (0 votes)