# Index and constraint names

[Wiki](../README.md) / [Indexes and constraints](README.md)

**On this page**

- [Naming at creation](#naming-at-creation)
- [Derived names](#derived-names)
- [Dropping by name](#dropping-by-name)
- [Listing](#listing)
- [Upgrading from 2.3 and earlier](#upgrading-from-23-and-earlier)

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

```cypher
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:

| Kind | Derived name |
|---|---|
| Hash index | `index_Person_email` |
| Range index | `range_Person_age` |
| Composite index | `composite_Person_first_last` |
| Composite range index | `composite_range_Person_born_city` |
| Element index | `element_Person_tags` |
| Relationship index | `rel_index_KNOWS_since` |
| Full-text index | `fulltext_Note_body` |
| Uniqueness constraint | `constraint_Person_email` |
| Existence constraint | `existence_Person_id` |
| Node key | `node_key_Person_first_last` |
| Element uniqueness | `element_unique_Person_aliases` |
| Shared uniqueness | `group_unique_Company_Person_handle` |
| Property type | `type_Person_age`, `rel_type_KNOWS_since` |

Derived names are worked out when needed and never stored.

## Dropping by name

```cypher
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:

| Column | `SHOW INDEXES` | `SHOW CONSTRAINTS` |
|---|---|---|
| `label` | Label or relationship type (`A|B` for shared uniqueness) | same |
| `property` | Properties, joined with `, ` | same |
| `type` | `BTREE`, `RANGE`, `ELEMENT`, `RELATIONSHIP`, `FULLTEXT`, `VECTOR`, `POINT`, `SPATIAL` | `UNIQUE`, `NODE_PROPERTY_EXISTENCE`, `NODE_KEY`, `ELEMENT_UNIQUE`, `GROUP_UNIQUE`, `NODE_PROPERTY_TYPE(T)`, `RELATIONSHIP_PROPERTY_TYPE(T)` |
| `name` | The name | same |
| `entityType` | `NODE` or `RELATIONSHIP` | same |
| `labelsOrTypes` | List of labels or types | same |
| `properties` | List of properties | same |
| `owningConstraint` / `ownedIndex` | The 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.

## Related articles

[Property, range and composite indexes](property.md) · [Constraints](constraints.md) · [Procedure directory](../reference/procedures.md)
