Challenge - 5 Problems
NOT NULL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Effect of NOT NULL constraint on INSERT
Given the table
Users(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT), what happens when you run this query?INSERT INTO Users (id, age) VALUES (1, 25);SQL
CREATE TABLE Users(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT); INSERT INTO Users (id, age) VALUES (1, 25);
Attempts:
2 left
💡 Hint
Think about what NOT NULL means for a column when no value is provided.
✗ Incorrect
The NOT NULL constraint means the column must have a value. Omitting the 'name' column in the INSERT causes the database to try to insert NULL, which violates the constraint and causes an error.
🧠 Conceptual
intermediate1:30remaining
Purpose of NOT NULL constraint
Why do database designers use the NOT NULL constraint on columns?
Attempts:
2 left
💡 Hint
Think about data integrity and missing information.
✗ Incorrect
NOT NULL ensures that every row has a meaningful value in that column, preventing missing or unknown data.
📝 Syntax
advanced2:00remaining
Adding NOT NULL constraint to existing column
Which SQL statement correctly adds a NOT NULL constraint to the existing column
email in the Customers table?Attempts:
2 left
💡 Hint
Different SQL dialects use different syntax; consider standard SQL or MySQL style.
✗ Incorrect
Option C uses the correct syntax to modify the column definition to add NOT NULL. Option C is MySQL-specific but changes the column name redundantly. Options A and B are invalid syntax.
❓ query_result
advanced2:00remaining
Query result with NOT NULL and DEFAULT
Consider this table:
What is the result of this query?
CREATE TABLE Products(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, price DECIMAL(5,2) NOT NULL DEFAULT 9.99);What is the result of this query?
INSERT INTO Products (id, name) VALUES (1, 'Pen');
SELECT * FROM Products WHERE id = 1;Attempts:
2 left
💡 Hint
Think about how DEFAULT values work with NOT NULL columns.
✗ Incorrect
Since price has a DEFAULT value and is NOT NULL, omitting it in INSERT uses the default 9.99, so the row inserts successfully with that price.
🔧 Debug
expert2:30remaining
Diagnosing NOT NULL constraint violation in multi-step insert
You have this table:
You run these queries:
What error will you get and why?
CREATE TABLE Orders(order_id INT PRIMARY KEY, customer_id INT NOT NULL, order_date DATE NOT NULL);You run these queries:
INSERT INTO Orders (order_id, customer_id) VALUES (101, 5);What error will you get and why?
Attempts:
2 left
💡 Hint
Check which NOT NULL columns are missing values in the INSERT.
✗ Incorrect
The 'order_date' column is NOT NULL and no value or default was provided, so the insert fails with a NOT NULL constraint violation.