CodeBlanks Logo Icon CodeBlanks

ALTER TABLE

Change an existing table.

ALTER TABLE

ALTER TABLE changes an existing table's structure. Add, drop or modify columns.

ALTER TABLE products
ADD COLUMN stock INT;

This query adds a stock column to products.

ALTER TABLE — details

ALTER TABLE updates the table without losing data. Beware of locking on large tables.

For example, ALTER TABLE customers ADD COLUMN phone VARCHAR(20) adds a phone column without deleting existing customer records.

Practice ALTER TABLE

Add a stock INT column to products.

Starter code

query.sql
-- alter table
ALTER TABLE products

Sample solution

query.sql
ALTER TABLE products
ADD COLUMN stock INT;

Expected output

Tablo güncellendi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does ALTER TABLE do?

  • Changes the table structure Correct answer
  • Deletes data
  • Runs a query
  • Looks up an index

Explanation: ALTER TABLE changes table structure.

2 Add ALTER TABLE

Question
____ products
ADD COLUMN stock INT;
Answer
ALTER TABLE products
ADD COLUMN stock INT;

Explanation: ALTER TABLE is the right answer.

3 ALTER TABLE: match the terms

Term Meaning
ALTER TABLE modify
ADD COLUMN add column
DROP COLUMN drop column
MODIFY change type

All lessons in Intermediate