After creating a table, you often need to modify it โ add a column, rename it,
or change something. That is what ALTER TABLE is for.
Adding a Column
ALTER TABLE employees ADD COLUMN email TEXT;
New columns are added at the end. Existing rows will have NULL for this column unless you specify a DEFAULT value.
Renaming a Table
ALTER TABLE employees RENAME TO staff;
SQLite Limitations
I should be honest with you โ SQLite has limited ALTER TABLE support compared to other databases. In SQLite, you can only:
- Rename a table
- Add a column
You cannot drop a column or change a column's type directly in SQLite. In MySQL or PostgreSQL, you have much more flexibility:
-- MySQL: Drop a column
-- ALTER TABLE employees DROP COLUMN email;
-- PostgreSQL: Change column type
-- ALTER TABLE employees ALTER COLUMN salary TYPE INTEGER;
If you ever switch databases, check the documentation. ALTER TABLE syntax varies a lot between systems.