Syntax
This page contains the syntax for creating, listing, and dropping the constraints available in Neo4j.
More details about the syntax can be found in the Operations Manual → Cypher® syntax for administration commands.
CREATE CONSTRAINT
Constraints are created with the CREATE CONSTRAINT
command.
When creating a constraint, it is recommended to provide a constraint 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.
Creating a constraint requires the CREATE CONSTRAINT privilege.
|
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.
As of Neo4j 5.17, an informational notification is returned in case nothing happens showing the existing constraint which blocks the creation.
Create property uniqueness constraints
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS [NODE] UNIQUE
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS [NODE] UNIQUE
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE r.propertyName IS [REL[ATIONSHIP]] UNIQUE
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE (r.propertyName_1, ..., r.propertyName_n) IS [REL[ATIONSHIP]] UNIQUE
For examples on how to create property uniqueness constraints, see Create, show, and drop constraints → Create property uniqueness constraint. Property uniqueness constraints are index-backed.
Create property existence constraints
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS NOT NULL
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE r.propertyName IS NOT NULL
For examples on how to create property existence constraints, see Create, show, and drop constraints → Create property existence constraints.
Create property type constraints
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName {[IS] :: | IS TYPED} <TYPE>
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE r.propertyName {[IS] :: | IS TYPED} <TYPE>
The three variations of the expression, IS ::
, ::
, and IS TYPED
are syntactic synonyms for the same expression.
The preferred syntax is the IS ::
variant.
Where <TYPE>
is one of the following property types:
-
BOOLEAN
-
STRING
-
INTEGER
-
FLOAT
-
DATE
-
LOCAL TIME
-
ZONED TIME
-
LOCAL DATETIME
-
ZONED DATETIME
-
DURATION
-
POINT
-
LIST<BOOLEAN NOT NULL>
Introduced in 5.10 -
LIST<STRING NOT NULL>
Introduced in 5.10 -
LIST<INTEGER NOT NULL>
Introduced in 5.10 -
LIST<FLOAT NOT NULL>
Introduced in 5.10 -
LIST<DATE NOT NULL>
Introduced in 5.10 -
LIST<LOCAL TIME NOT NULL>
Introduced in 5.10 -
LIST<ZONED TIME NOT NULL>
Introduced in 5.10 -
LIST<LOCAL DATETIME NOT NULL>
Introduced in 5.10 -
LIST<ZONED DATETIME NOT NULL>
Introduced in 5.10 -
LIST<DURATION NOT NULL>
Introduced in 5.10 -
LIST<POINT NOT NULL>
Introduced in 5.10 -
Any closed dynamic union of the above types, e.g.
INTEGER | FLOAT | STRING
. Introduced in 5.11
Allowed syntax variations of these types are listed in Types and their synonyms.
For examples on how to create property type constraints, see Create, show, and drop constraints → Create property type constraints.
Create key constraints
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName IS [NODE] KEY
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE (n.propertyName_1, ..., n.propertyName_n) IS [NODE] KEY
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE r.propertyName IS [REL[ATIONSHIP]] KEY
CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR ()-"["r:RELATIONSHIP_TYPE"]"-()
REQUIRE (r.propertyName_1, ..., r.propertyName_n) IS [REL[ATIONSHIP]] KEY
For examples on how to create key constraints, see Create, show, and drop constraints → Create key constraints. Key constraints are index-backed.
SHOW CONSTRAINTS
To list all constraints with the default output columns, use SHOW CONSTRAINTS
.
If all columns are required, use SHOW CONSTRAINTS YIELD *
.
If only specific columns are required, use SHOW CONSTRAINTS YIELD field[, …]
.
The SHOW CONSTRAINTS
clause can also be filtered using the WHERE
clause.
Listing constraints requires the SHOW CONSTRAINTS privilege.
|
SHOW [
ALL
|NODE UNIQUE[NESS]
|REL[ATIONSHIP] UNIQUE[NESS]
|UNIQUE[NESS]
|NODE [PROPERTY] EXIST[ENCE]
|REL[ATIONSHIP] [PROPERTY] EXIST[ENCE]
|[PROPERTY] EXIST[ENCE]
|NODE PROPERTY TYPE
|REL[ATIONSHIP] PROPERTY TYPE
|PROPERTY TYPE
|NODE KEY
|REL[ATIONSHIP] KEY
|KEY
] CONSTRAINT[S]
[WHERE expression]
SHOW [
ALL
|NODE UNIQUE[NESS]
|REL[ATIONSHIP] UNIQUE[NESS]
|UNIQUE[NESS]
|NODE [PROPERTY] EXIST[ENCE]
|REL[ATIONSHIP] [PROPERTY] EXIST[ENCE]
|[PROPERTY] EXIST[ENCE]
|NODE PROPERTY TYPE
|REL[ATIONSHIP] PROPERTY TYPE
|PROPERTY TYPE
|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. Introduced in 5.7 |
|
Returns the relationship property uniqueness constraints. Introduced in 5.7 |
|
Returns all property uniqueness constraints, for both nodes and relationships.
Allowing |
|
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 property type constraints. Introduced in 5.9 |
|
Returns the relationship property type constraints. Introduced in 5.9 |
|
Returns all property type constraints, for both nodes and relationships. Introduced in 5.9 |
|
Returns the node key constraints. |
|
Returns the relationship key constraints. Introduced in 5.7 |
|
Returns all node and relationship key constraints. Introduced in 5.7 |
For examples on how to list constraints, see Create, show, and drop constraints → SHOW CONSTRAINTS.
For full details of the result columns for the SHOW CONSTRAINTS
command, see Create, show, and drop constraints → Result columns for listing constraints.
DROP CONSTRAINT
Constraints are dropped using the DROP
CONSTRAINT command.
Dropping a constraint is done by specifying the name of the constraint.
Dropping a constraint requires the DROP CONSTRAINT privilege.
|
DROP CONSTRAINT constraint_name [IF EXISTS]
This 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.
As of Neo4j 5.17, an informational notification is instead returned detailing that the constraint does not exist.
For examples on how to drop constraints, see Create, show, and drop constraints → DROP CONSTRAINT.