TExcelTAD
Home › MySQL › MySQL ALTER TABLE

MySQL ALTER TABLE

Requirements change, and tables have to change with them. ALTER TABLE adds, removes, renames and modifies columns, and adds or drops constraints and indexes, while keeping the data that's already there.

To keep the examples short, each one ends by listing the table's columns from information_schema:

sql
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shop' AND TABLE_NAME = 'suppliers'
ORDER BY ORDINAL_POSITION;

ADD COLUMN

mysql · try it yourself
ALTER TABLE suppliers ADD COLUMN email VARCHAR(80);
ALTER TABLE suppliers ADD COLUMN rating TINYINT NOT NULL DEFAULT 3 AFTER name;

SELECT supplier_id, name, rating, email FROM suppliers WHERE supplier_id <= 3;
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shop' AND TABLE_NAME = 'suppliers'
ORDER BY ORDINAL_POSITION;
+-------------+-------------------+--------+-------+
| supplier_id | name              | rating | email |
+-------------+-------------------+--------+-------+
|           1 | Highland Roasters |      3 | NULL  |
|           2 | Golden Crust Ltd  |      3 | NULL  |
|           3 | Alpine Dairy Co   |      3 | NULL  |
+-------------+-------------------+--------+-------+
+--------------+-----------+-------------+
| COLUMN_NAME  | DATA_TYPE | IS_NULLABLE |
+--------------+-----------+-------------+
| supplier_id  | int       | NO          |
| name         | varchar   | NO          |
| rating       | tinyint   | NO          |
| contact_name | varchar   | YES         |
| city         | varchar   | YES         |
| country      | varchar   | YES         |
| phone        | varchar   | YES         |
| email        | varchar   | YES         |
+--------------+-----------+-------------+

New columns are added at the end unless you say AFTER column (or FIRST). Existing rows get the column's default: NULL for email, 3 for rating.

DROP COLUMN

mysql · try it yourself
ALTER TABLE suppliers DROP COLUMN phone;
SELECT * FROM suppliers WHERE supplier_id = 1;
+-------------+-------------------+--------------+-----------+---------+
| supplier_id | name              | contact_name | city      | country |
+-------------+-------------------+--------------+-----------+---------+
|           1 | Highland Roasters | Fiona Grant  | Edinburgh | UK      |
+-------------+-------------------+--------------+-----------+---------+

The column and all its data are gone. Like DROP TABLE, this can't be undone.

Changing a column: MODIFY, CHANGE, RENAME COLUMN

Clause Use it to
MODIFY col new_definition change the type or constraints, keep the name
RENAME COLUMN old TO new change only the name (MySQL 8, MariaDB 10.5+)
CHANGE old new new_definition rename and redefine at once (the old way to rename)
ALTER COLUMN col SET DEFAULT v / DROP DEFAULT change only the default
mysql · try it yourself
ALTER TABLE suppliers MODIFY contact_name VARCHAR(100);
ALTER TABLE suppliers RENAME COLUMN contact_name TO contact;
ALTER TABLE suppliers CHANGE city town VARCHAR(50) NOT NULL;

SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH AS max_len, IS_NULLABLE
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'shop' AND TABLE_NAME = 'suppliers'
ORDER BY ORDINAL_POSITION;
+-------------+-----------+---------+-------------+
| COLUMN_NAME | DATA_TYPE | max_len | IS_NULLABLE |
+-------------+-----------+---------+-------------+
| supplier_id | int       |    NULL | NO          |
| name        | varchar   |      60 | NO          |
| contact     | varchar   |     100 | YES         |
| town        | varchar   |      50 | NO          |
| country     | varchar   |      40 | YES         |
| phone       | varchar   |      20 | YES         |
+-------------+-----------+---------+-------------+

MODIFY and CHANGE replace the whole column definition. If the column was NOT NULL or had a default and you leave that out, it is gone. Always restate everything the column should keep.

Warning: Making a column NOT NULL fails while rows still contain NULL in it, and shrinking a type (VARCHAR(100) to VARCHAR(10)) fails in strict mode if longer values exist. Fix the data first with UPDATE, then alter.

mysql · try it yourself
UPDATE customers SET city = 'Unknown' WHERE city IS NULL;
ALTER TABLE customers MODIFY city VARCHAR(40) NOT NULL;
SELECT first_name, city FROM customers WHERE customer_id = 9;
+------------+---------+
| first_name | city    |
+------------+---------+
| Ivan       | Unknown |
+------------+---------+

Several changes in one statement

Separate the actions with commas. On a big table this is much faster than several ALTERs, because MySQL may rebuild the table only once:

mysql · try it yourself
ALTER TABLE products
  ADD COLUMN weight_g INT AFTER name,
  ADD COLUMN created_on DATE NOT NULL DEFAULT '2025-01-01',
  DROP COLUMN discontinued;

SELECT product_id, name, weight_g, unit_price, created_on FROM products WHERE product_id <= 2;
+------------+--------------------+----------+------------+------------+
| product_id | name               | weight_g | unit_price | created_on |
+------------+--------------------+----------+------------+------------+
|          1 | Espresso Beans 1kg |     NULL |      24.50 | 2025-01-01 |
|          2 | Earl Grey Tea      |     NULL |       6.75 | 2025-01-01 |
+------------+--------------------+----------+------------+------------+

Renaming a table

mysql · try it yourself
CREATE TABLE tmp_notes (id INT);
ALTER TABLE tmp_notes RENAME TO notes;
RENAME TABLE notes TO memos;
SHOW TABLES LIKE 'memos';
+------------------------+
| Tables_in_shop (memos) |
+------------------------+
| memos                  |
+------------------------+

RENAME TABLE a TO b, c TO d can rename several tables at once, atomically.

Constraints and indexes

ALTER TABLE also adds and removes constraints and indexes; those lessons show the details:

sql
ALTER TABLE customers ADD CONSTRAINT uq_email UNIQUE (email);
ALTER TABLE products  ADD CHECK (unit_price >= 0);
ALTER TABLE orders    ADD INDEX idx_order_date (order_date);
ALTER TABLE orders    DROP FOREIGN KEY orders_ibfk_1;

Tip: On large production tables, schema changes can take a long time and block writes. MySQL 8 runs many of them "online" (ALGORITHM=INSTANT or INPLACE); for the rest, teams use tools such as gh-ost or pt-online-schema-change.

Test yourself

Where does ALTER TABLE t ADD COLUMN c INT; put the new column?

  1. First
  2. Last
  3. Alphabetically
  4. After the primary key

Which clause only renames a column in MySQL 8?

  1. MODIFY
  2. RENAME COLUMN old TO new
  3. ALTER COLUMN
  4. SET NAME

A column price DECIMAL(8,2) NOT NULL DEFAULT 0 is altered with MODIFY price DECIMAL(10,2);. What happens to NOT NULL and the default?

  1. They are kept
  2. They are removed, because MODIFY replaces the whole definition
  3. Only NOT NULL is kept
  4. The statement fails