Constraints

This page contains an overview of the available constraints in Cypher®, and information about how constraints can impact indexes.

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

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 on which the constraint exists are not subject to this rule).

Unique relationship property constraints

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 on which the constraint exists are not subject to this rule).

Node property existence constraints

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

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 property type constraints

Node property type constraints ensure that a property have the required property type for all nodes with a specific label. Queries that try to add or modify this property to nodes of the specified label, but with a different property type, will fail. Node property type constraints do not require all nodes to have the property (nodes without the property on which the constraint exists are not subject to this rule).

For more information, see examples of node property type constraints.

Relationship property type constraints

Relationship property type constraints ensure that a property have the required property type for all relationships with a specific type. Queries that try to add or modify this property to relationships of the specified type, but with a different property type, will fail. Relationship property type constraints do not require all relationships to have the property (relationships without the property on which the constraint exists are not subject to this rule).

Node key constraints

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.

For more information, see examples of node key constraints.

Relationship key constraints

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.

For more information, see examples of relationship key constraints.

Multiple constraints on the same property combinations

Some constraint types are allowed on the same label/relationship type and property combination. For example, it is possible to have a uniqueness and an existence constraint on the same label/relationship type and property combination, though this would be the equivalent of having a node or relationship key constraint. A more useful example would be to combine a property type and an existence constraint to ensure that the property exists and has the given type.

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 Create, show, and delete indexes 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.