CodeEraFree roadmap

SQL Practice / ALTER command

SQL ALTER TABLE Command: practice questions with answers

8 questions with solved queries and real output. Outputs checked on MariaDB 10.11 (MySQL-compatible) on 23 November 2026.

ALTER TABLE changes the structure of a table without losing its data: add a column, rename or drop one, change a data type, add a constraint, set a default, or rename the table itself.

Work through these questions in order. Each one builds on the table left by the one before (for example, Question 7 uses the column name you set in Question 2). After each query we show the table structure.

The syntax below is for MySQL and MariaDB. Other databases differ a little, for example SQL Server uses sp_rename and ALTER COLUMN.

ALTER TABLE t ADD column_name datatype;
ALTER TABLE t RENAME COLUMN old TO new;
ALTER TABLE t MODIFY column_name new_datatype;
ALTER TABLE t DROP COLUMN column_name;
ALTER TABLE t RENAME TO new_table_name;
Set up the practice tables

Copy this script and run it once.

CREATE TABLE products (
  prod_id   CHAR(10) NOT NULL,
  prod_name CHAR(255) NOT NULL,
  price     DECIMAL(8,2) NOT NULL,
  quantity  INT,
  PRIMARY KEY (prod_id)
);

INSERT INTO products (prod_id, prod_name, price, quantity) VALUES
('BR01',   '8 inch teddy bear',   5.99,  8),
  ('BR02',   '12 inch teddy bear',  8.99,  12),
  ('BR03',   '18 inch teddy bear',  11.99, 18),
  ('BNBG01', 'Fish bean bag toy',   3.49,  7),
  ('BNBG02', 'Bird bean bag toy',   3.49,  20),
  ('BNBG03', 'Rabbit bean bag toy', 3.49,  9),
  ('RGAN01', 'Raggedy Ann',         4.99,  15),
  ('RYL01',  'King doll',           9.49,  12),
  ('RYL02',  'Queen doll',          9.49,  26);

Table: products

prod_idprod_namepricequantity
BNBG01Fish bean bag toy3.497
BNBG02Bird bean bag toy3.4920
BNBG03Rabbit bean bag toy3.499
BR018 inch teddy bear5.998
BR0212 inch teddy bear8.9912
BR0318 inch teddy bear11.9918
RGAN01Raggedy Ann4.9915
RYL01King doll9.4912
RYL02Queen doll9.4926

Do these in order. Each question uses what the one before it created or changed.

How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.

Question 1Add a new column named "description" of type VARCHAR(255) to the products table.

Show the answer and the output
ALTER TABLE products ADD description VARCHAR(255);

Output

Query OK.

Then check with DESCRIBE products

FieldTypeNullKeyDefaultExtra
prod_idchar(10)NOPRINULL
prod_namechar(255)NONULL
pricedecimal(8,2)NONULL
quantityint(11)YESNULL
descriptionvarchar(255)YESNULL

Question 2In the products table, rename the column "prod_id" to "product_id".

Show the answer and the output
ALTER TABLE products RENAME COLUMN prod_id TO product_id;

Output

Query OK.

Then check with DESCRIBE products

FieldTypeNullKeyDefaultExtra
product_idchar(10)NOPRINULL
prod_namechar(255)NONULL
pricedecimal(8,2)NONULL
quantityint(11)YESNULL
descriptionvarchar(255)YESNULL

RENAME COLUMN needs MySQL 8.0 or MariaDB 10.5 or newer. In older versions use CHANGE prod_id product_id CHAR(10) NOT NULL.

Question 3Change the data type of the "prod_name" column from CHAR to VARCHAR(25).

Show the answer and the output
ALTER TABLE products MODIFY prod_name VARCHAR(25) NOT NULL;

Output

Query OK.

Then check with DESCRIBE products

FieldTypeNullKeyDefaultExtra
product_idchar(10)NOPRINULL
prod_namevarchar(25)NONULL
pricedecimal(8,2)NONULL
quantityint(11)YESNULL
descriptionvarchar(255)YESNULL

MODIFY replaces the whole column definition, so repeat NOT NULL or the column becomes nullable. The change fails if any existing name is longer than 25 characters.

Different from the PDF: Added NOT NULL, which the PDF answer silently dropped.

Question 4Add a constraint to the products table so that the quantity column only contains values less than 10.

Show the answer and the output
ALTER TABLE products MODIFY quantity INT CHECK (quantity < 10);

Output

Query OK.

Then check with SELECT CONSTRAINT_NAME, CHECK_CLAUSE FROM information_schema.CHECK_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = 'products'

CONSTRAINT_NAMECHECK_CLAUSE
quantity`quantity` < 10

Check this on your database. Several products here already have a quantity of 10 or more. MySQL 8 checks existing rows when you add a constraint, so this statement fails with error 3819 until you fix those rows (for example UPDATE products SET quantity = 9 WHERE quantity >= 10;). MariaDB accepts it and only checks rows you insert or update later, which is what the output below shows. Adding the constraint when you create the table avoids the problem.

Different from the PDF: The PDF answer did not mention that MySQL 8 rejects this when existing rows break the rule.

Question 5Delete the column "quantity" from the products table.

Show the answer and the output
ALTER TABLE products DROP COLUMN quantity;

Output

Query OK.

Then check with DESCRIBE products

FieldTypeNullKeyDefaultExtra
product_idchar(10)NOPRINULL
prod_namevarchar(25)NONULL
pricedecimal(8,2)NONULL
descriptionvarchar(255)YESNULL

Question 6Set a default value of 20 for the price column.

Show the answer and the output
ALTER TABLE products ALTER COLUMN price SET DEFAULT 20;

MODIFY form

ALTER TABLE products MODIFY price DECIMAL(8,2) NOT NULL DEFAULT 20;

Output

Query OK.

Then check with DESCRIBE products

FieldTypeNullKeyDefaultExtra
product_idchar(10)NOPRINULL
prod_namevarchar(25)NONULL
pricedecimal(8,2)NO20.00
descriptionvarchar(255)YESNULL

Different from the PDF: The PDF answer had a typo (DEAFULT). Corrected, and the simpler SET DEFAULT form is used.

Question 7Increase the maximum length of the product_id column from 10 to 15.

Show the answer and the output
ALTER TABLE products MODIFY product_id CHAR(15) NOT NULL;

Output

Query OK.

Then check with DESCRIBE products

FieldTypeNullKeyDefaultExtra
product_idchar(15)NOPRINULL
prod_namevarchar(25)NONULL
pricedecimal(8,2)NO20.00
descriptionvarchar(255)YESNULL

This is the column renamed in Question 2. It is the primary key, so it stays NOT NULL.

Different from the PDF: Added NOT NULL so the definition matches the primary key column.

Question 8Rename the "products" table to "product_details".

Show the answer and the output
ALTER TABLE products RENAME TO product_details;

Output

Query OK.

Then check with SHOW TABLES

Tables_in_t_alter
product_details

Want these as PDFs? Download the question PDF or the answer PDF to practise offline.