Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 38

ALTER TABLE in MySQL

In this lesson: Change table structure in MySQL with ALTER TABLE — add, modify, rename and drop columns and keys — with output.

Changing a table structure

ALTER TABLE lets you change an existing table's structure without deleting it or losing data. You can add new columns, change a column's type, rename columns, or remove columns. It is a DDL command used whenever your data needs evolve.

💡 ALTER TABLE edits the table's shape (its columns) while keeping the data inside.

Add a column

ALTER TABLE students ADD email VARCHAR(100);
Query OK, 0 rows affected (0.04 sec)

This adds an email column to the students table. Existing rows get NULL in the new column until you fill them. You can add several columns in one command too.

Modify a column

ALTER TABLE students MODIFY marks FLOAT;

MODIFY changes a column's data type — here marks becomes a FLOAT to allow decimals. Ensure the existing values fit the new type, or the change may fail or lose data.

Rename a column

ALTER TABLE students RENAME COLUMN name TO full_name;

RENAME COLUMN changes a column's name while keeping its data and type. The old name name becomes full_name. (On older MySQL, use CHANGE with the type instead.)

Drop a column

ALTER TABLE students DROP COLUMN email;
Query OK, 0 rows affected (0.05 sec)

DROP COLUMN removes a column and all its data permanently, but keeps the rest of the table. This is different from DROP TABLE, which deletes the whole table.

Several changes at once

ALTER TABLE students
    ADD phone VARCHAR(15),
    MODIFY marks FLOAT,
    DROP COLUMN email;

You can combine multiple operations in one ALTER TABLE, separated by commas. This is efficient and keeps related changes together.

Common mistakes

  • Modifying to a smaller type. Shrinking a type can truncate or reject existing data.
  • Confusing DROP COLUMN and DROP TABLE. One removes a column; the other the whole table.
  • Forgetting existing rows get NULL. New columns start as NULL unless given a default.
  • Old syntax for rename. On older MySQL use CHANGE old new type instead of RENAME COLUMN.

Summary

  • ALTER TABLE changes a table's structure without losing data.
  • ADD adds a column; MODIFY changes its type.
  • RENAME COLUMN renames; DROP COLUMN removes a column.
  • You can combine several changes in one statement.
  • DROP COLUMN keeps the table; DROP TABLE deletes everything.

Next, compare removal commands in DROP vs TRUNCATE vs DELETE, or add rules with NOT NULL, UNIQUE, CHECK, DEFAULT.

Frequently Asked Questions

What does ALTER TABLE do in MySQL?
ALTER TABLE changes the structure of an existing table without deleting it. You can add columns, change a column's type, rename columns, or drop columns, and the existing data stays in place.
How do I add a column to an existing table?
Use ALTER TABLE tablename ADD columnname datatype;. For example ALTER TABLE students ADD email VARCHAR(100); adds an email column to all rows, filled with NULL by default.
How do I change a column's data type?
Use ALTER TABLE tablename MODIFY columnname newtype;. Make sure the existing data fits the new type, or MySQL may reject the change or truncate values.
How do I delete a column?
Use ALTER TABLE tablename DROP COLUMN columnname;. This removes the column and all its data permanently, so be careful. The rest of the table stays intact.
What is the difference between DROP COLUMN and DROP TABLE?
DROP COLUMN (inside ALTER TABLE) removes only one column but keeps the table. DROP TABLE removes the entire table with all its columns and data. One trims; the other deletes everything.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicConstraints in MySQL Your progress: 38 / 151 (25%) · Lesson 39 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free