Constraints

This section explains how to manage constraints used for ensuring data integrity.

Types of constraint

The following constraint types are available:

Unique node property constraints

Unique node property constraints, or node property uniqueness constraints, ensure that property values are unique for all nodes with a specific label. For property uniqueness constraints on multiple properties, the combination of the property values is unique. Node property uniqueness constraints do not require all nodes to have a unique value for the properties listed (nodes without all properties are not subject to this rule).

Unique relationship property constraints

This feature was introduced in Neo4j 5.7.

Unique relationship property constraints, or relationship property uniqueness constraints, ensure that property values are unique for all relationships with a specific type. For property uniqueness constraints on multiple properties, the combination of the property values is unique. Relationship property uniqueness constraints do not require all relationships to have a unique value for the properties listed (relationships without all properties are not subject to this rule).

Node property existence constraints Enterprise Edition

Node property existence constraints ensure that a property exists for all nodes with a specific label. Queries that try to create new nodes of the specified label, but without this property, will fail. The same is true for queries that try to remove the mandatory property.

Relationship property existence constraints Enterprise Edition

Relationship property existence constraints ensure that a property exists for all relationships with a specific type. All queries that try to create relationships of the specified type, but without this property, will fail. The same is true for queries that try to remove the mandatory property.

Node key constraints Enterprise Edition

Node key constraints ensure that, for a given label and set of properties:

  1. All the properties exist on all the nodes with that label.

  2. The combination of the property values is unique.

Queries attempting to do any of the following will fail:

  • Create new nodes without all the properties or where the combination of property values is not unique.

  • Remove one of the mandatory properties.

  • Update the properties so that the combination of property values is no longer unique.

Relationship key constraints Enterprise Edition

This feature was introduced in Neo4j 5.7.

Relationship key constraints ensure that, for a given type and set of properties:

  1. All the properties exist on all the relationships with that type.

  2. The combination of the property values is unique.

Queries attempting to do any of the following will fail:

  • Create new relationships without all the properties or where the combination of property values is not unique.

  • Remove one of the mandatory properties.

  • Update the properties so that the combination of property values is no longer unique.

Node key constraints, relationship key constraints, node property existence constraints, and relationship property existence constraints are not available in Neo4j Community Edition. Databases containing one of these constraint types cannot be opened using Neo4j Community Edition.

Implications on indexes

Creating a constraint has the following implications on indexes:

  • Adding a node key, relationship key, or property uniqueness constraint on a single property also adds an index on that property, and therefore, an index of the same index type, label/relationship type, and property combination cannot be added separately.

  • Adding a node key, relationship key, or property uniqueness constraint for a set of properties also adds an index on those properties, and therefore, an index of the same index type, label/relationship type, and properties combination cannot be added separately.

  • Cypher® will use these indexes for lookups just like other indexes. Refer to Indexes for search performance for more details on indexes.

  • If a node key, relationship key, or property uniqueness constraint is dropped and the backing index is still required, the index need to be created explicitly.

Additionally, the following is true for constraints:

  • A given label or relationship type can have multiple constraints, and uniqueness and property existence constraints can be combined on the same property.

  • Adding constraints is an atomic operation that can take a while — all existing data has to be scanned before Neo4j DBMS can turn the constraint 'on'.

  • Best practice is to give the constraint a name when it is created. If the constraint is not explicitly named, it will get an auto-generated name.

  • The constraint name must be unique among both indexes and constraints.

  • Constraint creation is by default not idempotent, and an error will be thrown if you attempt to create the same constraint twice. Using the keyword IF NOT EXISTS makes the command idempotent, and no error will be thrown if you attempt to create the same constraint twice.