By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
ALTER TABLE is a SQL command used to modify the structure of an existing database table. This includes adding, modifying, and dropping columns. Mastering this command is crucial for database management and optimization. Incorrect usage can lead to data loss or corruption, affecting application performance and user experience. For instance, dropping a column without understanding its dependencies can break queries and stored procedures.
ALTER TABLE ... ADD COLUMN
ALTER TABLE employees ADD COLUMN hire_date DATE;
⚠️ Pitfall: Adding a column with NOT NULL without a default value can cause errors if the table already has rows.
NOT NULL
Modifying a Column
ALTER TABLE ... MODIFY COLUMN
ALTER TABLE employees MODIFY COLUMN salary DECIMAL(10, 2);
⚠️ Pitfall: Modifying a column type can lead to data truncation or loss if the new type is incompatible.
Dropping a Column
ALTER TABLE ... DROP COLUMN
ALTER TABLE employees DROP COLUMN middle_name;
⚠️ Pitfall: Dropping a column used in queries or constraints can break dependencies.
Setting a Default Value
ALTER TABLE ... ALTER COLUMN ... SET DEFAULT
ALTER TABLE employees ALTER COLUMN hire_date SET DEFAULT '2023-01-01';
⚠️ Pitfall: Setting a default value that is incompatible with the column type can cause errors.
Removing a Default Value
ALTER TABLE ... ALTER COLUMN ... DROP DEFAULT
ALTER TABLE employees ALTER COLUMN hire_date DROP DEFAULT;
Experts view ALTER TABLE as a tool for schema evolution, not just a command for quick fixes. They consider the impact on existing data, dependencies, and future scalability. Instead of making ad-hoc changes, they plan modifications to align with long-term database design goals.
Exam trap: Questions may ask about adding NOT NULL columns to non-empty tables.
The mistake: Modifying a column type without checking existing data.
Exam trap: Scenarios involving data type changes and potential data loss.
The mistake: Dropping a column used in queries or constraints.
Exam trap: Questions about the impact of dropping columns.
The mistake: Setting an incompatible default value.
Scenario 1: You need to add a new column birth_date to the employees table.Question: Write the SQL command to add this column.Solution: Use ALTER TABLE employees ADD COLUMN birth_date DATE;.Answer: ALTER TABLE employees ADD COLUMN birth_date DATE; Why it works: Extends the table schema with a new field for birth dates.
birth_date
employees
ALTER TABLE employees ADD COLUMN birth_date DATE;
Scenario 2: The salary column in the employees table needs to be changed from INT to DECIMAL(10, 2).Question: Write the SQL command to modify the column.Solution: Use ALTER TABLE employees MODIFY COLUMN salary DECIMAL(10, 2);.Answer: ALTER TABLE employees MODIFY COLUMN salary DECIMAL(10, 2); Why it works: Changes the data type to accommodate decimal values.
salary
INT
DECIMAL(10, 2)
Scenario 3: The middle_name column in the employees table is no longer needed.Question: Write the SQL command to drop this column.Solution: Use ALTER TABLE employees DROP COLUMN middle_name;.Answer: ALTER TABLE employees DROP COLUMN middle_name; Why it works: Removes the unnecessary column from the table.
middle_name
ALTER TABLE
ALTER TABLE table_name action;
ADD COLUMN
MODIFY COLUMN
DROP COLUMN
FOREIGN KEY
UNIQUE
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.