Challenge - 5 Problems
NOT NULL and DEFAULT Master
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
What happens when you run this query?
users defined as:CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT DEFAULT 18);
What happens when you run this query?
INSERT INTO users (id) VALUES (1);
MySQL
CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT DEFAULT 18); INSERT INTO users (id) VALUES (1);
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. Since 'name' is NOT NULL and no value is given, the INSERT fails. DEFAULT applies only when a value is not provided for columns that allow NULL or have a default.
❓ query_result
intermediate2:00remaining
Default value usage on INSERT
Consider the table
What will be the result of this query?
products defined as:CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, stock INT DEFAULT 0);
What will be the result of this query?
INSERT INTO products (id, name) VALUES (10, 'Pen'); SELECT * FROM products WHERE id = 10;
MySQL
CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, stock INT DEFAULT 0); INSERT INTO products (id, name) VALUES (10, 'Pen'); SELECT * FROM products WHERE id = 10;
Attempts:
2 left
💡 Hint
What happens when a column has a DEFAULT value and no value is given in INSERT?
✗ Incorrect
Since 'stock' has a DEFAULT of 0 and is not NOT NULL, the INSERT uses the default value 0 for 'stock'. The row is inserted successfully.
📝 Syntax
advanced2:00remaining
Identify the syntax error in table definition
Which of the following CREATE TABLE statements has a syntax error related to NOT NULL and DEFAULT constraints?
Attempts:
2 left
💡 Hint
Check the order of NOT NULL and DEFAULT keywords in column definition.
✗ Incorrect
The correct order is NOT NULL before DEFAULT. Option A places DEFAULT before NOT NULL, which is invalid syntax.
🧠 Conceptual
advanced2:00remaining
Behavior of NOT NULL with DEFAULT NULL
If a column is defined as
col INT NOT NULL DEFAULT NULL, what will happen when you insert a row without specifying a value for col?Attempts:
2 left
💡 Hint
Think about the contradiction between NOT NULL and DEFAULT NULL.
✗ Incorrect
NOT NULL means the column cannot be NULL. DEFAULT NULL tries to set NULL as default. This conflict causes an error on insert without a value.
❓ optimization
expert2:00remaining
Optimizing table design with NOT NULL and DEFAULT
You have a large table
events with a column status defined as VARCHAR(10) DEFAULT 'active' but without NOT NULL. You want to ensure every row has a valid status and optimize storage. Which is the best approach?Attempts:
2 left
💡 Hint
Consider constraints that enforce data integrity and reduce NULL storage.
✗ Incorrect
Adding NOT NULL with a DEFAULT ensures no NULLs and automatic default insertion, improving data integrity and storage efficiency.