Examples

Examples of how to manage constraints used for ensuring data integrity.

Node property uniqueness constraints

A node property uniqueness constraint ensures that all nodes with a particular label have a set of defined properties whose combined value is unique when existing.

Create a node property uniqueness constraint

When creating a property uniqueness constraint, a name can be provided.

Example 1. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name
FOR (book:Book) REQUIRE book.isbn IS UNIQUE
Result
+-------------------+
| No data returned. |
+-------------------+
Unique constraints added: 1

Handling existing constraints when creating a constraint

Creating an already existing constraint will fail. To avoid such an error, IF NOT EXISTS can be added to the CREATE command. This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another node property uniqueness constraint on the same schema already exists.

Example 2. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name IF NOT EXISTS
FOR (book:Book) REQUIRE book.isbn IS UNIQUE

Assuming no constraint with the given name or other node property uniqueness constraint on the same schema exists:

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

Specifying an index provider when creating a constraint

To create a property uniqueness constraint with a specific index provider for the backing index, the OPTIONS clause is used.

The index type of the backing index is set with the indexProvider option.

The only valid value for the index provider is:

  • range-1.0 Default

Example 3. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_with_options
FOR (n:Label) REQUIRE (n.prop1, n.prop2) IS UNIQUE
OPTIONS {
  indexProvider: 'range-1.0',
}
Result
+-------------------+
| No data returned. |
+-------------------+
Unique constraints added: 1

There is no valid index configuration values for the constraint-backing range indexes.

Creating an already existing constraint will fail

Example 4. CREATE CONSTRAINT

Create a property uniqueness constraint on the property title on nodes with the Book label, when that constraint already exists.

Query
CREATE CONSTRAINT FOR (book:Book) REQUIRE book.title IS UNIQUE

In this case the constraint can not be created because it already exists.

Error message
Constraint already exists:
Constraint( id=4, name='preExistingUnique', type='UNIQUENESS', schema=(:Book {title}), ownedIndex=3 )

Creating a constraint on the same schema as an existing index will fail

Example 5. CREATE CONSTRAINT

Create a property uniqueness constraint on the property wordCount on nodes with the Book label, when an index already exists on that label and property combination.

Query
CREATE CONSTRAINT FOR (book:Book) REQUIRE book.wordCount IS UNIQUE

In this case the constraint can not be created because there already exists an index covering that schema.

Error message
There already exists an index (:Book {wordCount}).
A constraint cannot be created until the index has been dropped.

Creating a node that complies with an existing constraint

Example 6. CREATE NODE

Create a Book node with an isbn that is not 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

Creating a node that violates an existing constraint will fail

Example 7. CREATE NODE

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 is not created in the graph.

Error message
Node(0) already exists with label `Book` and property `isbn` = '1449356265'

Creating a constraint when there exist conflicting nodes will fail

Example 8. CREATE CONSTRAINT

Create a property uniqueness constraint on the property isbn on nodes with the Book label when there are two nodes with the same isbn.

Query
CREATE CONSTRAINT FOR (book:Book) REQUIRE book.isbn IS UNIQUE

In this case the constraint can not be created because it is violated by existing data. Either use Indexes for search performance instead, or remove the offending nodes and then re-apply the constraint.

Error message
Unable to create Constraint( name='constraint_62365a16', type='UNIQUENESS', schema=(:Book {isbn}) ):
Both Node(0) and Node(1) have the label `Book` and property `isbn` = '1449356265'

Node property existence constraints

A node property existence constraint ensures that all nodes with a certain label have a certain property.

Create a node property existence constraint

When creating a node property existence constraint, a name can be provided.

Example 9. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name
FOR (book:Book) REQUIRE book.isbn IS NOT NULL
Result
+-------------------+
| No data returned. |
+-------------------+
Property existence constraints added: 1

Handling existing constraints when creating a constraint

Creating an already existing constraint will fail. To avoid such an error, IF NOT EXISTS can be added to the CREATE command. This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another node property existence constraint on the same schema already existed.

Example 10. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name IF NOT EXISTS
FOR (book:Book) REQUIRE book.isbn IS NOT NULL

Assuming a constraint with the name constraint_name already existed:

Result
+--------------------------------------------+
| No data returned, and nothing was changed. |
+--------------------------------------------+

Creating an already existing constraint will fail

Example 11. CREATE CONSTRAINT

Create a node property existence constraint on the property title on nodes with the Book label, when that constraint already exists.

Query
CREATE CONSTRAINT booksShouldHaveTitles
FOR (book:Book) REQUIRE book.title IS NOT NULL

In this case the constraint can not be created because it already exists.

Error message
Constraint already exists:
Constraint( id=3, name='preExistingNodePropExist', type='NODE PROPERTY EXISTENCE', schema=(:Book {title}) )

Creating a node that complies with an existing constraint

