← Back to MySQL Course | Chapter 2: Databases & Tables | Lesson 7 of 7

ALTER TABLE

ALTER TABLE is like adding a new column to a chart that already exists, without redrawing it or losing what has already been written.
Syntax
sql
ALTER TABLE table_name ADD column_name datatype;

ALTER TABLE table_name DROP COLUMN column_name;

Adding Columns

ALTER TABLE ... ADD lets you insert a new column into an existing table without dropping and recreating it, preserving all the data already stored in the other columns.

New columns are typically added as nullable or with a DEFAULT value, since existing rows won't have any value for the newly added column otherwise.

Example: Adding Columns

sql
ALTER TABLE users ADD email VARCHAR(100);

Dropping Columns

DROP COLUMN permanently deletes both the column definition and every value stored in it for every row — there's no partial or reversible version of this operation. Because of this permanence, it's wise to back up a table (or at least that column's data) before running a DROP COLUMN in production.

Example: Dropping Columns

sql
ALTER TABLE users DROP COLUMN email;

Modifying Column Data Types

MODIFY changes an existing column's data type or size, such as widening a VARCHAR(50) to VARCHAR(100) to accommodate longer values without touching the data already stored. Shrinking a column's size can silently truncate existing values that no longer fit, so this should always be checked against current data first.

Example: Modifying Column Data Types

sql
ALTER TABLE users MODIFY name VARCHAR(100);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Renaming Columns

RENAME COLUMN changes only the column's name in the schema; the underlying stored values are completely untouched, so existing data survives the rename unchanged. Any views, stored procedures, or application queries that reference the old column name by string will also need to be updated separately.

Example: Renaming Columns

sql
ALTER TABLE users RENAME COLUMN name TO full_name;

Renaming Tables

RENAME TO changes the table's own name — useful when refactoring a schema, though remember any application code or foreign keys referencing the old name will need updating too. Unlike renaming a column, renaming a table has no effect on the data or column structure inside it whatsoever.

Example: Renaming Tables

sql
ALTER TABLE users RENAME TO customers;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using DROP COLUMN on the wrong column, which deletes the column and all its data for every row.
  2. Using MODIFY and leaving out the full column definition (like NOT NULL), which silently removes it.
  3. Shrinking a column with MODIFY, such as VARCHAR(100) to VARCHAR(10), when existing longer values cause an error or truncation.
Chapter Summary
  • CREATE DATABASE, DROP DATABASE, and USE create, remove, and select a database.
  • CREATE TABLE, DROP TABLE, and TRUNCATE TABLE create, remove, and empty tables.
  • ALTER TABLE changes the structure of an existing table.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.