Constraints
Introduction
The following constraint types are available:
- Unique node property constraints
-
Unique property constraints ensure that property values are unique for all nodes with a specific label. Unique constraints do not mean that all nodes have to have a unique value for the properties — nodes without the property 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
-
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
-
Node key constraints ensure that, for a given label and set of properties:
-
All the properties exist on all the nodes with that label.
-
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.
-
Unique node property constraints, node property existence constraints and relationship property existence constraints are only available in Neo4j Enterprise Edition. Databases containing one of these constraint types cannot be opened using Neo4j Community Edition. |
Creating a constraint has the following implications on indexes:
-
Adding a unique property constraint on a property will also add a single-property index on that property, so such an index cannot be added separately.
-
Adding a node key constraint for a set of properties will also add a composite index on those properties, so such an index 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 unique property constraint is dropped and the single-property index on the property is still required, the index will need to be created explicitly.
-
If a node key constraint is dropped and the composite-property index on the properties is still required, the index will need to be created explicitly.
Additionally, the following is true for constraints:
-
A given label can have multiple constraints, and unique 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 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 not idempotent. An error will be thrown if you attempt to create the same constraint twice.
Syntax
Command | Description | Comment |
---|---|---|
|
Create a unique node property constraint. |
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 not idempotent. An error will be thrown if you attempt to create the same constraint twice. |
|
Create a node property existence constraint. |
|
|
Create a relationship property existence constraint. |
|
|
Create a node key constraint. |
|
|
Drop a constraint. |
|
|
List all constraints in the database. |
|
|
Drop a unique constraint without specifying a name. |
This syntax is deprecated. |
|
Drop an exists constraint without specifying a name. |
|
|
Drop a relationship property existence constraint without specifying a name. |
|
|
Drop a node key constraint without specifying a name. |
Creating a constraint requires the CREATE CONSTRAINT
privilege,
while dropping a constraint requires the DROP CONSTRAINT
privilege.
Listing constraints does not require any privileges.
Create a unique constraint
When creating a unique constraint, a name can be provided. The constraint ensures that your database will never contain more than one node with a specific label and one property value.
CREATE CONSTRAINT constraint_name ON (book:Book) ASSERT book.isbn IS UNIQUE
+-------------------+
| No data returned. |
+-------------------+
Unique constraints added: 1
Create a node that complies with unique property constraints
Create a Book
node with an isbn
that isn’t already in the database.
CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'})
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 2
Labels added: 1
Create a node that violates a unique property constraint
Create a Book
node with an isbn
that is already used in the database.
CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'})
In this case the node isn’t created in the graph.
Node(0) already exists with label `Book` and property `isbn` = '1449356265'
Failure to create a unique property constraint due to conflicting nodes
Create a unique property constraint on the property isbn
on nodes with the Book
label when there are two nodes with the same isbn
.
CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
In this case the constraint can’t be created because it is violated by existing data. We may choose to use Indexes for search performance instead or remove the offending nodes and then re-apply the constraint.
Unable to create CONSTRAINT ON ( book:Book ) ASSERT (book.isbn) IS UNIQUE:
Both Node(0) and Node(1) have the label `Book` and property `isbn` = '1449356265'
Create a node property existence constraint
When creating a node property existence constraint, a name can be provided. The constraint ensures that all nodes with a certain label have a certain property.
CREATE CONSTRAINT constraint_name ON (book:Book) ASSERT exists(book.isbn)
+-------------------+
| No data returned. |
+-------------------+
Property existence constraints added: 1
Create a node that complies with property existence constraints
Create a Book
node with an isbn
property.
CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'})
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 2
Labels added: 1
Create a node that violates a property existence constraint
Trying to create a Book
node without an isbn
property, given a property existence constraint on :Book(isbn)
.
CREATE (book:Book {title: 'Graph Databases'})
In this case the node isn’t created in the graph.
Node(0) with label `Book` must have the property `isbn`
Removing an existence constrained node property
Trying to remove the isbn
property from an existing node book
, given a property existence constraint on :Book(isbn)
.
MATCH (book:Book {title: 'Graph Databases'}) REMOVE book.isbn
In this case the property is not removed.
Node(0) with label `Book` must have the property `isbn`
Failure to create a node property existence constraint due to existing node
Create a constraint on the property isbn
on nodes with the Book
label when there already exists a node without an isbn
.
CREATE CONSTRAINT ON (book:Book) ASSERT exists(book.isbn)
In this case the constraint can’t be created because it is violated by existing data. We may choose to remove the offending nodes and then re-apply the constraint.
Unable to create CONSTRAINT ON ( book:Book ) ASSERT exists(book.isbn):
Node(0) with label `Book` must have the property `isbn`
Create a relationship property existence constraint
When creating a relationship property existence constraint, a name can be provided. The constraint ensures all relationships with a certain type have a certain property.
CREATE CONSTRAINT constraint_name ON ()-[like:LIKED]-() ASSERT exists(like.day)
+-------------------+
| No data returned. |
+-------------------+
Property existence constraints added: 1
Create a relationship that complies with property existence constraints
Create a LIKED
relationship with a day
property.
CREATE (user:User)-[like:LIKED {day: 'yesterday'}]->(book:Book)
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 2
Relationships created: 1
Properties set: 1
Labels added: 2
Create a relationship that violates a property existence constraint
Trying to create a LIKED
relationship without a day
property, given a property existence constraint :LIKED(day)
.
CREATE (user:User)-[like:LIKED]->(book:Book)
In this case the relationship isn’t created in the graph.
Relationship(0) with type `LIKED` must have the property `day`
Removing an existence constrained relationship property
Trying to remove the day
property from an existing relationship like
of type LIKED
, given a property existence constraint :LIKED(day)
.
MATCH (user:User)-[like:LIKED]->(book:Book) REMOVE like.day
In this case the property is not removed.
Relationship(0) with type `LIKED` must have the property `day`
Failure to create a relationship property existence constraint due to existing relationship
Create a constraint on the property day
on relationships with the LIKED
type when there already exists a relationship without a property named day
.
CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)
In this case the constraint can’t be created because it is violated by existing data. We may choose to remove the offending relationships and then re-apply the constraint.
Unable to create CONSTRAINT ON ()-[ liked:LIKED ]-() ASSERT exists(liked.day):
Relationship(0) with type `LIKED` must have the property `day`
Create a node key constraint
When creating a node key constraint, a name can be provided. The constraint ensures that all nodes with a particular label have a set of defined properties whose combined value is unique and all properties in the set are present.
CREATE CONSTRAINT constraint_name ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY
+-------------------+
| No data returned. |
+-------------------+
Node key constraints added: 1
Create a node that complies with node key constraints
Create a Person
node with both a firstname
and surname
property.
CREATE (p:Person {firstname: 'John', surname: 'Wood', age: 55})
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 3
Labels added: 1
Create a node that violates a node key constraint
Trying to create a Person
node without a surname
property, given a node key constraint on :Person(firstname, surname)
, will fail.
CREATE (p:Person {firstname: 'Jane', age: 34})
In this case the node isn’t created in the graph.
Node(0) with label `Person` must have the properties (firstname, surname)
Removing a NODE KEY
-constrained property
Trying to remove the surname
property from an existing node Person
, given a NODE KEY
constraint on :Person(firstname, surname)
.
MATCH (p:Person {firstname: 'John', surname: 'Wood'}) REMOVE p.surname
In this case the property is not removed.
Node(0) with label `Person` must have the properties (firstname, surname)
Failure to create a node key constraint due to existing node
Trying to create a node key constraint on the property surname
on nodes with the Person
label will fail when a node without a surname
already exists in the database.
CREATE CONSTRAINT ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY
In this case the node key constraint can’t be created because it is violated by existing data. We may choose to remove the offending nodes and then re-apply the constraint.
Unable to create CONSTRAINT ON ( person:Person ) ASSERT exists(person.firstname,
person.surname):
Node(0) with label `Person` must have the properties (firstname, surname)
Drop a constraint
A constraint can be dropped using the name with the DROP CONSTRAINT constraint_name
command. It is the same command for unique property, property existence and node key constraints.
DROP CONSTRAINT constraint_name
+-------------------+
| No data returned. |
+-------------------+
Named constraints removed: 1
List constraints
Calling the built-in procedure db.constraints
will list all constraints, including their names.
CALL db.constraints
+------------------------------------------------------------------------------------+
| name | description |
+------------------------------------------------------------------------------------+
| "constraint_ca412c3d" | "CONSTRAINT ON ( book:Book ) ASSERT (book.isbn) IS UNIQUE" |
+------------------------------------------------------------------------------------+
1 row
Drop a unique constraint
By using DROP CONSTRAINT
, you remove a constraint from the database.
DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
+-------------------+
| No data returned. |
+-------------------+
Unique constraints removed: 1
Drop a node property existence constraint
By using DROP CONSTRAINT
, you remove a constraint from the database.
DROP CONSTRAINT ON (book:Book) ASSERT exists(book.isbn)
+-------------------+
| No data returned. |
+-------------------+
Property existence constraints removed: 1