Example 12. CREATE NODE

Create a Book node with an isbn property.

Query
CREATE (book:Book {isbn: '1449356265', title: 'Graph Databases'})
Result
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 2
Labels added: 1

Creating a node that violates an existing constraint will fail

Example 13. CREATE NODE

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 is not created in the graph.

Error message
Node(0) with label `Book` must have the property `isbn`

Removing an existence constrained node property will fail

Example 14. REMOVE 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`

Creating a constraint when there exist conflicting nodes will fail

Example 15. CREATE CONSTRAINT

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 FOR (book:Book) REQUIRE book.isbn IS NOT NULL

In this case the constraint can’t be created because it is violated by existing data. Remove the offending nodes and then re-apply the constraint.

Error message
Unable to create Constraint( type='NODE PROPERTY EXISTENCE', schema=(:Book {isbn}) ):
Node(0) with label `Book` must have the property `isbn`

Relationship property existence constraints

A relationship property existence constraint ensures that all relationships with a certain type have a certain property.

Create a relationship property existence constraint

When creating a relationship property existence constraint, a name can be provided.

Example 16. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name
FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL
Result
+-------------------+
| No data returned. |
+-------------------+
Property existence constraints added: 1

Handling existing constraints when creating a constraint

Creating an already existing constraint will fail. To avoid such an error, IF NOT EXISTS can be added to the CREATE command. This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another relationship property existence constraint on the same schema already existed.

Example 17. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name
IF NOT EXISTS FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL

Assuming a constraint with the name constraint_name already existed:

Result
+--------------------------------------------+
| No data returned, and nothing was changed. |
+--------------------------------------------+

Creating an already existing constraint will fail

Example 18. CREATE CONSTRAINT

Create a named relationship property existence constraint on the property week on relationships with the LIKED type, when a constraint with the given name already exists.

Query
CREATE CONSTRAINT relPropExist
FOR ()-[like:LIKED]-() REQUIRE like.week IS NOT NULL

In this case the constraint can not be created because there already exists a constraint with the given name.

Error message
There already exists a constraint called 'relPropExist'.

Creating a relationship that complies with an existing constraint

Example 19. CREATE RELATIONSHIP

Create a LIKED relationship with a 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

Creating a relationship that violates an existing constraint will fail

Example 20. CREATE RELATIONSHIP

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 is not created in the graph.

Error message
Relationship(0) with type `LIKED` must have the property `day`

Removing an existence constrained relationship property will fail

Example 21. REMOVE 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`

Creating a constraint when there exist conflicting relationships will fail

Example 22. CREATE CONSTRAINT

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 FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL

In this case the constraint can not be created because it is violated by existing data. Remove the offending relationships and then re-apply the constraint.

Error message
Unable to create Constraint( type='RELATIONSHIP PROPERTY EXISTENCE', schema=-[:LIKED {day}]- ):
Relationship(0) with type `LIKED` must have the property `day`

Node key constraints

A node key 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 a node key constraint

When creating a node key constraint, a name can be provided.

Example 23. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name
FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY
Result
+-------------------+
| No data returned. |
+-------------------+
Node key constraints added: 1

Handling existing constraints when creating a constraint

Creating an already existing constraint will fail. To avoid such an error, IF NOT EXISTS can be added to the CREATE command. This will ensure that no error is thrown and no constraint is created if any other constraint with the given name or another node key constraint on the same schema already exists.

Example 24. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_name IF NOT EXISTS
FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY

Assuming a node key constraint on (:Person {firstname, surname}) already existed:

Result
+--------------------------------------------+
| No data returned, and nothing was changed. |
+--------------------------------------------+

Specifying an index provider when creating a constraint

To create a node key constraint with a specific index provider for the backing index, the OPTIONS clause is used.

The index type of the backing index is set with the indexProvider option.

The only valid value for the index provider is:

  • range-1.0 Default

Example 25. CREATE CONSTRAINT
Query
CREATE CONSTRAINT constraint_with_provider
FOR (n:Label) REQUIRE (n.prop1) IS NODE KEY
OPTIONS {
  indexProvider: 'range-1.0'
}
Result
+-------------------+
| No data returned. |
+-------------------+
Node key constraints added: 1

There is no valid index configuration values for the constraint-backing range indexes.

Node key and property uniqueness constraints are not allowed on the same schema

Example 26. CREATE CONSTRAINT

Create a node key constraint on the properties firstname and age on nodes with the Person label, when a property uniqueness constraint already exists on the same label and property combination.

Query
CREATE CONSTRAINT FOR (p:Person) REQUIRE (p.firstname, p.age) IS NODE KEY

In this case the constraint can not be created because there already exist a conflicting constraint on that label and property combination.

Error message
Constraint already exists:
Constraint( id=4, name='preExistingUnique', type='UNIQUENESS', schema=(:Person {firstname, age}), ownedIndex=3 )

