LearnContact
Lesson 1218 min read

Altering Tables

Learn how to change the structure of an existing table using ALTER TABLE — adding, modifying, renaming, and dropping columns.

Introduction

Tables rarely stay exactly the way you first created them. As a project grows, you often need to add a new column, fix a column that was made too small, rename something to be clearer, or remove a column you no longer need.

MySQL gives you the ALTER TABLE statement for exactly this purpose. It lets you change the structure of an existing table without having to drop it and recreate it, which means the data already stored in the table is preserved.

What You Will Learn
  • What the ALTER TABLE statement is used for.
  • How to add a new column with ADD COLUMN.
  • How to change a column's data type with MODIFY COLUMN.
  • How to rename a column with RENAME COLUMN.
  • How to remove a column with DROP COLUMN.

The ALTER TABLE Statement

ALTER TABLE is the general-purpose statement for changing a table's structure. It is always followed by the name of the table you want to change, and then one of several clauses describing exactly what change to make.

ALTER TABLE table_name
clause_describing_the_change;
Note

ALTER TABLE only changes the structure (the columns) of a table. It does not add, change, or remove the rows of data already stored — those are handled by INSERT, UPDATE, and DELETE, covered in later lessons.

Adding a Column

Use ADD COLUMN when a table is missing a piece of information you now need to track. You specify the new column's name and its data type, just like when creating a table.

ALTER TABLE students
ADD COLUMN email VARCHAR(100);
Result
Query OK, 0 rows affected
Records: 0  Duplicates: 0  Warnings: 0

For every row that already existed in the table, the new email column will be filled in with NULL, since MySQL has no way to know what each student's email should be.

idnameagegradeemail
1Alex16ANULL
2Sam17BNULL
3Priya16ANULL

Modifying a Column

Use MODIFY COLUMN when the column already exists but its data type or size needs to change. A very common example is a VARCHAR column that was created too small and now needs to hold longer values.

ALTER TABLE students
MODIFY COLUMN email VARCHAR(150);
Result
Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0

The column keeps its name (email) but can now store up to 150 characters instead of 100. MODIFY COLUMN can also change the data type entirely, for example from INT to BIGINT, as long as the existing data can be safely converted.

Be Careful When Shrinking a Column

If you MODIFY a column to a smaller size or a stricter type than the data currently stored in it, MySQL may truncate values or reject the change entirely. Always check your existing data before shrinking a column.

Renaming a Column

Use RENAME COLUMN when the column's name itself needs to change but its data type stays the same. This is useful for fixing a typo or making a name clearer.

ALTER TABLE students
RENAME COLUMN email TO contact_email;
Result
Query OK, 0 rows affected
Records: 0  Duplicates: 0  Warnings: 0

Every value already stored in that column is preserved — only the column's name changes, from email to contact_email.

Dropping a Column

Use DROP COLUMN when a column is no longer needed. This permanently removes both the column and every value stored in it for every row — so use it carefully.

ALTER TABLE students
DROP COLUMN contact_email;
Result
Query OK, 0 rows affected
Records: 0  Duplicates: 0  Warnings: 0
This Cannot Be Undone

Dropping a column deletes all the data stored in it for every row in the table. There is no built-in "undo" — make sure you truly do not need that data (or have a backup) before running DROP COLUMN.

Worked Example: Evolving the students Table

Let's walk through a realistic sequence of changes to the students table, starting from its original structure and ending with an email column added correctly.

-- Original table
CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    grade VARCHAR(2)
);

-- Step 1: Add an email column
ALTER TABLE students
ADD COLUMN email VARCHAR(100);

-- Step 2: We realize some emails are longer than expected, so widen it
ALTER TABLE students
MODIFY COLUMN email VARCHAR(150);

-- Step 3: Rename it to be more descriptive
ALTER TABLE students
RENAME COLUMN email TO student_email;

-- Check the final structure
DESCRIBE students;
Result
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| id            | int          | NO   | PRI | NULL    |       |
| name          | varchar(50)  | YES  |     | NULL    |       |
| age           | int          | YES  |     | NULL    |       |
| grade         | varchar(2)   | YES  |     | NULL    |       |
| student_email | varchar(150) | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+

DESCRIBE students shows the full, current structure of the table, which is a great way to double-check the effect of every ALTER TABLE statement you run.

Common Mistakes

Avoid These Mistakes
  • Forgetting that DROP COLUMN permanently deletes data, not just the column definition.
  • Trying to MODIFY a column to a type that existing data cannot be converted to.
  • Confusing MODIFY COLUMN (changes type/size, keeps name) with RENAME COLUMN (changes name, keeps type).
  • Running ALTER TABLE on a huge production table without expecting it to take noticeable time or lock the table.

Best Practices

  • Always check DESCRIBE table_name after an ALTER TABLE to confirm the change worked as expected.
  • Back up important data before running any ALTER TABLE that changes or drops a column.
  • Make one structural change at a time so it is easy to trace what changed and why.
  • On a live production database, test ALTER TABLE statements on a copy of the table first.

Frequently Asked Questions

Can I add more than one column in a single ALTER TABLE statement?

Yes. You can separate multiple ADD COLUMN clauses with commas in a single ALTER TABLE statement, and MySQL will apply them together.

Does ALTER TABLE delete my existing rows?

No, not on its own. Adding, modifying, or renaming a column preserves existing rows. Only DROP COLUMN removes data, and only the data in that specific column.

What happens to existing rows when I add a new column?

Every existing row gets NULL in the new column, unless you specify a DEFAULT value for it.

Is there a way to undo an ALTER TABLE?

Not directly. You would need to run another ALTER TABLE to reverse the change, and any data lost from a DROP COLUMN cannot be recovered unless you have a backup.

Key Takeaways

  • ALTER TABLE changes the structure of an existing table without deleting its data.
  • ADD COLUMN adds a new column, filled with NULL for existing rows by default.
  • MODIFY COLUMN changes a column's data type or size while keeping its name.
  • RENAME COLUMN changes a column's name while keeping its data type.
  • DROP COLUMN permanently removes a column and every value stored in it.

Summary

ALTER TABLE is the tool you reach for whenever a table's structure needs to evolve. Its ADD COLUMN, MODIFY COLUMN, RENAME COLUMN, and DROP COLUMN clauses cover the vast majority of real-world structural changes.

In this lesson, you learned how to add, modify, rename, and remove columns from an existing table, and walked through a realistic example evolving the students table. Next, you will learn how to remove an entire table using DROP TABLE.

Lesson 12 Completed
  • You can add a new column to an existing table.
  • You can change a column's data type or size.
  • You can rename a column without losing its data.
  • You can remove a column you no longer need.
Next Lesson →

Dropping Tables