LoginSignup
2

More than 3 years have passed since last update.

Cypher QLでリレーションの存在するノードを削除するための方法

Posted at

Cypher QLとは?

Neo4jというグラフデータベースを扱うための、SQLライクな言語です。

Cypher QL(Cypher:サイファーと読む)は、Neo4jでグラフ構造のデータ処理を行うために開発した宣言型のグラフクエリ言語です。簡単に言うと、あらかじめ決まった機能を遂行する文やひな形のような構文の書式が存在し、それを組み合わせてクエリを作成し、データ処理を実行します。
(CREATIONLINE Inc.ホームページより)https://www.creationline.com/lab/7577

参照整合性問題

RDBと同様に、リレーションが存在するノード同士は、先に整合性を削除しないとノードを削除できない仕組みになっています。
Neo4jでは、全てのリレーションには始点と終点のノードが存在するということを規約としているようです。

Within the Neo4j graph database, there’s a guarantee that each relationship will always have a starting and ending node. This guarantee of referential integrity is core to the principles of reliability in which Neo4j concentrates its write architecture. To achieve this, ever time a relationship is being created or modified, Neo4j locks the nodes on both sides of the relationship.
https://www.graphgrid.com/understanding-detach-delete-in-cypher/

リレーションの存在するノードを削除するには

DETACH DELETEを使用します。

・リレーションをもつノードを作成

CREATE(p:Person{name:"Kento Yamazaki"})-[:ACTED_IN]->(m:Movie{title:"Kingdom"})
RETURN p,m

スクリーンショット 2019-05-18 14.32.48.png

・削除しようとすると…エラー!

MATCH(p:Person{name:"Kento Yamazaki"})-[:ACTED_IN]->(m:Movie{title:"Kingdom"})
DELETE p,m

スクリーンショット 2019-05-18 14.37.45.png

・DETACH DELETEを使用する

MATCH(p:Person{name:"Kento Yamazaki"})-[:ACTED_IN]->(m:Movie{title:"Kingdom"})
DETACH DELETE p,m

スクリーンショット 2019-05-18 14.38.49.png

無事削除されました。

最後に

まだNeo4jに触り始めたばかりなので、間違いなどありましたら、どんどんコメントしていただけると幸いです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2