The ALTER statement in MySQL is used to modify the structure of an existing database object such as a table. It allows you to add, delete, or modify columns, indexes, and constraints. ALTER is a powerful command that gives you flexibility when managing database schema changes without losing any data.
The ALTER command changes table definitions after creation. It can add, remove, or update table components without recreating the table.
// Add a new column to an existing table
ALTER TABLE employees ADD COLUMN email VARCHAR(255);
// Remove an existing column from a table
ALTER TABLE employees DROP COLUMN email;
// Change the data type or size of a column
ALTER TABLE employees MODIFY COLUMN email VARCHAR(100);
// Rename an existing column (MySQL 8+)
ALTER TABLE employees RENAME COLUMN old_name TO new_name;
// Create an index to improve search performance
ALTER TABLE employees ADD INDEX idx_email (email);
// Remove an index from the table
ALTER TABLE employees DROP INDEX idx_email;
After running ALTER commands, the table structure changes immediately. Existing data remains intact unless explicitly removed.