14.2. Constraints

Neo4j helps enforce data integrity with the use of constraints. There are two types of constraints, unique property constraints and mandatory property 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 mandatory property constraints to ensure that a property exists for all nodes with a specific label. All queries that try to create new nodes without the property, or queries that try to remove the mandatory property will now fail.

You can have multiple constraints for a given label and you can also combine unique and mandatory property 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.

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.

Parameters 

{ }

Query 

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

Result

Unique constraints added: 1

(empty result)

Drop uniqueness constraint

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

Parameters 

{ }

Query 

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

Result

Unique constraints removed: 1

(empty result)

Create a node that complies with unique property constraints

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

Parameters 

{ }

Query 

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

Result

Nodes created: 1
Properties set: 2
Labels added: 1

(empty result)

Create a node that breaks a unique property constraint

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

Parameters 

{ }

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.

Parameters 

{ }

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

Create mandatory property constraint

To create a constraint that makes sure that all nodes with a certain label have a certain property, use the IS NOT NULL syntax.

Parameters 

{ }

Query 

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

Result

Mandatory property constraints added: 1

(empty result)

Drop mandatory property constraint

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

Parameters 

{ }

Query 

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

Result

Mandatory property constraints removed: 1

(empty result)

Create a node that complies with mandatory property constraints

Create a Book node with an existing isbn propery.

Parameters 

{ }

Query 

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

Result

Nodes created: 1
Properties set: 2
Labels added: 1

(empty result)

Create a node that breaks a mandatory property constraint

Create a Book node without an isbn.

Parameters 

{ }

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" does not have a "isbn" property

Removing a mandatory property

Trying to remove property isbn from an existing book.

Parameters 

{ }

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" does not have a "isbn" property

Failure to create a mandatory property 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.

Parameters 

{ }

Query 

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

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 book.isbn IS NOT NULL:
Node(0) with label `Book` has no value for property `isbn`