ALTER TABLE
- SQL course
- Intermediate
- Lesson 106/115
- 4 min
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
-- alter table
ALTER TABLE productsSample solution
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
____ products
ADD COLUMN stock INT;
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 |