0
0
MySQLquery~20 mins

ALTER TABLE operations in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ALTER TABLE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Adding a new column with default value

Consider a table employees with columns id (int) and name (varchar). You run this command:

ALTER TABLE employees ADD COLUMN status VARCHAR(10) DEFAULT 'active';

What will be the value of status for existing rows after this command?

ANULL for all existing rows
B'active' for all existing rows
CEmpty string ('') for all existing rows
DThe command will fail because existing rows have no value
Attempts:
2 left
💡 Hint

Think about what happens when you add a column with a default value to existing rows.

📝 Syntax
intermediate
2:00remaining
Correct syntax to rename a column

You want to rename the column birthdate to dob in a MySQL table users. Which ALTER TABLE command is correct?

AALTER TABLE users CHANGE birthdate dob DATE;
BALTER TABLE users RENAME COLUMN birthdate TO dob;
CALTER TABLE users MODIFY COLUMN birthdate dob DATE;
DALTER TABLE users ALTER COLUMN birthdate RENAME TO dob;
Attempts:
2 left
💡 Hint

MySQL uses CHANGE to rename a column and requires the new column type.

optimization
advanced
2:00remaining
Improving performance when adding a column

You want to add a new nullable column last_login of type DATETIME to a large table accounts without locking the table for a long time. Which approach is best?

AALTER TABLE accounts ADD COLUMN last_login DATETIME NULL;
BDrop indexes before adding the column, then recreate indexes.
CAdd the column with NOT NULL and a default value to avoid NULLs.
DCreate a new table with the extra column, copy data, then rename tables.
Attempts:
2 left
💡 Hint

Think about minimizing table locks and downtime for large tables.

🔧 Debug
advanced
2:00remaining
Error when dropping a column

You run this command:

ALTER TABLE orders DROP COLUMN order_date;

But you get an error: ERROR 1091 (42000): Can't DROP 'order_date'; check that column/key exists. What is the most likely cause?

AThe column <code>order_date</code> does not exist in the table.
BYou cannot drop columns in MySQL using ALTER TABLE.
CThe column <code>order_date</code> is part of a foreign key constraint.
DYou need to use DROP COLUMN IF EXISTS syntax.
Attempts:
2 left
💡 Hint

Check if the column name is correct and exists in the table.

🧠 Conceptual
expert
2:00remaining
Impact of changing column datatype with ALTER TABLE

You have a column price defined as INT in a table products. You run:

ALTER TABLE products MODIFY price DECIMAL(10,2);

What happens to the existing data in price after this command?

AThe data remains as integers and the datatype change is ignored.
BThe command fails because you cannot change from INT to DECIMAL.
CAll integer values are converted to decimal with two decimal places (e.g., 10 becomes 10.00).
DAll values are truncated to zero because of incompatible types.
Attempts:
2 left
💡 Hint

Think about how MySQL converts data when changing column types.