Syntax

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. As of Neo4j 5.17, an informational notification is returned in case nothing happens showing the existing constraint which blocks the creation.

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 CONSTRAINT privilege.

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.

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.

Node property existence constraint

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 OPTIONS values for existence constraints, but an empty options map is allowed for consistency.

Relationship property existence constraint

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 OPTIONS values for existence constraints, but an empty options map is allowed for consistency.

Node property type constraint

This command creates a property type constraint on nodes with the specified label and property.

CREATE CONSTRAINT [constraint_name] [IF NOT EXISTS]
FOR (n:LabelName)
REQUIRE n.propertyName {[IS] :: | IS TYPED} <TYPE>
[OPTIONS "{" "}"]

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 here.

There are no supported OPTIONS values for property type constraints, but an empty options map is allowed for consistency.

Relationship property type constraint

This command creates a property type 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] :: | IS TYPED} <TYPE>
[OPTIONS "{" "}"]

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 here.

There are no supported OPTIONS values for property type constraints, but an empty options map is allowed for consistency.

Node key constraint

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.

Relationship key constraint

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. As of Neo4j 5.17, an informational notification is instead returned detailing that the constraint does not exist.

Dropping a constraint requires the DROP CONSTRAINT privilege.

Syntax for listing constraints

List constraints in the database, either all or filtered on constraint type.

Listing constraints requires the SHOW CONSTRAINTS privilege.

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 PROPERTY TYPE
     |REL[ATIONSHIP] PROPERTY TYPE
     |PROPERTY TYPE
     |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 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:

Table 1. Type filters
Filter Description

ALL

Returns all constraints, no filtering on constraint type. This is the default if none is given.

NODE UNIQUE[NESS]

Returns the node property uniqueness constraints. Introduced in 5.7

REL[ATIONSHIP] UNIQUE[NESS]

Returns the relationship property uniqueness constraints. Introduced in 5.7

UNIQUE[NESS]

Returns all property uniqueness constraints, for both nodes and relationships. Allowing UNIQUENESS was introduced in 5.3

NODE [PROPERTY] EXIST[ENCE]

Returns the node property existence constraints.

REL[ATIONSHIP] [PROPERTY] EXIST[ENCE]

Returns the relationship property existence constraints.

[PROPERTY] EXIST[ENCE]

Returns all property existence constraints, for both nodes and relationships.

NODE PROPERTY TYPE

Returns the node property type constraints. Introduced in 5.9

REL[ATIONSHIP] PROPERTY TYPE

Returns the relationship property type constraints. Introduced in 5.9

PROPERTY TYPE

Returns all property type constraints, for both nodes and relationships. Introduced in 5.9

NODE KEY

Returns the node key constraints.

REL[ATIONSHIP] KEY

Returns the relationship key constraints. Introduced in 5.7

KEY

Returns all node and relationship key constraints. Introduced in 5.7

The returned columns from the show command is:

Table 2. Listing constraints output
Column Description Type

id

The id of the constraint. Default Output

INTEGER

name

Name of the constraint (explicitly set by the user or automatically assigned). Default Output

STRING

type

The ConstraintType of this constraint (UNIQUENESS (node uniqueness), RELATIONSHIP_UNIQUENESS, NODE_PROPERTY_EXISTENCE, RELATIONSHIP_PROPERTY_EXISTENCE, NODE_PROPERTY_TYPE, RELATIONSHIP_PROPERTY_TYPE, NODE_KEY, or RELATIONSHIP_KEY). Default Output

STRING

entityType

Type of entities this constraint represents (nodes or relationship). Default Output

STRING

labelsOrTypes

The labels or relationship types of this constraint. Default Output

LIST<STRING>

properties

The properties of this constraint. Default Output

LIST<STRING>

ownedIndex

The name of the index associated with the constraint or null, in case no index is associated with it. Default Output

STRING

propertyType

The property type the property is restricted to for property type constraints, or null for the other constraints. Default Output Introduced in 5.9

STRING

options

The options passed to CREATE command, for the index associated to the constraint, or null if no index is associated with the constraint.

MAP

createStatement

Statement used to create the constraint.

STRING