Bird
0
0

Which is the correct way to add this column?

hard📝 Application Q15 of 15
SQL - Table Constraints

You have a table orders with columns order_id INT PRIMARY KEY, customer_id INT NOT NULL, and order_date DATE NOT NULL. You want to add a new column status that must always have a value and defaults to 'pending'. Which is the correct way to add this column?

AALTER TABLE orders ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'pending';
BALTER TABLE orders ADD COLUMN status VARCHAR(20) DEFAULT 'pending';
CALTER TABLE orders ADD COLUMN status VARCHAR(20) NOT NULL;
DALTER TABLE orders ADD COLUMN status VARCHAR(20);
Step-by-Step Solution
Solution:
  1. Step 1: Understand NOT NULL with default value

    Adding a NOT NULL column requires a default value to avoid errors on existing rows.
  2. Step 2: Analyze each ALTER statement

    ALTER TABLE orders ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'pending'; adds status with NOT NULL and default 'pending', so existing rows get this value. Others miss NOT NULL or default.
  3. Final Answer:

    ALTER with NOT NULL and DEFAULT 'pending' is correct. -> Option A
  4. Quick Check:

    NOT NULL columns need default when added to existing table [OK]
Quick Trick: Add NOT NULL column with DEFAULT to avoid errors [OK]
Common Mistakes:
MISTAKES
  • Adding NOT NULL column without default
  • Assuming default alone enforces NOT NULL
  • Omitting NOT NULL when required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes