
Answer-first summary for fast verification
Answer: The users table already contains rows that violate the new constraint; all existing rows must satisfy the constraint before adding it to the table.
The `ADD CONSTRAINT` command checks that all existing rows in the table comply with the new constraint before it is added. If any rows violate the constraint, the command will fail, indicating that some rows do not meet the new CHECK constraint requirements. For more details, refer to the [Databricks documentation on constraints](https://docs.databricks.com/tables/constraints.html#set-a-check-constraint-in-databricks).
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
The data engineering team has a large Delta table named ‘users’. A recent query on the table returned some entries with negative values in the ‘age’ column. To enforce data quality, a junior data engineer attempted to add a CHECK constraint with the command: ALTER TABLE users ADD CONSTRAINT valid_age CHECK (age > 0);. However, the command failed. Which statement best explains the cause of this failure?
A
The users table already exists; CHECK constraints can only be added during table creation using CREATE TABLE command.
B
The users table already contains rows that violate the new constraint; all existing rows must satisfy the constraint before adding it to the table.
C
The users table is not partitioned on the age column. CHECK constraints can only be added on partitioning columns.
D
The users table already contains rows; CHECK constraints can only be added on empty tables.
No comments yet.