---
title: "Declare and optimize with UNIQUE constraints on Databricks"
description: "Databricks now supports UNIQUE constraints in Public Preview for Unity Catalog Delta tables, allowing tables to declare multiple uniqueness guarantees beyond a single primary key. Unlike primary keys, UNIQUE constraints can be nullable and multiple can exist per table, enabling schemas to record business-level uniqueness such as username or composite (workspace_id, external_user_id). These constraints are informational by default and not enforced on writes, but after validation they can be marked with RELY to enable query optimizations. With RELY, the optimizer can eliminate unnecessary joins and DISTINCT aggregations, achieving up to 7.9x and 12.0x speedups respectively on TPC-DS SF10000 benchmarks by removing over 99.99% of shuffle. Foreign keys can also reference UNIQUE constraints, and the feature supports composite and nullable unique columns with specific nullability considerations for optimization, and the post provides validation queries to check data before enabling RELY."
---

# Declare and optimize with UNIQUE constraints on Databricks

[Databricks](https://yomu.fyi/company/databricks) · Yan Yan, Yuchuan Huang, Xinyi Yu, Gengliang Wang · Nov 3, 2026

**Type:** Tutorial

## Summary

Databricks now supports UNIQUE constraints in Public Preview for Unity Catalog Delta tables, allowing tables to declare multiple uniqueness guarantees beyond a single primary key. Unlike primary keys, UNIQUE constraints can be nullable and multiple can exist per table, enabling schemas to record business-level uniqueness such as username or composite (workspace\_id, external\_user\_id). These constraints are informational by default and not enforced on writes, but after validation they can be marked with RELY to enable query optimizations. With RELY, the optimizer can eliminate unnecessary joins and DISTINCT aggregations, achieving up to 7.9x and 12.0x speedups respectively on TPC-DS SF10000 benchmarks by removing over 99.99% of shuffle. Foreign keys can also reference UNIQUE constraints, and the feature supports composite and nullable unique columns with specific nullability considerations for optimization, and the post provides validation queries to check data before enabling RELY.

## Context

Databricks previously supported only PRIMARY KEY and FOREIGN KEY constraints, which limited the ability to record multiple uniqueness guarantees in table schemas.

## Approach / What changed

Declare UNIQUE constraints on Unity Catalog Delta tables using SQL DDL, validate data with provided queries, mark constraints with RELY after validation, and rely on the optimizer to eliminate joins and DISTINCT aggregations.

## Takeaways

- UNIQUE constraints can be nullable and multiple per table, unlike PRIMARY KEY which requires non-nullable columns and allows only one per table.
- With RELY, the optimizer can eliminate inner joins when a foreign key references a UNIQUE constraint and the child column is non-nullable, achieving up to 7.9x speedup.
- DISTINCT can be removed when selecting from a non-nullable UNIQUE column, but not from a nullable UNIQUE column unless a filter removes nulls, achieving up to 12.0x speedup.

## FAQ

### Can UNIQUE constraints be enforced on writes?

No, UNIQUE constraints are informational by default and not enforced on writes. Enforcement must be handled by the ingestion pipeline or another data-quality process.

### How do I enable query optimization with UNIQUE constraints?

Validate the data, then drop and recreate the constraint with RELY. Ensure foreign keys also have RELY. The optimizer can then use them for join elimination and DISTINCT removal.

### What happens if data violates a RELY constraint?

The optimizer may produce incorrect results because it assumes the constraint holds. Only use RELY for guarantees that are validated and continuously maintained.

**Tags:** [Databricks](https://yomu.fyi/topic/databricks), [Delta Lake](https://yomu.fyi/topic/delta-lake), [Optimization](https://yomu.fyi/topic/optimization), [SQL](https://yomu.fyi/topic/sql), [Unity Catalog](https://yomu.fyi/topic/unity-catalog)

- Source: [Databricks](https://www.databricks.com/blog/declare-and-optimize-unique-constraints-databricks)
- Source URL: https://www.databricks.com/blog/declare-and-optimize-unique-constraints-databricks
- Ingested by Yomu: 2026-11-03T21:00:14.095Z

[Read original post](https://www.databricks.com/blog/declare-and-optimize-unique-constraints-databricks)
