Examples
Examples of how to manage constraints used for ensuring data integrity.
Unique node property constraints
-
Create a unique constraint only if it does not already exist
-
Create a unique constraint with specified index provider and configuration
-
Failure to create an already existing unique property constraint
-
Failure to create a unique property constraint on same schema as existing index
-
Create a node that complies with unique property constraints
-
Failure to create a unique property constraint due to conflicting nodes
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
FOR (book:Book) REQUIRE book.isbn IS UNIQUE
+-------------------+ | No data returned. | +-------------------+ Unique constraints added: 1
Create a unique constraint only if it does not already exist
If it is not known whether a constraint exists or not, add IF NOT EXISTS
to ensure it does.
The uniqueness constraint ensures that your database will never contain more than one node with a specific label and one property value.
No constraint will be created if any other constraint with that name or another uniqueness constraint on the same schema already exists. |
CREATE CONSTRAINT constraint_name IF NOT EXISTS
FOR (book:Book) REQUIRE book.isbn IS UNIQUE
+-------------------+ | No data returned. | +-------------------+ Unique constraints added: 1
Create a unique constraint with specified index provider and configuration
To create a unique constraint with a specific index provider and configuration for the backing index, the OPTIONS
clause is used.
Valid values for the index provider are native-btree-1.0
(deprecated), lucene+native-3.0
(deprecated), and range-1.0
(future index), default is native-btree-1.0
.
The index type of the backing index is set depending on the provider, the range-1.0
generates a future range index while the other providers generates a B-tree index.
The range index have no configuration settings.
The valid B-tree configuration settings are:
-
spatial.cartesian.min
-
spatial.cartesian.max
-
spatial.cartesian-3d.min
-
spatial.cartesian-3d.max
-
spatial.wgs-84.min
-
spatial.wgs-84.max
-
spatial.wgs-84-3d.min
-
spatial.wgs-84-3d.max
Non-specified settings have their respective default values.
In Neo4j 4.4, B-tree index-backed constraints are still the correct alternative to use.
CREATE CONSTRAINT constraint_with_options
FOR (n:Label) REQUIRE (n.prop1, n.prop2) IS UNIQUE
OPTIONS {
indexProvider: 'lucene+native-3.0',
indexConfig: {
`spatial.wgs-84.min`: [-100.0, -80.0],
`spatial.wgs-84.max`: [100.0, 80.0]
}
}
Specifying index provider and configuration can be done individually.
+-------------------+ | No data returned. | +-------------------+ Unique constraints added: 1
Failure to create an already existing unique property constraint
Create a unique property constraint on the property title
on nodes with the Book
label, when that constraint already exists.
CREATE CONSTRAINT FOR (book:Book) REQUIRE book.title IS UNIQUE
In this case the constraint can not be created because it already exists.
Failure to create a unique property constraint on same schema as existing index
Create a unique property constraint on the property wordCount
on nodes with the Book
label, when an index already exists on that label and property combination.
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.
Create a node that complies with unique property constraints
Create a Book
node with an isbn
that is not 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 is not created in the graph.
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 FOR (book:Book) REQUIRE book.isbn IS UNIQUE
In this case the constraint can not be created because it is violated by existing data. You may choose to use Indexes for search performance instead or remove the offending nodes and then re-apply the constraint.
Node property existence constraints
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
FOR (book:Book) REQUIRE book.isbn IS NOT NULL
+-------------------+ | No data returned. | +-------------------+ Property existence constraints added: 1
Create a node property existence constraint only if it does not already exist
If it is not known whether a constraint exists or not, add IF NOT EXISTS
to ensure it does.
The node property existence constraint ensures that all nodes with a certain label have a certain property.
No constraint will be created if any other constraint with that name or another node property existence constraint on the same schema already exists.
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:
+--------------------------------------------+ | No data returned, and nothing was changed. | +--------------------------------------------+
Failure to create an already existing node property existence constraint
Create a node property existence constraint on the property title
on nodes with the Book
label, when that constraint already exists.
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.
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 is not created in the graph.
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.
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 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. We may choose to remove the offending nodes and then re-apply the constraint.
Relationship property existence constraints
-
Create a relationship property existence constraint only if it does not already exist
-
Failure to create an already existing relationship property existence constraint
-
Create a relationship that complies with property existence constraints
-
Create a relationship that violates a property existence constraint
-
Failure to create a relationship property existence constraint due to existing relationship
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
FOR ()-[like:LIKED]-() REQUIRE like.day IS NOT NULL
+-------------------+ | No data returned. | +-------------------+ Property existence constraints added: 1
Create a relationship property existence constraint only if it does not already exist
If it is not known whether a constraint exists or not, add IF NOT EXISTS
to ensure it does.
The relationship property existence constraint ensures all relationships with a certain type have a certain property.
No constraint will be created if any other constraint with that name or another relationship property existence constraint on the same schema already exists.
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:
+--------------------------------------------+ | No data returned, and nothing was changed. | +--------------------------------------------+
Failure to create an already existing relationship property existence constraint
Create a named relationship property existence constraint on the property week
on relationships with the LIKED
type, when a constraint with that name already exists.
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 that name.
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 is not created in the graph.
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.
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 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. We may choose to remove the offending relationships and then re-apply the constraint.
Node key constraints
-
Create a node key constraint only if it does not already exist
-
Create a node key constraint with specified index configuration
-
Failure to create a node key constraint when a unique property constraint exists on the same schema
-
Failure to create a node key constraint with the same name as existing index
-
Failure to create a node key constraint due to existing node
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
FOR (n:Person) REQUIRE (n.firstname, n.surname) IS NODE KEY
+-------------------+ | No data returned. | +-------------------+ Node key constraints added: 1
Create a node key constraint only if it does not already exist
If it is not known whether a constraint exists or not, add IF NOT EXISTS
to ensure it does.
The 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.
No constraint will be created if any other constraint with that name or another node key constraint on the same schema already exists.
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:
+--------------------------------------------+ | No data returned, and nothing was changed. | +--------------------------------------------+
Create a node key constraint with specified index provider
To create a node key constraint with a specific index provider for the backing index, the OPTIONS
clause is used.
Valid values for the index provider are native-btree-1.0
(deprecated), lucene+native-3.0
(deprecated), and range-1.0
(future index), default is native-btree-1.0
.
The index type of the backing index is set depending on the provider, the range-1.0
generates a future range index while the other providers generates a B-tree index.
In Neo4j 4.4, B-tree index-backed constraints are still the correct alternative to use.
CREATE CONSTRAINT constraint_with_provider
FOR (n:Label) REQUIRE (n.prop1) IS NODE KEY
OPTIONS {indexProvider: 'native-btree-1.0'}
B-tree providers can be combined with specifying index configuration.
+-------------------+ | No data returned. | +-------------------+ Node key constraints added: 1
Create a node key constraint with specified index configuration
To create a node key constraint with a specific index configuration for the backing index, the OPTIONS
clause is used.
The index type of the backing index is set depending on the provider and future range indexes have no configuration settings.
The valid B-tree configuration settings are:
-
spatial.cartesian.min
-
spatial.cartesian.max
-
spatial.cartesian-3d.min
-
spatial.cartesian-3d.max
-
spatial.wgs-84.min
-
spatial.wgs-84.max
-
spatial.wgs-84-3d.min
-
spatial.wgs-84-3d.max
Non-specified settings have their respective default values.
CREATE CONSTRAINT constraint_with_config
FOR (n:Label) REQUIRE (n.prop2) IS NODE KEY
OPTIONS {
indexConfig: {
`spatial.cartesian.min`: [-100.0, -100.0],
`spatial.cartesian.max`: [100.0, 100.0]
}
}
Can be combined with specifying a B-tree index provider.
+-------------------+ | No data returned. | +-------------------+ Node key constraints added: 1
Failure to create a node key constraint when a unique property constraint exists on the same schema
Create a node key constraint on the properties firstname
and age
on nodes with the Person
label, when a unique property constraint already exists on the same label and property combination.
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.
Failure to create a node key constraint with the same name as existing index
Create a named node key constraint on the property title
on nodes with the Book
label, when an index already exists with that name.
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 that name.
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 is not created in the graph.
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.
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 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. We may choose to remove the offending nodes and then re-apply the constraint.
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 unique property, 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
.
DROP CONSTRAINT constraint_name
+-------------------+ | 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 unique property, property existence and node key constraints.
DROP CONSTRAINT missing_constraint_name IF EXISTS
+--------------------------------------------+ | 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
+----------------------------------------------------------------------------------------------------+ | id | name | type | entityType | labelsOrTypes | properties | ownedIndexId | +----------------------------------------------------------------------------------------------------+ | 4 | "constraint_62365a16" | "UNIQUENESS" | "NODE" | ["Book"] | ["isbn"] | 3 | +----------------------------------------------------------------------------------------------------+ 1 row
Listing constraints with filtering
One way of filtering the output from SHOW CONSTRAINTS
by constraint type is the use of type keywords,
listed in Syntax for listing constraints.
For example, to show only unique node property constraints, use SHOW UNIQUE CONSTRAINTS
.
Another more flexible way of filtering the output is to use the WHERE
clause.
An example is to only show constraints on relationships.
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 | ownedIndexId | +-----------------------------------------------------------------------------------------------------------------------------+ | 7 | "constraint_f076a74d" | "RELATIONSHIP_PROPERTY_EXISTENCE" | "RELATIONSHIP" | ["KNOWS"] | ["since"] | <null> | +-----------------------------------------------------------------------------------------------------------------------------+ 1 row
Deprecated syntax
Create a unique constraint using deprecated syntax
The unique constraint ensures that your database will never contain more than one node with a specific label and one property value.
CREATE CONSTRAINT ON (book:Book) ASSERT book.title IS UNIQUE
+-------------------+ | No data returned. | +-------------------+ Unique constraints added: 1
Drop a unique constraint
By using DROP CONSTRAINT
, a B-tree index-backed unique constraint is removed from the database.
DROP CONSTRAINT ON (book:Book) ASSERT book.isbn IS UNIQUE
+-------------------+ | No data returned. | +-------------------+ Unique constraints removed: 1
Create a node property existence constraint using deprecated syntax 1
The node property existence constraint ensures that all nodes with a certain label have a certain property.
CREATE CONSTRAINT ON (book:Book) ASSERT book.title IS NOT NULL
+-------------------+ | No data returned. | +-------------------+ Property existence constraints added: 1
Create a node property existence constraint using deprecated syntax 2
The node property existence constraint ensures that all nodes with a certain label have a certain property.
CREATE CONSTRAINT ON (book:Book) ASSERT exists(book.title)
+-------------------+ | No data returned. | +-------------------+ Property existence constraints added: 1
Drop a node property existence constraint
By using DROP CONSTRAINT
, a node property existence constraint is removed from the database.
DROP CONSTRAINT ON (book:Book) ASSERT exists(book.isbn)
+-------------------+ | No data returned. | +-------------------+ Property existence constraints removed: 1
Create a relationship property existence constraint using deprecated syntax 1
The relationship property existence constraint ensures all relationships with a certain type have a certain property.
CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT like.week IS NOT NULL
+-------------------+ | No data returned. | +-------------------+ Property existence constraints added: 1
Create a relationship property existence constraint using deprecated syntax 2
The relationship property existence constraint ensures all relationships with a certain type have a certain property.
CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.week)
+-------------------+ | No data returned. | +-------------------+ Property existence constraints added: 1
Drop a relationship property existence constraint
To remove a relationship property existence constraint from the database, use DROP CONSTRAINT
.
DROP CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)
+-------------------+ | No data returned. | +-------------------+ Property existence constraints removed: 1
Create a node key constraint using deprecated syntax
The 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 CONSTRAINT ON (n:Person) ASSERT (n.firstname) IS NODE KEY
+-------------------+ | No data returned. | +-------------------+ Node key constraints added: 1
Drop a node key constraint
Use DROP CONSTRAINT
to remove a B-tree index-backed node key constraint from the database.
DROP CONSTRAINT ON (n:Person) ASSERT (n.firstname, n.surname) IS NODE KEY
+-------------------+ | No data returned. | +-------------------+ Node key constraints removed: 1