Syntax
More details about the syntax descriptions can be found here. |
Syntax for creating constraints
Best practice when creating a constraint is to give the constraint a name. This name must be unique among both indexes and constraints. If a name is not explicitly given, a unique name will be auto-generated.
The CREATE CONSTRAINT
command is optionally idempotent.
This means its default behavior is to throw an error if an attempt is made to create the same constraint twice.
With the IF NOT EXISTS
flag, no error is thrown and nothing happens should a constraint with the same name or same schema and constraint type already exist.
It may still throw an error if conflicting data, indexes, or constraints exist.
Examples of this are nodes with missing properties, indexes with the same name, or constraints with same schema but a different conflicting constraint type.
For constraints that are backed by an index, the index provider for the backing index can be specified using the OPTIONS
clause.
Only one valid value exists for the index provider, range-1.0
, which is the default value.
There is no supported index configuration for range indexes.
Creating a constraint requires the |
Create a node property uniqueness constraint
This command creates a property uniqueness constraint on nodes with the specified label and properties.
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS [NODE] UNIQUE
[OPTIONS "{" option: value[, ...] "}"]
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS [NODE] UNIQUE
[OPTIONS "{" option: value[, ...] "}"]
Index provider can be specified using the OPTIONS
clause.
Create a relationship property uniqueness constraint
This command creates a property uniqueness constraint on relationships with the specified relationship type and properties.
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE r.propertyName IS [REL[ATIONSHIP]] UNIQUE
[OPTIONS "{" option: value[, ...] "}"]
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE (r.propertyName_1, ..., r.propertyName_n) IS [REL[ATIONSHIP]] UNIQUE
[OPTIONS "{" option: value[, ...] "}"]
Index provider can be specified using the OPTIONS
clause.
Create a node property existence constraint Enterprise Edition
This command creates a property existence constraint on nodes with the specified label and property.
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS NOT NULL
[OPTIONS "{" "}"]
There are no supported |
Create a relationship property existence constraint Enterprise Edition
This command creates a property existence constraint on relationships with the specified relationship type and property.
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE r.propertyName IS NOT NULL
[OPTIONS "{" "}"]
There are no supported |
Create a node key constraint Enterprise Edition
This command creates a node key constraint on nodes with the specified label and properties.
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS [NODE] KEY
[OPTIONS "{" option: value[, ...] "}"]
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS [NODE] KEY
[OPTIONS "{" option: value[, ...] "}"]
Index provider can be specified using the OPTIONS
clause.
Create a relationship key constraint Enterprise Edition
This command creates a relationship key constraint on relationships with the specified relationship type and properties.
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE r.propertyName IS [REL[ATIONSHIP]] KEY
[OPTIONS "{" option: value[, ...] "}"]
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE (r.propertyName_1, ..., r.propertyName_n) IS [REL[ATIONSHIP]] KEY
[OPTIONS "{" option: value[, ...] "}"]
Index provider can be specified using the OPTIONS
clause.
Syntax for dropping constraints
Dropping a constraint is done by specifying the name of the constraint.
DROP CONSTRAINT constraint_name [IF EXISTS]
This drop command is optionally idempotent. This means its default behavior is to throw an error if an attempt is made to drop the same constraint twice.
With the IF EXISTS
flag, no error is thrown and nothing happens should the constraint not exist.
Dropping a constraint requires the |
Syntax for listing constraints
List constraints in the database, either all or filtered on constraint type.
Listing constraints requires the |
The simple version of the command allows for a WHERE
clause and will give back the default set of output columns:
SHOW [
ALL
|NODE UNIQUE[NESS]
|REL[ATIONSHIP] UNIQUE[NESS]
|UNIQUE[NESS]
|NODE [PROPERTY] EXIST[ENCE]
|REL[ATIONSHIP] [PROPERTY] EXIST[ENCE]
|[PROPERTY] EXIST[ENCE]
|NODE KEY
|REL[ATIONSHIP] KEY
|KEY
] CONSTRAINT[S]
[WHERE expression]
To get the full set of output columns, a yield clause is needed:
SHOW [
ALL
|NODE UNIQUE[NESS]
|REL[ATIONSHIP] UNIQUE[NESS]
|UNIQUE[NESS]
|NODE [PROPERTY] EXIST[ENCE]
|REL[ATIONSHIP] [PROPERTY] EXIST[ENCE]
|[PROPERTY] EXIST[ENCE]
|NODE KEY
|REL[ATIONSHIP] KEY
|KEY
] CONSTRAINT[S]
YIELD { * | field[, ...] } [ORDER BY field[, ...]] [SKIP n] [LIMIT n]
[WHERE expression]
[RETURN field[, ...] [ORDER BY field[, ...]] [SKIP n] [LIMIT n]]
The type filtering keywords filters the returned constraints on constraint type:
Filter | Description |
---|---|
|
Returns all constraints, no filtering on constraint type. This is the default if none is given. |
|
Returns the node property uniqueness constraints. |
|
Returns the relationship property uniqueness constraints. |
|
Returns all property uniqueness constraints, for both nodes and relationships. This feature was introduced in Neo4j 5.3. |
|
Returns the node property existence constraints. |
|
Returns the relationship property existence constraints. |
|
Returns all property existence constraints, for both nodes and relationships. |
|
Returns the node key constraints. |
|
Returns the relationship key constraints. |
|
Returns all node and relationship key constraints. |
The returned columns from the show command is:
Column | Description | Type |
---|---|---|
|
The id of the constraint. Default Output |
|
|
Name of the constraint (explicitly set by the user or automatically assigned). Default Output |
|
|
The ConstraintType of this constraint ( |
|
|
Type of entities this constraint represents (nodes or relationship). Default Output |
|
|
The labels or relationship types of this constraint. Default Output |
|
|
The properties of this constraint. Default Output |
|
|
The name of the index associated with the constraint or |
|
|
The options passed to |
|
|
Statement used to create the constraint. |
|
Was this page helpful?