Delta tables support standard SQL constraint management clauses that ensure that the quality and integrity of data added to a table is automatically verified. When a constraint is violated, Delta Lake throws an
InvariantViolationException to signal that the new data can’t be added.
Two types of constraints are supported:
NOT NULL: indicates that values in specific columns cannot be null.
CHECK: indicates that a specified Boolean expression must be true for each input row.
NOT NULL constraints in the schema when you create a table and drop
NOT NULL constraints using the
ALTER TABLE CHANGE COLUMN command.
CREATE TABLE events( id LONG NOT NULL, date STRING NOT NULL, location STRING, description STRING ) USING DELTA; ALTER TABLE events CHANGE COLUMN date DROP NOT NULL;
NOT NULL constraint is specified on a column nested within a struct, the parent struct is also constrained to not be null. Columns nested within array or map types do not accept
NOT NULL constraints.
CHECK constraints using the
ALTER TABLE ADD CONSTRAINT and
ALTER TABLE DROP CONSTRAINT commands.
ALTER TABLE ADD CONSTRAINT verifies that all existing rows satisfy the constraint before adding it to the table.
CREATE TABLE events( id LONG NOT NULL, date STRING, location STRING, description STRING ) USING DELTA; ALTER TABLE events ADD CONSTRAINT dateWithinRange CHECK (date > '1900-01-01'); ALTER TABLE events DROP CONSTRAINT dateWithinRange;
CHECK constraints appear as table properties in the output of the
DESCRIBE DETAIL and
SHOW TBLPROPERTIES commands.
ALTER TABLE events ADD CONSTRAINT validIds CHECK (id > 1000 and id < 999999); DESCRIBE DETAIL events;