ALTER TABLE in SQL

Level 5216 keystrokes

ALTER TABLE changes a table's structure; one statement can add columns, add an index and modify a column.

SQL code

-- Change the structure of products; the changes are separated by commas
1ALTER TABLE products
-- New integer column; existing rows get 0
2 ADD COLUMN stock INT DEFAULT 0,
-- New timestamp column, set to the current time on insert
3 ADD COLUMN updated_at TIMESTAMP
4 DEFAULT CURRENT_TIMESTAMP
-- ...and refreshed automatically whenever the row is updated
5 ON UPDATE CURRENT_TIMESTAMP,
-- Index on category to speed up searches and sorts by category
6 ADD INDEX idx_category (category),
-- MODIFY replaces the whole column definition: now up to 200 characters, still required
7 MODIFY COLUMN name VARCHAR(200) NOT NULL;

Output

Query OK, 0 rows affected
Records: 0  Duplicates: 0  Warnings: 0
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • _× 4
  • ,× 3
  • (× 2
  • )× 2
  • ;× 1

More SQL examples at level 5

← SQL typing practice