Challenge - 5 Problems
Constraint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of INSERT with NOT NULL constraint violation
Given the table
users with a NOT NULL constraint on the email column, what happens when you run this query?INSERT INTO users (id, email) VALUES (1, NULL);PostgreSQL
CREATE TABLE users (id SERIAL PRIMARY KEY, email VARCHAR(255) NOT NULL);
Attempts:
2 left
💡 Hint
NOT NULL means the column cannot have NULL values.
✗ Incorrect
The NOT NULL constraint prevents inserting NULL values into the column. The database raises an error if you try.
❓ query_result
intermediate2:00remaining
Effect of UNIQUE constraint on duplicate inserts
Consider a table
products with a UNIQUE constraint on the sku column. What happens if you run these two queries?INSERT INTO products (id, sku) VALUES (1, 'ABC123');
INSERT INTO products (id, sku) VALUES (2, 'ABC123');PostgreSQL
CREATE TABLE products (id SERIAL PRIMARY KEY, sku VARCHAR(50) UNIQUE);
Attempts:
2 left
💡 Hint
UNIQUE means no two rows can have the same value in that column.
✗ Incorrect
The UNIQUE constraint prevents duplicate values in the sku column. The second insert violates this and causes an error.
📝 Syntax
advanced2:00remaining
Correct CHECK constraint syntax for age column
Which option correctly adds a CHECK constraint to ensure the
age column is between 18 and 65 inclusive in PostgreSQL?PostgreSQL
ALTER TABLE employees ADD CONSTRAINT age_check CHECK (age >= 18 AND age <= 65);
Attempts:
2 left
💡 Hint
CHECK constraints require a condition inside parentheses.
✗ Incorrect
Option A uses correct syntax with CONSTRAINT name and a condition in parentheses using AND for the range.
🔧 Debug
advanced2:00remaining
Identify the error in this table definition with constraints
What is wrong with this table creation statement?
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INT NOT NULL UNIQUE,
order_date DATE CHECK order_date > CURRENT_DATE
);Attempts:
2 left
💡 Hint
CHECK constraints require parentheses around the condition.
✗ Incorrect
The CHECK constraint must have the condition inside parentheses. Missing parentheses cause a syntax error.
🧠 Conceptual
expert2:00remaining
Behavior of multiple constraints on a single column
If a column
username has both NOT NULL and UNIQUE constraints, which of the following statements is true?Attempts:
2 left
💡 Hint
NOT NULL means no NULLs allowed; UNIQUE means no duplicates allowed.
✗ Incorrect
NOT NULL disallows NULLs, so the column must have unique, non-NULL values.