ALTER TABLE — Add & Drop Columns
Change an existing table's structure: add a new column with ALTER TABLE … ADD and remove one with DROP COLUMN.
Mid-season, the franchise wants to store each player's jersey number. You don't want to delete the players table and rebuild it — you just want one more column. That's exactly what ALTER TABLE is for.
Definition
ALTER TABLE is a DDL statement that modifies the structure of an existing table — for example adding a column, removing a column, changing a column's data type, or adding/removing constraints — while keeping the table and its other data.
- Existing table — unlike
CREATE, the table must already exist. - Keeps the data — all rows remain; only the structure changes.
This topic covers ADD and DROP COLUMN. Changing a data type (ALTER COLUMN) has its own topic next.
Syntax
sql
-- Add a column (SQL Server)
ALTER TABLE table_name
ADD column_name data_type [constraint];
-- Remove a column
ALTER TABLE table_name
DROP COLUMN column_name;| Part | Meaning |
|---|---|
ALTER TABLE table_name | Which existing table to change |
ADD column_name data_type | Append a new column at the end of the table |
DROP COLUMN column_name | Delete the column and all values in it |
ADD — keep everything + 1 new column
sql
ALTER TABLE players ADD jersey_no INT;
SELECT * FROM players;| id | name | team | country | runs | jersey_no |
|---|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 | NULL |
| 2 | Rohit Sharma | MI | India | 500 | NULL |
| 3 | Jos Buttler | RR | England | 850 | NULL |
| 4 | MS Dhoni | CSK | India | 350 | NULL |
| 5 | David Warner | DC | Australia | 700 | NULL |
| 6 | Jasprit Bumrah | MI | India | 20 | NULL |
Step by step: 1️⃣ ALTER TABLE players picks the table → 2️⃣ ADD jersey_no INT adds the column.
Existing rows get NULL in the new column until you fill it (with UPDATE). The new column always appears at the end — SQL Server has no "add after column X" option.
ADD a column that needs a value: NOT NULL + DEFAULT
What if the new column must never be NULL? A table that already has rows can't get a plain NOT NULL column — what value would the old rows get? Give it a DEFAULT so SQL Server can fill the existing rows:
sql
ALTER TABLE players
ADD titles INT NOT NULL DEFAULT 0;| id | name | team | country | runs | titles |
|---|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 | 0 |
| 2 | Rohit Sharma | MI | India | 500 | 0 |
| 3 | Jos Buttler | RR | England | 850 | 0 |
| 4 | MS Dhoni | CSK | India | 350 | 0 |
| 5 | David Warner | DC | Australia | 700 | 0 |
| 6 | Jasprit Bumrah | MI | India | 20 | 0 |
You can add several columns at once in SQL Server by separating them with commas: ALTER TABLE players ADD jersey_no INT, role VARCHAR(20);
DROP COLUMN — remove a column
sql
ALTER TABLE players DROP COLUMN jersey_no;Step by step: 1️⃣ ALTER TABLE players → 2️⃣ DROP COLUMN jersey_no. The table goes back to id, name, team, country, runs.
Warning: The column AND its data are removed for good. If 6 players had jersey numbers, all 6 numbers are gone. There is no "undo".
Adding and removing constraints
ALTER TABLE can also add rules to an existing table (SQL Server):
sql
ALTER TABLE players
ADD CONSTRAINT ck_players_runs CHECK (runs >= 0);
ALTER TABLE players
DROP CONSTRAINT ck_players_runs;Common mistakes
- ❌
ALTER TABLE players ADD COLUMN jersey_no INTin SQL Server → syntax error. ✅ SQL Server uses justADD(MySQL/PostgreSQL acceptADD COLUMN). - ❌
ALTER TABLE players DROP jersey_no✅DROP COLUMN jersey_no— the wordCOLUMNis required for dropping. - ❌ Adding a
NOT NULLcolumn without aDEFAULTto a table that has rows → error. ✅ Add aDEFAULT, or add it as NULL, fill it, then make it NOT NULL. - ❌ Dropping a column that has a constraint (e.g. a DEFAULT) in SQL Server → "The object 'DF__players__…' is dependent on column…". ✅
DROP CONSTRAINTfirst, thenDROP COLUMN. - ❌ Expecting the new column to be filled automatically. ✅ It's NULL until you
UPDATEit (unless it has NOT NULL + DEFAULT).
SQL Server vs MySQL / PostgreSQL
| Action | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Add column | ADD col INT | ADD [COLUMN] col INT | ADD [COLUMN] col INT |
| Add at a position | ❌ not possible | ADD col INT AFTER name | ❌ not possible |
| Drop column | DROP COLUMN col | DROP [COLUMN] col | DROP [COLUMN] col |
| Rename column | EXEC sp_rename 'players.name', 'player_name', 'COLUMN' | RENAME COLUMN name TO player_name | RENAME COLUMN name TO player_name |
Interview tip
Q: You add a new nullable column to a table with 1 million rows. What values do the existing rows have?
A: NULL. A nullable column with no default leaves old rows as NULL. If you add it as NOT NULL with a DEFAULT, SQL Server fills the existing rows with the default value instead.
Key points to remember
- ALTER TABLE changes the structure of an existing table while keeping its data.
- SQL Server syntax: ALTER TABLE t ADD col type — no COLUMN keyword when adding.
- Dropping needs the keyword: ALTER TABLE t DROP COLUMN col.
- Existing rows get NULL in a newly added column until you fill it with UPDATE.
- To add a NOT NULL column to a table that has rows, give it a DEFAULT so old rows can be filled.
- DROP COLUMN removes the column and all its data permanently.
- ALTER TABLE ... ADD CONSTRAINT / DROP CONSTRAINT adds or removes rules on an existing table.
Try it yourself
ADD a column — existing rows get NULL
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
ADD, fill with UPDATE, then DROP COLUMN
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
ADD a NOT NULL column with a DEFAULT
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: add a role column
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
