In data pipelines, quality issues like 'a NULL slipped into a non-nullable column' or 'a negative amount got through' propagate to downstream reports and ML models, causing massive rework. Delta Constraints let you define data quality rules at the table level and automatically reject any write that violates them.
This article covers the SQL syntax for NOT NULL and CHECK constraints, when to use them versus DLT Expectations, how to inspect and drop constraints, behavior during streaming writes, and the points you need to know for the exam.
The simplest constraint. It prevents NULL values from being written to the specified column.
-- Specify NOT NULL at table creation
CREATE TABLE prod.silver.orders (
order_id BIGINT NOT NULL,
customer_id BIGINT NOT NULL,
amount DECIMAL(10,2) NOT NULL,
status STRING,
created_at TIMESTAMP NOT NULL
);
-- Add a NOT NULL constraint to an existing table
ALTER TABLE prod.silver.orders
ALTER COLUMN status SET NOT NULL;
-- Drop a NOT NULL constraint
ALTER TABLE prod.silver.orders
ALTER COLUMN status DROP NOT NULL;When adding NOT NULL to an existing table, ALTER TABLE will fail if NULL values already exist. Update or delete the NULL rows first, then add the constraint.
CHECK constraints define a condition as a SQL expression and reject any row that violates it. You can define multiple CHECK constraints on the same table — only rows that satisfy every constraint are written.
-- Add a named CHECK constraint
ALTER TABLE prod.silver.orders
ADD CONSTRAINT chk_amount_positive CHECK (amount > 0);
ALTER TABLE prod.silver.orders
ADD CONSTRAINT chk_status_valid CHECK (status IN ('pending', 'shipped', 'delivered', 'cancelled'));
ALTER TABLE prod.silver.orders
ADD CONSTRAINT chk_created_after_2020 CHECK (created_at >= '2020-01-01');
-- CHECK constraint with a compound condition
ALTER TABLE prod.silver.orders
ADD CONSTRAINT chk_cancelled_has_reason CHECK (
status != 'cancelled' OR cancel_reason IS NOT NULL
);
-- Drop a CHECK constraint
ALTER TABLE prod.silver.orders
DROP CONSTRAINT chk_amount_positive;When a constraint is violated, the entire transaction is rolled back. If even one row in a batch write violates a constraint, the entire batch is rejected.
-- List all constraints on a table
DESCRIBE DETAIL prod.silver.orders;
-- Constraint definitions live under delta.constraints.xxx in the properties column
-- Check NOT NULL from table info
DESCRIBE TABLE prod.silver.orders;
-- Columns where the nullable column shows false have a NOT NULL constraint
-- Inspect as table properties
SHOW TBLPROPERTIES prod.silver.orders;
-- delta.constraints.chk_amount_positive = 'amount > 0'
-- delta.constraints.chk_status_valid = 'status IN ...'| Dimension | Delta Constraints | DLT Expectations |
|---|---|---|
| When it runs | At write time (INSERT/UPDATE/MERGE/COPY INTO) | During DLT pipeline processing |
| Scope | Every write path into the table | Only processing inside the DLT pipeline |
| On violation | Rolls back the entire transaction (fail only) | Choose warn (log only) / drop (exclude row) / fail (stop pipeline) |
| Metadata recording | Stores constraint definitions in table properties | Records quality metrics in the pipeline event log |
| Quality dashboard | None (manual inspection required) | Metrics surfaced automatically in the DLT UI |
| Bad-record quarantine | Not supported (reject only) | Can quarantine to a separate table |
-- DLT Expectations example (for reference)
CREATE OR REFRESH STREAMING LIVE TABLE silver_orders (
CONSTRAINT valid_amount EXPECT (amount > 0) ON VIOLATION DROP ROW,
CONSTRAINT valid_status EXPECT (status IS NOT NULL) ON VIOLATION FAIL UPDATE
)
AS SELECT * FROM STREAM(LIVE.bronze_orders);Delta Constraints are also enforced during Structured Streaming micro-batch writes. If a micro-batch contains a row that violates a constraint, the entire batch is rolled back and the stream stops with an error.
In streaming pipelines, Delta Constraints' 'fail only' behavior can be too strict. A practical best practice is to drop bad records with DLT Expectations' DROP ROW and keep Delta Constraints as the final safety net.
-- Check existing data for violations (run before adding the constraint)
SELECT COUNT(*) AS violation_count
FROM prod.silver.orders
WHERE amount <= 0;
SELECT COUNT(*) AS null_count
FROM prod.silver.orders
WHERE customer_id IS NULL;Data Engineer Associate / Professional
問題 1
The Silver-layer orders table has a CHECK constraint amount > 0. A Structured Streaming micro-batch writes 100 rows, one of which has amount = -50. Which describes the behavior correctly?
正解: A
Unlike DLT Expectations, a Delta Constraints CHECK violation is 'fail only'. Even a single violating row rolls back the entire transaction (the micro-batch) and stops the stream. Per-row skipping, auto-correction, and warning-only behavior are not available with Delta Constraints — if you need per-row control, use DLT Expectations' DROP ROW.
Should I use Delta Constraints or DLT Expectations?
Using both together is recommended. Delta Constraints (NOT NULL/CHECK) are the 'last line of defense' — enforced at the table level for every write path so violations cannot slip through. DLT Expectations are pipeline-level quality gates that measure quality metrics and quarantine bad records inside the pipeline. Combining DLT Expectations' drop or fail actions with Delta Constraints gives you layered data quality management.
What SQL is allowed inside a CHECK constraint?
CHECK constraints only allow expressions that can be evaluated on a single row. Subqueries, aggregate functions (COUNT, SUM, etc.), window functions, and UDFs are not allowed. You can use column references, literals, comparison operators (=, <, >, !=), logical operators (AND, OR, NOT), IS NULL / IS NOT NULL, BETWEEN, IN, LIKE, CASE WHEN, and so on.
Can I customize the error message when a constraint is violated?
The constraint name (the alias in the CONSTRAINT clause) is included in the error message, but you cannot supply a custom message. The recommended practice is to give constraints descriptive names (e.g., chk_amount_positive, chk_status_valid) so that the root cause is easy to identify when a violation occurs.
Practice with certification-focused question sets
無料で問題を解いてみるNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
Databricks Certifications: All 7 Exams, Difficulty & Study Plan (2026)
Complete guide to all 7 Databricks certifications — Data Eng...
Databricks Exam Difficulty Ranking: All 7 Certs Compared (2026)
Every Databricks certification ranked by difficulty, with st...
Databricks Study Guide: Fastest Pass Route & Time Estimates (2026)
How to pass Databricks certifications efficiently. Official ...
Databricks Data Engineer Associate: Complete Guide (2026)
Domain-by-domain breakdown of the Databricks Certified Data ...
Databricks Data Engineer Professional: Complete Guide (2026)
Tactics for the Databricks Certified Data Engineer Professio...