VALIDATE CONSTRAINT checks all existing rows against a previously-defined CHECK or NOT NULL constraint and returns any violations.
VALIDATE CONSTRAINT scans every row in a table and evaluates it against a specific CHECK or NOT NULL constraint that was added with NOT ENFORCED. The statement reports whether the data passes and, if it fails, lists the first 1,000 violations.
Run it after bulk-loading historical data or changing business rules. First create the constraint as NOT ENFORCED, backfill or clean data, then validate.If the command returns zero violations, you can safely switch the constraint to ENFORCED.
Use ALTER TABLE table_name VALIDATE CONSTRAINT constraint_name. Optionally, add OPTIONS(max_rows=INT64, return_summary=BOOL). See the full syntax below.
No. VALIDATE CONSTRAINT is online; queries can read and write while validation runs, but heavy tables may experience higher slot usage.
You must run one statement per constraint.Automate with scripting if many constraints exist.
Suppose Orders.total_amount must be non-negative. You add the constraint as NOT ENFORCED, load old orders, and then validate:
-- Step 1: Add but don’t enforce
ALTER TABLE Orders
ADD CONSTRAINT chk_total_amount
CHECK (total_amount >= 0) NOT ENFORCED;.
-- Step 2: Validate existing rows
ALTER TABLE Orders
VALIDATE CONSTRAINT chk_total_amount;
-- Step 3: Enforce going forward
ALTER TABLE Orders
ALTER CONSTRAINT chk_total_amount ENFORCED;
Add constraints as NOT ENFORCED in staging first, fix bad rows using UPDATE … WHERE NOT condition, then validate in production. Monitor INFORMATION_SCHEMA.CONSTRAINT_VALIDATION_RESULTS to audit failures.
Mistake 1: Adding an ENFORCED constraint before cleaning legacy data. Fix: Use NOT ENFORCED, clean, validate, then enforce.
Mistake 2: Assuming VALIDATE automatically turns the constraint on.Fix: After a successful validation, issue ALTER CONSTRAINT … ENFORCED.
Explore INFORMATION_SCHEMA views, automate checks with scheduled queries, and integrate with CI pipelines for continuous data quality.
.
No. It only scans and reports; it does not modify or delete rows.
The statement fails and lists up to 1,000 bad rows in the job output. Fix the data and rerun.
Yes, cancel the BigQuery job in the console or with bq cancel. Partial scans have no side effects.