Creating a constraint on same name as an existing index will fail

Example 27. CREATE CONSTRAINT

Create a named node key constraint on the property title on nodes with the Book label, when an index already exists with the given name.

Query
CREATE CONSTRAINT bookTitle
FOR (book:Book) REQUIRE book.title IS NODE KEY

In this case the constraint can’t be created because there already exists an index with the given name.

Error message
There already exists an index called 'bookTitle'.

Creating a node that complies with an existing constraint

Example 28. CREATE NODE

Create a Person node with both a firstname and surname property.

Query
CREATE (p:Person {firstname: 'John', surname: 'Wood', age: 55})
Result
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 1
Properties set: 3
Labels added: 1

Creating a node that violates an existing constraint will fail

Example 29. CREATE NODE

Trying to create a Person node without a surname property, given a node key constraint on :Person(firstname, surname), will fail.

Query
CREATE (p:Person {firstname: 'Jane', age: 34})

In this case the node is not created in the graph.

Error message
Node(0) with label `Person` must have the properties (`firstname`, `surname`)

Removing a NODE KEY-constrained property will fail

Example 30. REMOVE PROPERTY

Trying to remove the surname property from an existing node Person, given a NODE KEY constraint on :Person(firstname, surname).

Query
MATCH (p:Person {firstname: 'John', surname: 'Wood'}) REMOVE p.surname

In this case the property is not removed.

Error message
Node(0) with label `Person` must have the properties (`firstname`, `surname`)

Creating a constraint when there exist conflicting node will fail

Example 31. CREATE CONSTRAINT

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.

Query
CREATE CONSTRAINT FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY

In this case the node key constraint can not be created because it is violated by existing data. Either use Indexes for search performance instead, or remove the offending nodes and then re-apply the constraint.

Error message
Unable to create Constraint( type='NODE KEY', schema=(:Person {firstname, surname}) ):
Node(0) with label `Person` must have the properties (`firstname`, `surname`)

Drop a constraint by name

Drop a constraint

A constraint can be dropped using the name with the DROP CONSTRAINT constraint_name command. It is the same command for uniqueness, property existence, and node key constraints. The name of the constraint can be found using the SHOW CONSTRAINTS command, given in the output column name.

Example 32. DROP CONSTRAINT
Query
DROP CONSTRAINT constraint_name
Result
+-------------------+
| No data returned. |
+-------------------+
Named constraints removed: 1

Drop a non-existing constraint

If it is uncertain if any constraint with a given name exists and you want to drop it if it does but not get an error should it not, use IF EXISTS. It is the same command for uniqueness, property existence, and node constraints.

Example 33. DROP CONSTRAINT
Query
DROP CONSTRAINT missing_constraint_name IF EXISTS
Result
+--------------------------------------------+
| No data returned, and nothing was changed. |
+--------------------------------------------+

Listing constraints

Listing all constraints

To list all constraints with the default output columns, the SHOW CONSTRAINTS command can be used. If all columns are required, use SHOW CONSTRAINTS YIELD *.

One of the output columns from SHOW CONSTRAINTS is the name of the constraint. This can be used to drop the constraint with the DROP CONSTRAINT command.

Example 34. SHOW CONSTRAINTS
Query
SHOW CONSTRAINTS
+---------------------------------------------------------------------------------------------------+
| id | name             | type         | entityType | labelsOrTypes | properties | ownedIndex       |
+---------------------------------------------------------------------------------------------------+
| 4  | "isbnConstraint" | "UNIQUENESS" | "NODE"     | ["Book"]      | ["isbn"]   | "isbnConstraint" |
+---------------------------------------------------------------------------------------------------+
2 rows

Listing constraints with filtering

One way of filtering the output from SHOW CONSTRAINTS by constraint type is the use of type keywords, listed in the syntax for listing constraints type filter table. For example, to show only property uniqueness constraints, use SHOW UNIQUENESS CONSTRAINTS. Another more flexible way of filtering the output is to use the WHERE clause. An example is to only show constraints on relationships.

Example 35. SHOW CONSTRAINTS
Query
SHOW EXISTENCE CONSTRAINTS
WHERE entityType = 'RELATIONSHIP'

This will only return the default output columns. To get all columns, use SHOW INDEXES YIELD * WHERE ....

+---------------------------------------------------------------------------------------------------------------------------+
| id | name                  | type                              | entityType     | labelsOrTypes | properties | ownedIndex |
+---------------------------------------------------------------------------------------------------------------------------+
| 7  | "constraint_f076a74d" | "RELATIONSHIP_PROPERTY_EXISTENCE" | "RELATIONSHIP" | ["KNOWS"]     | ["since"]  | <null>     |
+---------------------------------------------------------------------------------------------------------------------------+
1 row