# `@unique` annotation

The `@unique` annotation is used to specify unique attributes for entities and relations.

## [](#_syntax)Syntax

Syntax

```typeql
<type label> owns <attribute type label> @unique;
```

The `@unique` annotation does not accept any arguments.

## [](#_usage)Usage

The `@unique` annotation can be defined for an ownership to put the `unique` constraint on it. The `unique` constraint means that no two instances of the owner type (owners) can have instances of the attribute type (attributes) with the same _value._ That makes every owned attribute unique _by value_ among all instances of the owner type **and its subtypes**.

For example, the following statement defines that every `user` should have a unique `phone`:

```typeql
#!test[schema]
define
  entity user owns phone @unique;
  attribute phone value string;
```

With this constraint, no instance of `user` can share the same `phone` value.

### [](#subtyping)Subtyping

The `unique` constraint would also affect the following schema extension:

```typeql
#!test[schema]
define
  entity uk-user sub user, owns uk-phone;
  entity usa-user sub user, owns usa-phone;
  phone @abstract;
  attribute uk-phone sub phone;
  attribute usa-phone sub phone;
```

With the `unique` constraint on the `phone`, there is a protection ensuring that:

*   Any `user` can only have one unique `phone`.
    
*   A `uk-user` cannot have a `uk-phone` with the same value as a `usa-user` 's `usa-phone`.
    

If there is a need to restrict uniqueness within subtypes (e.g., allowing `uk-user` and `usa-user` to have phones with overlapping values, but still unique within their respective regions), the `unique` constraint should be applied directly to the subtype ownerships instead.

## [](#_limitations)Limitations

Cannot be used with the [`@key` annotation](../key/index.md) as they both produce the `unique` constraint.

See [Supported value types](#_value_types) for the list of supported value types.

## [](#_references)References

### [](#_value_types)Supported value types

The `@unique` annotation is applicable only to uniquely hashable value types.

Table 1. Supported value types   

Value type

Supported?

Comment

`boolean`

✓

`integer`

✓

`decimal`

✓

`string`

✓

`date`

✓

`datetime`

✓

`datetime-tz`

✓

`duration`

✓

`double`

✗

Constraining `double` attributes by exact value is error-prone and as such disallowed

[@subkey](../subkey/index.md) [@values](../values/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/typeql-reference/modules/ROOT/pages/annotations/unique.adoc) Edit this page on GitHub.