Beginner LevelSQL & MySQL
How do ALTER TABLE statements modify database schemas dynamically?
❓ Interview Prompt: Explain Data Definition Language (DDL), `ALTER TABLE` operations (ADD COLUMN, DROP COLUMN, MODIFY COLUMN, ADD INDEX).
'ALTER TABLE' is a Data Definition Language (DDL) command used to modify the structure of an existing database table:
Common Operations:
•
'ADD COLUMN': Append a new column with specified data type and constraints.
•
'DROP COLUMN': Remove an existing column and its stored data.
•
'MODIFY / CHANGE COLUMN': Change column data types or nullability rules.
•
'ADD INDEX / CONSTRAINT': Attach new indexes or foreign key rules.
Code Implementation Example
sql
-- Adding a new column and composite index to an existing table
ALTER TABLE users
ADD COLUMN phone_verified TINYINT(1) DEFAULT 0 NOT NULL AFTER email,
ADD INDEX idx_phone_verified (phone_verified);💡Modifying table schema and adding an index via ALTER TABLE.
🎯 Interview Key Takeaway
Use ALTER TABLE to update database table structures dynamically in response to evolving domain requirements.
#SQL#ALTER TABLE#DDL#Schema#Beginner
Back to all SQL & MySQL Questions→