MariaDB ALTER TABLE: Add, drop, move, or change a column

In MariaDB, ALTER TABLE … ADD COLUMN adds a column and DROP COLUMN removes the column and its data. MODIFY COLUMN changes a definition or position while keeping the name. CHANGE COLUMN can change the name and definition together. Repeat the attributes you want to keep even when you only move a column. MariaDB Docs · ALTER TABLE

Prepare the practice table and record its structure

Use a MariaDB server, a SQL client, and a practice database named learning. Create and populate the sample table, then run the blocks in order on the same connection. If the name is already used, use a separate practice database. In production, back up the schema and data, verify recovery, and review application code, views, indexes, and constraints that depend on the table. MariaDB Docs · CREATE TABLE

USE learning;
CREATE TABLE products_columns_demo (
    id INT NOT NULL PRIMARY KEY,
    name VARCHAR(30) NOT NULL COMMENT 'Product name',
    quantity INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Stock',
    note VARCHAR(20) NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO products_columns_demo VALUES
    (1, 'Notebook', 5, 'Existing note'), (2, 'Mug', 3, NULL);
SHOW CREATE TABLE products_columns_demo;

The initial order is id, name, quantity, note. Check that quantity is UNSIGNED and NOT NULL, with a default of 0 and a comment. SHOW CREATE TABLE gives the definition, while SHOW FULL COLUMNS makes individual attributes easy to compare. Back up the rows separately. MariaDB Docs · SHOW COLUMNS

Add columns with FIRST and AFTER

The first block appends created_at because it specifies no position. The existing rows get NULL. Then FIRST puts category at the front and AFTER name puts unit_price after the name. The 0.00 price is an exercise default. Plan a suitable value for existing records before adding a production NOT NULL column.

ALTER TABLE products_columns_demo
    ADD COLUMN created_at DATETIME NULL;
SHOW FULL COLUMNS FROM products_columns_demo;
ALTER TABLE products_columns_demo
    ADD COLUMN category VARCHAR(20) NULL FIRST,
    ADD COLUMN unit_price DECIMAL(10,2) NOT NULL DEFAULT 0.00 AFTER name;
SHOW FULL COLUMNS FROM products_columns_demo;

Drop a column and its data

Dropping note removes the column and the stored note. The two product rows remain. Adding an empty column of the same name later will not bring back the old note. Review queries, views, indexes, and constraints that refer to it before dropping a real column.

ALTER TABLE products_columns_demo DROP COLUMN note;
SHOW FULL COLUMNS FROM products_columns_demo;

Move a column while keeping its attributes

The first MODIFY moves quantity to the front, and the second moves it back after unit_price. Both repeat the type, unsigned attribute, nullability, default, and comment. Compare the column order and confirm the quantities are still 5 and 3. If only the query output needs a new order, list the columns in that order in SELECT.

ALTER TABLE products_columns_demo
    MODIFY COLUMN quantity INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Stock' FIRST;
SHOW FULL COLUMNS FROM products_columns_demo;
ALTER TABLE products_columns_demo
    MODIFY COLUMN quantity INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Stock' AFTER unit_price;
SHOW FULL COLUMNS FROM products_columns_demo;

Change the data type and column name

Use MODIFY to expand name from VARCHAR(30) to VARCHAR(100). The length is measured in characters. Before shrinking it, use a check such as SELECT id FROM products_columns_demo WHERE CHAR_LENGTH(name) > 30 to identify rows beyond the proposed limit. Before converting text to a number, check which values cannot be converted or fit the range, and review warnings and errors. MariaDB Docs · VARCHAR MariaDB Docs · SQL_MODE

ALTER TABLE products_columns_demo
    MODIFY COLUMN name VARCHAR(100) NOT NULL COMMENT 'Product name';
SHOW FULL COLUMNS FROM products_columns_demo;

The next CHANGE renames quantity to stock and widens it to BIGINT UNSIGNED. Write the old name, the new name, then the complete definition. With no placement clause, the column stays in place. Update application queries that still use the old name.

ALTER TABLE products_columns_demo
    CHANGE COLUMN quantity stock BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Stock';
SHOW FULL COLUMNS FROM products_columns_demo;

Verify the result and plan recovery

SELECT id, name, stock, unit_price, category, created_at
FROM products_columns_demo ORDER BY id;
SHOW CREATE TABLE products_columns_demo;

In the final query, IDs 1 and 2 have stock values 5 and 3. Both prices are 0.00 and both category and created_at are NULL. Verify that the schema shows name as VARCHAR(100) and stock as BIGINT UNSIGNED NOT NULL with the default of 0 and its comment.

ALTER TABLE causes an implicit commit, so an ordinary ROLLBACK cannot undo it. To narrow a type again, check data added since the change as well. Recover deleted or truncated values from a backup. On production-size data, test how long the change takes and whether it blocks concurrent work. Rebuild and locking rules depend on the operation, server version, and storage engine. MariaDB Docs · Implicit commits