Lecture

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.

Add a new phone column
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.

Rename the phone column
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:

Change data type of phone
ALTER TABLE clients ALTER COLUMN contact_number TYPE TEXT;

Note: SQLite, the database engine used at CodeFriends, doesn’t support ALTER COLUMN TYPE directly. 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.

Remove the contact_number column
ALTER TABLE clients DROP COLUMN contact_number;

Be careful: Dropping a column permanently deletes its data.

Quiz
0 / 1

What does the ALTER TABLE command do?

Creates a new table

Modifies the structure of an existing table

Deletes all rows from a table

Sorts table rows

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Tables

Execution Result