Use ALTER TABLE … MODIFY or CHANGE to safely alter a column’s data type without recreating the table.
Adjusting a column’s data type lets you store larger values, enforce stricter constraints, or improve storage efficiency. Typical cases include converting VARCHAR order totals to DECIMAL or switching INT flags to BOOLEAN for clarity.
Always back up the table, disable foreign-key checks if needed, and run the ALTER TABLE statement inside a transaction when using InnoDB. For mission-critical tables, test on a clone first.
MODIFY keeps the column name intact and only changes its definition. Ideal when the name stays the same—e.g., increasing VARCHAR length or switching to DECIMAL.
ALTER TABLE Orders MODIFY total_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00;
CHANGE requires you to repeat (or rename) the column name and lets you alter its data type and position simultaneously. Use it when renaming columns or moving them with FIRST/AFTER.
ALTER TABLE Customers CHANGE created_at registered_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER email;
MariaDB automatically rebuilds affected indexes, but you must recreate CHECK constraints referencing the altered column. Verify foreign-key columns share the same type and length.
ALTER TABLE locks the table for the operation’s duration unless online DDL is enabled. Schedule during low-traffic windows or use pt-online-schema-change for zero-downtime migrations.
Suppose customer_id
is VARCHAR(10) but should be INT. First, validate data, then run:
ALTER TABLE Orders MODIFY customer_id INT UNSIGNED NOT NULL;
Keep column and referenced key types identical, set sensible DEFAULT values, monitor disk space, and update ORM schemas or application code immediately after deployment.
Omitting NULL/NOT NULL: MariaDB resets the NULLability if you do not specify it. Always repeat the desired constraint.
Forgetting to copy DEFAULT: CHANGE without DEFAULT drops the old default. Explicitly restate default values.
Yes, use MariaDB’s online DDL (ROW_FORMAT=COMPRESSED/INNODB) or tools like pt-online-schema-change to minimize blocking.
Only if the referenced and referencing columns end up with identical definitions. Otherwise, drop and recreate the foreign key.
Performance is identical; both trigger a table rebuild unless the change is metadata-only. Choose based on whether you need to rename the column.