Index and constraint names

Wiki / Indexes and constraints

On this page

Every index and every constraint has a name, unique across both. From version 2.4.0, the name given at creation is kept, and DROP INDEX name or DROP CONSTRAINT name drops any kind by name.

Naming at creation

CREATE INDEX person_last FOR (p:Person) ON (p.last_name);
CREATE RANGE INDEX person_age IF NOT EXISTS FOR (p:Person) ON (p.age);
CREATE INDEX knows_since FOR ()-[r:KNOWS]-() ON (r.since);
CREATE FULLTEXT INDEX note_body FOR (n:Note) ON EACH [n.body];
CREATE CONSTRAINT person_email FOR (p:Person) REQUIRE p.email IS UNIQUE;
CREATE CONSTRAINT person_key IF NOT EXISTS FOR (p:Person) REQUIRE (p.first, p.last) IS NODE KEY;
  • A name is 1 to 255 printable characters; quote it with backticks if it is not a plain identifier.
  • A name already held by another index or constraint is an error. With IF NOT EXISTS the statement does nothing instead.
  • If an equivalent index or constraint already has a different name, that is an error too; with IF NOT EXISTS it does nothing.
  • If the equivalent object has no name of its own (it was created unnamed, or before 2.4), it takes the name given. Re-running a schema script with names therefore names what it created before.
  • Without a name, creation stays idempotent: repeating it changes nothing.

Vector, point and spatial indexes have always been declared by name; their names share the same namespace.

Derived names

An index or constraint created without a name gets one from its kind, labels and properties, as db.indexes() and db.constraints() have always shown:

KindDerived name
Hash indexindex_Person_email
Range indexrange_Person_age
Composite indexcomposite_Person_first_last
Composite range indexcomposite_range_Person_born_city
Element indexelement_Person_tags
Relationship indexrel_index_KNOWS_since
Full-text indexfulltext_Note_body
Uniqueness constraintconstraint_Person_email
Existence constraintexistence_Person_id
Node keynode_key_Person_first_last
Element uniquenesselement_unique_Person_aliases
Shared uniquenessgroup_unique_Company_Person_handle
Property typetype_Person_age, rel_type_KNOWS_since

Derived names are worked out when needed and never stored.

Dropping by name

DROP INDEX person_last;
DROP INDEX knows_since IF EXISTS;
DROP CONSTRAINT person_email;
DROP INDEX composite_Person_first_last;   // a derived name works too
  • DROP INDEX drops an index of any kind, vector, point and spatial included; DROP CONSTRAINT drops a constraint of any kind.
  • A missing name is an error unless IF EXISTS is given.
  • Naming a constraint in DROP INDEX, or an index in DROP CONSTRAINT, is an error.
  • The index a uniqueness constraint is enforced through cannot be dropped while the constraint exists; drop the constraint first.
  • DROP VECTOR INDEX, DROP POINT INDEX and DROP SPATIAL INDEX also accept IF EXISTS. Dropping by definition (DROP INDEX FOR (p:Person) ON (p.email)) works as before.

Listing

SHOW INDEXES lists every index of every kind; SHOW CONSTRAINTS lists every constraint. The first three columns are the ones every release has had:

ColumnSHOW INDEXESSHOW CONSTRAINTS
labelLabel or relationship type (`AB` for shared uniqueness)
propertyProperties, joined with , same
typeBTREE, RANGE, ELEMENT, RELATIONSHIP, FULLTEXT, VECTOR, POINT, SPATIALUNIQUE, NODE_PROPERTY_EXISTENCE, NODE_KEY, ELEMENT_UNIQUE, GROUP_UNIQUE, NODE_PROPERTY_TYPE(T), RELATIONSHIP_PROPERTY_TYPE(T)
nameThe namesame
entityTypeNODE or RELATIONSHIPsame
labelsOrTypesList of labels or typessame
propertiesList of propertiessame
owningConstraint / ownedIndexThe constraint an index enforces, and the reverse

SHOW FULLTEXT INDEXES, SHOW VECTOR INDEXES, SHOW POINT INDEXES and SHOW SPATIAL INDEXES still list their kind with its own details (language, dimensions and similarity).

Upgrading from 2.3 and earlier

There is nothing to run. A database written by an earlier release opens with a derived name for every index and constraint, so DROP INDEX index_Person_email and SHOW INDEXES work at once. Names given at creation before 2.4 were never stored; to get them back, run the original CREATE … name IF NOT EXISTS statements again, which name the matching objects.

The first explicit name a database stores marks its log segment (version 7) and any later snapshot or dump (version 9) so that 2.3 and earlier refuse them rather than misread them. A database with no explicit names stays readable by 2.3. Upgrade replicas before their source, because a 2.3 replica cannot apply a name.

Property, range and composite indexes · Constraints · Procedure directory

Planning a deployment? Review compatibility and licence setup for your instance.