3.5.2. Constraints

Neo4j helps enforce data integrity with the use of constraints. Constraints can be applied to either nodes or relationships. Unique node property constraints can be created, as well as node and relationship property existence constraints.

You can use unique property constraints to 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.

You can use property existence constraints to ensure that a property exists for all nodes with a specific label or for all relationships with a specific type. All queries that try to create new nodes or relationships without the property, or queries that try to remove the mandatory property will now fail.

Property existence constraints are only available in Neo4j Enterprise Edition. Note that databases with property existence constraints cannot be opened using Neo4j Community Edition.

You can have multiple constraints for a given label and you can also combine unique and property existence constraints on the same property.

Remember that 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'.

Note that adding a unique property constraint on a property will also add an index on that property, so you cannot add such an index separately. Cypher will use that index for lookups just like other indexes. If you drop a unique property constraint and still want an index on the property, you will have to create the index.

3.5.2.1. Unique node property constraints

Create uniqueness constraint

To create a constraint that makes sure that your database will never contain more than one node with a specific label and one property value, use the IS UNIQUE syntax.

Query. 

CREATE CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE

Result. 

+-------------------+
| No data returned. |
+-------------------+
Unique constraints added: 1

Drop uniqueness constraint

By using DROP CONSTRAINT, you remove a constraint from the database.

Query. 

DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE

Result. 

+-------------------+
| No data returned. |
+-------------------+
Unique constraints removed: 1

Create a node that complies with unique property constraints

Create a Book node with an isbn that isn’t already in the database.

Query. 

CREATE (book:Book { isbn: '1449356265', title: 'Graph Databases' })

Result. 

+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 2
Labels added: 1

Create a node that breaks a unique property constraint

Create a Book node with an isbn that is already used in the database.

Query. 

CREATE (book:Book { isbn: '1449356265', title: 'Graph Databases' })

In this case the node isn’t created in the graph.

Error message. 

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.

Query. 

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 Section 3.5.1, “Indexes” instead or remove the offending nodes and then re-apply the constraint.

Error message. 

Unable to create CONSTRAINT ON ( book:Book ) ASSERT book.isbn IS UNIQUE:
Multiple nodes with label `Book` have property `isbn` = '1449356265':
  node(0)
  node(1)

3.5.2.2. Node property existence constraints

Create node property existence constraint

To create a constraint that makes sure that all nodes with a certain label have a certain property, use the ASSERT exists(variable.propertyName) syntax.

Query. 

CREATE CONSTRAINT ON (book:Book) ASSERT exists(book.isbn)

Result. 

+-------------------+
| No data returned. |
+-------------------+
Property existence constraints added: 1

Drop node property existence constraint

By using DROP CONSTRAINT, you remove a constraint from the database.

Query. 

DROP CONSTRAINT ON (book:Book) ASSERT exists(book.isbn)

Result. 

+-------------------+
| No data returned. |
+-------------------+
Property existence constraints removed: 1

Create a node that complies with property existence constraints

Create a Book node with an existing isbn property.

Query. 

CREATE (book:Book { isbn: '1449356265', title: 'Graph Databases' })

Result. 

+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 2
Labels added: 1

Create a node that breaks a property existence constraint

Trying to create a Book node without an isbn property, given a property existence constraint on :Book(isbn).

Query. 

CREATE (book:Book { title: 'Graph Databases' })

In this case the node isn’t created in the graph.

Error message. 

Node 1 with label "Book" must have the property "isbn" due to a constraint

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).

Query. 

MATCH (book:Book { title: 'Graph Databases' })
REMOVE book.isbn

In this case the property is not removed.

Error message. 

Node 0 with label "Book" must have the property "isbn" due to a constraint

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.

Query. 

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.

Error message. 

Unable to create CONSTRAINT ON ( book:Book ) ASSERT exists(book.isbn):
Node(0) with label `Book` has no value for property `isbn`

3.5.2.3. Relationship property existence constraints

Create relationship property existence constraint

To create a constraint that makes sure that all relationships with a certain type have a certain property, use the ASSERT exists(variable.propertyName) syntax.

Query. 

CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)

Result. 

+-------------------+
| No data returned. |
+-------------------+
Property existence constraints added: 1

Drop relationship property existence constraint

To remove a constraint from the database, use DROP CONSTRAINT.

Query. 

DROP CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)

Result. 

+-------------------+
| No data returned. |
+-------------------+
Property existence constraints removed: 1

Create a relationship that complies with property existence constraints

Create a LIKED relationship with an existing day property.

Query. 

CREATE (user:User)-[like:LIKED { day: 'yesterday' }]->(book:Book)

Result. 

+-------------------+
| No data returned. |
+-------------------+
Nodes created: 2
Relationships created: 1
Properties set: 1
Labels added: 2

Create a relationship that breaks a property existence constraint

Trying to create a LIKED relationship without a day property, given a property existence constraint :LIKED(day).

Query. 

CREATE (user:User)-[like:LIKED]->(book:Book)

In this case the relationship isn’t created in the graph.

Error message. 

Relationship 1 with type "LIKED" must have the property "day" due to a constraint

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).

Query. 

MATCH (user:User)-[like:LIKED]->(book:Book)
REMOVE like.day

In this case the property is not removed.

Error message. 

Relationship 0 with type "LIKED" must have the property "day" due to a constraint

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.

Query. 

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.

Error message. 

Unable to create CONSTRAINT ON ()-[ liked:LIKED ]-() ASSERT exists(liked.day):
Relationship(0) with type `LIKED` has no value for property `day`