0
0
PostgreSQLquery~20 mins

Column constraints (NOT NULL, UNIQUE, CHECK) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constraint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
AThe row is inserted with a NULL email value.
BThe query fails with a NOT NULL constraint violation error.
CThe query succeeds but sets email to an empty string.
DThe query ignores the NULL and inserts a default email.
Attempts:
2 left
💡 Hint
NOT NULL means the column cannot have NULL values.
query_result
intermediate
2: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);
AThe second insert fails with a UNIQUE constraint violation error.
BThe second insert overwrites the first row.
CThe second insert ignores the duplicate sku and inserts anyway.
DBoth rows are inserted successfully with the same sku.
Attempts:
2 left
💡 Hint
UNIQUE means no two rows can have the same value in that column.
📝 Syntax
advanced
2: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);
AALTER TABLE employees ADD CONSTRAINT age_check CHECK (age >= 18 AND age <= 65);
BALTER TABLE employees ADD CHECK age BETWEEN 18 AND 65;
CALTER TABLE employees ADD CONSTRAINT age_check CHECK age >= 18 AND age <= 65;
DALTER TABLE employees ADD CONSTRAINT age_check CHECK (age > 18 OR age < 65);
Attempts:
2 left
💡 Hint
CHECK constraints require a condition inside parentheses.
🔧 Debug
advanced
2: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
);
AThe SERIAL type cannot be used with PRIMARY KEY.
BThe UNIQUE constraint cannot be combined with NOT NULL.
CThe PRIMARY KEY must be declared after all columns.
DThe CHECK constraint syntax is missing parentheses around the condition.
Attempts:
2 left
💡 Hint
CHECK constraints require parentheses around the condition.
🧠 Conceptual
expert
2: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?
AThe column can have multiple NULL values but no duplicates otherwise.
BThe column can have NULL values but only one unique non-NULL value.
CThe column cannot have NULL values and all values must be unique.
DThe UNIQUE constraint overrides NOT NULL, allowing NULLs.
Attempts:
2 left
💡 Hint
NOT NULL means no NULLs allowed; UNIQUE means no duplicates allowed.