0
0
MySQLquery~20 mins

NOT NULL and DEFAULT constraints in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NOT NULL and DEFAULT Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of NOT NULL constraint on INSERT
Given the table 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);
AThe query fails with an error because the NOT NULL column 'name' is missing a value.
BThe query succeeds, inserting id=1, name=NULL, and age=18.
CThe query succeeds, inserting id=1, name='', and age=18.
DThe query fails with an error because the DEFAULT value for 'age' is missing.
Attempts:
2 left
💡 Hint
Think about what NOT NULL means for a column when no value is provided.
query_result
intermediate
2:00remaining
Default value usage on INSERT
Consider the table 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;
AThe SELECT returns no rows because the INSERT failed.
BRow with id=10, name='Pen', stock=NULL is inserted and returned.
CRow with id=10, name='Pen', stock=0 is inserted and returned.
DThe INSERT fails because 'stock' is NOT NULL and no value is provided.
Attempts:
2 left
💡 Hint
What happens when a column has a DEFAULT value and no value is given in INSERT?
📝 Syntax
advanced
2: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?
ACREATE TABLE orders (id INT PRIMARY KEY, status VARCHAR(20) DEFAULT NOT NULL 'pending');
BCREATE TABLE orders (id INT PRIMARY KEY, status VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT 'Order status');
CCREATE TABLE orders (id INT PRIMARY KEY, status VARCHAR(20) DEFAULT 'pending' NOT NULL);
DCREATE TABLE orders (id INT PRIMARY KEY, status VARCHAR(20) NOT NULL DEFAULT 'pending');
Attempts:
2 left
💡 Hint
Check the order of NOT NULL and DEFAULT keywords in column definition.
🧠 Conceptual
advanced
2: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?
AThe INSERT succeeds and inserts the default NULL value, which is treated as 0.
BThe INSERT succeeds and inserts NULL into <code>col</code>.
CThe INSERT succeeds and inserts 0 into <code>col</code>.
DThe INSERT fails because NOT NULL conflicts with DEFAULT NULL.
Attempts:
2 left
💡 Hint
Think about the contradiction between NOT NULL and DEFAULT NULL.
optimization
expert
2: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?
AKeep the column as is and rely on application logic to prevent NULLs.
BAlter the column to <code>status VARCHAR(10) NOT NULL DEFAULT 'active'</code> to enforce NOT NULL and default value.
CRemove the DEFAULT and allow NULLs to save space.
DChange the column to <code>status VARCHAR(10) NOT NULL</code> without DEFAULT and always specify status on INSERT.
Attempts:
2 left
💡 Hint
Consider constraints that enforce data integrity and reduce NULL storage.