Altering Tables
The ALTER TABLE command lets you modify the structure of an existing table without deleting or recreating it.
You can add, rename, or remove columns — all while keeping your existing data intact.
Add a Column
To add a new column to an existing table, use the ADD COLUMN command.
ALTER TABLE clients
ADD COLUMN phone TEXT;
This adds a phone column to the clients table to store contact numbers.
Rename a Column
Renaming a column helps make names more descriptive or consistent with your data.
Syntax can vary between database systems.
For example, SQLite and PostgreSQL support the RENAME COLUMN command, while MySQL uses ALTER TABLE RENAME COLUMN.
Below is an example of how to rename the phone column to contact_number in the clients table.
ALTER TABLE clients
RENAME COLUMN phone TO contact_number;
This command renames the phone column to contact_number in the clients table.
Note: Some databases require different syntax or do not support this operation at all.
Change a Column's Data Type
Some SQL systems let you change a column’s data type using ALTER COLUMN:
ALTER TABLE clients
ALTER COLUMN contact_number TYPE TEXT;
Note: SQLite, the database engine used at CodeFriends, doesn’t support
ALTER COLUMN TYPEdirectly. You’d need to recreate the table or copy data into a new structure instead.
Drop a Column
To permanently remove a column, you can use the DROP COLUMN command.
ALTER TABLE clients
DROP COLUMN contact_number;
Be careful: Dropping a column permanently deletes its data.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.