Neo4j: Constraints Cheat Sheet

Creating an Index

CREATE INDEX ON:label (node)  

Deleting an Index

DROP INDEX ON:label(node) 

Create a unique node property constraint

CREATE CONSTRAINT [constraint_name]
ON (n:LabelName)
ASSERT n.propertyName IS UNIQUE

Create a node property existence constraint

CREATE CONSTRAINT [constraint_name]
ON (n:LabelName)
ASSERT EXISTS (n.propertyName)

Create a relationship property existence constraint

CREATE CONSTRAINT [constraint_name]
ON ()-[R:RELATIONSHIP_TYPE]-()
ASSERT EXISTS (R.propertyName)

Create a node key constraint

CREATE CONSTRAINT [constraint_name]
ON (n:LabelName)
ASSERT (n.propertyName_1,
n.propertyName_2,
…
n.propertyName_n)
IS NODE KEY

Drop a constraint

DROP CONSTRAINT constraint_name

List all constraints in the database

CALL db.constraints

Drop a unique constraint without specifying a name

DROP CONSTRAINT
ON (n:LabelName)
ASSERT n.propertyName IS UNIQUE

Drop an exists constraint without specifying a name

DROP CONSTRAINT
ON (n:LabelName)
ASSERT EXISTS (n.propertyName)

Drop a relationship property existence constraint without specifying a name

DROP CONSTRAINT
ON ()-[R:RELATIONSHIP_TYPE]-()
ASSERT EXISTS (R.propertyName)

Drop a node key constraint without specifying a name

DROP CONSTRAINT
ON (n:LabelName)
ASSERT (n.propertyName_1,
n.propertyName_2,
…
n.propertyName_n)
IS NODE KEY

Leave a Reply

Your email address will not be published. Required fields are marked *