Bird
Raised Fist0
MySQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the NOT NULL constraint do in a MySQL table column?
easy
A. It automatically fills the column with a default value.
B. It deletes rows where the column is empty.
C. It allows the column to accept any type of data.
D. It ensures the column cannot have empty or missing values.

Solution

  1. Step 1: Understand NOT NULL constraint

    The NOT NULL constraint means a column must always have a value; it cannot be left empty or null.
  2. Step 2: Compare with other options

    Only It ensures the column cannot have empty or missing values. correctly describes this behavior. It automatically fills the column with a default value. describes DEFAULT, C is unrelated, and D is incorrect.
  3. Final Answer:

    It ensures the column cannot have empty or missing values. -> Option D
  4. Quick Check:

    NOT NULL means no empty values allowed [OK]
Hint: NOT NULL means value required, no blanks allowed [OK]
Common Mistakes:
  • Confusing NOT NULL with DEFAULT
  • Thinking NOT NULL deletes rows
  • Assuming NOT NULL allows empty strings
2. Which of the following is the correct syntax to add a NOT NULL and DEFAULT constraint to a column named age of type INT in MySQL?
easy
A. ALTER TABLE users MODIFY age INT DEFAULT 18 NULL;
B. ALTER TABLE users MODIFY age INT NOT NULL DEFAULT 18;
C. ALTER TABLE users CHANGE age INT DEFAULT 18 NOT NULL;
D. ALTER TABLE users ADD age INT DEFAULT NOT NULL 18;

Solution

  1. Step 1: Review correct ALTER TABLE syntax

    To modify a column with NOT NULL and DEFAULT, the syntax is: MODIFY column_name data_type NOT NULL DEFAULT value.
  2. Step 2: Check each option

    ALTER TABLE users MODIFY age INT NOT NULL DEFAULT 18; matches this syntax exactly. Options B and C have wrong order or keywords. ALTER TABLE users MODIFY age INT DEFAULT 18 NULL; allows NULL, which is incorrect.
  3. Final Answer:

    ALTER TABLE users MODIFY age INT NOT NULL DEFAULT 18; -> Option B
  4. Quick Check:

    Correct syntax uses MODIFY with NOT NULL then DEFAULT [OK]
Hint: Use MODIFY column TYPE NOT NULL DEFAULT value [OK]
Common Mistakes:
  • Placing DEFAULT before NOT NULL
  • Using ADD instead of MODIFY for existing columns
  • Allowing NULL when NOT NULL is required
3. Given the table employees with column status VARCHAR(10) NOT NULL DEFAULT 'active', what will be the value of status after this insert?
INSERT INTO employees (name) VALUES ('John');
medium
A. Empty string ''
B. NULL
C. 'active'
D. Error due to missing status

Solution

  1. Step 1: Understand DEFAULT and NOT NULL behavior

    The column status cannot be NULL and has a default value 'active'. If no value is given, MySQL uses the default.
  2. Step 2: Analyze the INSERT statement

    The insert only provides name, so status uses its default 'active'. No error occurs.
  3. Final Answer:

    'active' -> Option C
  4. Quick Check:

    Missing value uses DEFAULT when NOT NULL [OK]
Hint: Missing NOT NULL column uses DEFAULT value automatically [OK]
Common Mistakes:
  • Expecting NULL instead of default
  • Thinking insert will fail without explicit value
  • Assuming empty string is default
4. You have this table creation statement:
CREATE TABLE products (id INT NOT NULL, price DECIMAL(5,2) DEFAULT 0.00);

What is the problem with this table definition?
medium
A. The id column is NOT NULL but has no DEFAULT, causing insert errors.
B. The id column allows NULL values.
C. The price column cannot have a default value.
D. There is no PRIMARY KEY defined, so the table is invalid.

Solution

  1. Step 1: Check NOT NULL columns without DEFAULT

    The id column is NOT NULL but has no default value. If an insert omits id, it will cause an error.
  2. Step 2: Analyze other options

    The price column cannot have a default value. is wrong because price can have a default. The id column allows NULL values. is wrong because id is NOT NULL. There is no PRIMARY KEY defined, so the table is invalid. is not mandatory for table creation.
  3. Final Answer:

    The id column is NOT NULL but has no DEFAULT, causing insert errors. -> Option A
  4. Quick Check:

    NOT NULL without DEFAULT requires explicit value on insert [OK]
Hint: NOT NULL columns need value or DEFAULT to avoid insert errors [OK]
Common Mistakes:
  • Assuming NOT NULL columns get default NULL
  • Thinking PRIMARY KEY is required for table creation
  • Believing DEFAULT is mandatory for NOT NULL
5. You want to create a table orders where the status column must never be empty and should default to 'pending'. Which of these statements correctly creates this table?
hard
A. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending');
B. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) DEFAULT 'pending');
C. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL);
D. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) DEFAULT NOT NULL 'pending');

Solution

  1. Step 1: Identify constraints needed

    Status must never be empty (NOT NULL) and default to 'pending' if no value is given.
  2. Step 2: Check each CREATE TABLE option

    CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending'); correctly uses NOT NULL and DEFAULT together. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) DEFAULT 'pending'); misses NOT NULL, allowing NULLs. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL); misses DEFAULT, so no automatic value. CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) DEFAULT NOT NULL 'pending'); has wrong syntax.
  3. Final Answer:

    CREATE TABLE orders (id INT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending'); -> Option A
  4. Quick Check:

    NOT NULL with DEFAULT sets required and default value [OK]
Hint: Use NOT NULL before DEFAULT for required columns [OK]
Common Mistakes:
  • Omitting NOT NULL allows NULL values
  • Placing DEFAULT before NOT NULL
  • Using invalid syntax with DEFAULT NOT NULL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard