3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Neo4j: propertyにユニーク制約を設置する

Posted at

スキーマレスなグラフデータベースである Neo4j の Node に対して、その Property ごとにユニーク制約を持たせる方法を記述します。

ユニーク制約の例

Country Label のついた Node に name property をつけて、同一名の国は作成できないように制約をつけたい

ユニーク制約の設定方法

Neo4j は本来スキーマレスなので、 Country Label をつけたとしても、そこに name property があるかどうかもわかりませんし、同じ名前でそれをつける事もできます。

ユニーク制約をProperty につけるには、Constraint という機能を使います。

Constraint はあらかじめ設定しておく事で Query 実行時に効果を発揮します。

Country Labelにおいて、name property がユニークになるConstraintの作成例

CREATE CONSTRAINT ON (country:Country) ASSERT country.name IS UNIQUE

自分が設定したConstraintを一覧確認

call db.constraints

上で作成したConstraintを削除

DROP CONSTRAINT ON (country:Country) ASSERT country.name IS UNIQUE

制約

  • 複数のLabelをまたいで制約をつける事はできません!!
  • 複数のLabelを跨ぎたい場合は共通のLabelを作って常にそのLabelを付加する事で対応します
3
4
0

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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?