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?
Think about what happens when you add a column with a default value to existing rows.
When you add a new column with a default value, MySQL sets that default value for all existing rows automatically.
You want to rename the column birthdate to dob in a MySQL table users. Which ALTER TABLE command is correct?
MySQL uses CHANGE to rename a column and requires the new column type.
In MySQL, CHANGE is used to rename a column and you must specify the new column name and its data type.
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?
Think about minimizing table locks and downtime for large tables.
For large tables, creating a new table with the new structure and copying data in chunks reduces locking time compared to ALTER TABLE which locks the table.
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?
Check if the column name is correct and exists in the table.
The error means MySQL cannot find the column to drop. The most common cause is a typo or the column does not exist.
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?
Think about how MySQL converts data when changing column types.
MySQL converts existing integer values to decimal format preserving the numeric value, adding decimal places as specified.