# Check Constraints & Composite Unique

> Add check constraints and composite unique constraints in Schemity. Express validation rules and multi-column uniqueness directly in your ERD, offline.

Source: https://schemity.com/doc/check-constraints-composite-unique/

Beyond keys and types, real schemas carry rules: a value must be positive, a status must be one of a set, or a pair of columns must be unique together. Schemity supports check constraints and composite unique constraints directly in the ERD, so your diagram captures the real validation rules and multi-column uniqueness, not just the shape.

## How do I add a check constraint?

**ERD check constraints support** lets you attach a boolean condition to an entity. The check-constraint dialog offers two ways to write it:

- **Visual mode** - pick a **Target field** and the allowed **Values**, and Schemity builds the expression for you (great for `status IN (...)` style rules).
- **Expression mode** - write the raw SQL condition yourself, for example `price >= 0` or `end_date >= start_date`, with a live preview.

When you export DDL, Schemity emits each one as a `CHECK` constraint.

## How do I add a composite unique constraint?

A single-column unique flag lives on the field (see [Entities & Fields](https://schemity.com/doc/tables-and-fields/)). When uniqueness spans more than one column, add a **composite unique constraint** through the Unique Constraint dialog, where you choose the fields, an optional name, and a color:

- `(tenant_id, email)` - email is unique per tenant, not globally.
- `(order_id, line_number)` - line numbers are unique within an order.

This is essential for **multi-tenant RBAC schema** designs, where uniqueness is almost always scoped by tenant.

## Do unique indexes show up on the diagram?

A multi-column **unique index** carries the same "unique together" meaning as a unique constraint. Schemity surfaces both with a **U** marker on the involved fields, so you can see uniqueness on the diagram whatever its source - while the index still stays in the index list.

## Can a unique constraint be there and still not constrain anything?

Yes, and it is the constraint most likely to survive a code review. A multi-column unique containing a **nullable** column does not stop repeats: NULLs compare as distinct, so the same combination inserts as many times as you like whenever that column is NULL. [Schema Lint](https://schemity.com/doc/schema-lint/) flags this one (`unique-includes-nullable`), along with uniqueness that covers the wrong set of columns.

## Why should I model constraints in the ERD?

Putting constraints in the diagram keeps the **ERD single source of truth**. A reviewer reading the diagram sees the same rules the database enforces - no drift between the picture and production.

## Next

Control connector routing with [Custom Waypoints](https://schemity.com/doc/custom-waypoints/).
