ALTER TABLE and DROP TABLE

Updated · 11 min read
mediumPostgreSQL 18sqlalter-tabledrop-tabletruncateddlinterview

ALTER TABLE changes a table that already exists, and DROP TABLE removes it completely. Tables rarely stay exactly as you first designed them. Navika's study_sessions log works, but now she wants a column for notes, a clearer name for minutes, and a longer limit on topic. This lesson covers adding, renaming and dropping columns, changing a column's type, renaming a table, and the two ways to get rid of data in bulk: TRUNCATE TABLE, which empties a table, and DROP TABLE, which deletes it.

Every example starts from Navika's study log with two sessions in it:

SQL
CREATE TABLE study_sessions (
  session_id INTEGER,
  student_id INTEGER,
  studied_on DATE,
  minutes    INTEGER,
  topic      VARCHAR(60)
);
 
INSERT INTO study_sessions VALUES
  (1, 103, '2026-01-05', 45, 'JavaScript loops'),
  (2, 101, '2026-01-05', 90, 'Java streams');

In One Minute

  • ALTER TABLE t ADD COLUMN c type; adds a column; existing rows get NULL, or the DEFAULT you give.
  • RENAME COLUMN a TO b renames a column; DROP COLUMN c deletes it and its data.
  • ALTER COLUMN c TYPE new_type changes a type, if every existing value can be converted.
  • TRUNCATE TABLE t removes every row but keeps the table; DROP TABLE t removes the table itself.
  • In PostgreSQL these can be undone inside a transaction; in MySQL and Oracle they commit at once.

Adding a Column

SQL
ALTER TABLE table_name ADD COLUMN column_name data_type;

Navika adds a notes column:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
ALTER TABLE study_sessions ADD COLUMN notes TEXT;
 
SELECT * FROM study_sessions ORDER BY session_id;
Output
CREATE TABLE
 
INSERT 0 2
 
ALTER TABLE
 
 session_id | student_id | studied_on | minutes |      topic       | notes
------------+------------+------------+---------+------------------+-------
          1 |        103 | 2026-01-05 |      45 | JavaScript loops | NULL
          2 |        101 | 2026-01-05 |      90 | Java streams     | NULL
(2 rows)

The new column goes at the end, and the two existing rows get NULL, because nobody has written notes for them yet. A column that must have a value needs a DEFAULT to fill the existing rows:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
ALTER TABLE study_sessions ADD COLUMN mood VARCHAR(10) NOT NULL DEFAULT 'focused';
 
SELECT session_id, mood FROM study_sessions ORDER BY session_id;
Output
CREATE TABLE
 
INSERT 0 2
 
ALTER TABLE
 
 session_id |  mood
------------+---------
          1 | focused
          2 | focused
(2 rows)

Without the DEFAULT, PostgreSQL would have to put NULL into a NOT NULL column, so it refuses: column "mood" of relation "study_sessions" contains null values.

A new column at the end: NULL for existing rows, or the DEFAULT you give

Renaming and Dropping a Column

RENAME COLUMN changes only the name; the data stays.

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
ALTER TABLE study_sessions RENAME COLUMN minutes TO duration_minutes;
 
SELECT session_id, duration_minutes FROM study_sessions ORDER BY session_id;
Output
CREATE TABLE
 
INSERT 0 2
 
ALTER TABLE
 
 session_id | duration_minutes
------------+------------------
          1 |               45
          2 |               90
(2 rows)

Any saved query that still says minutes will now fail, so rename with care in a shared database.

DROP COLUMN deletes the column and every value in it. Adding the column back gives an empty column, not the old values:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
ALTER TABLE study_sessions DROP COLUMN topic;
ALTER TABLE study_sessions ADD COLUMN topic VARCHAR(60);
 
SELECT * FROM study_sessions ORDER BY session_id;
Output
CREATE TABLE
 
INSERT 0 2
 
ALTER TABLE
 
ALTER TABLE
 
 session_id | student_id | studied_on | minutes | topic
------------+------------+------------+---------+-------
          1 |        103 | 2026-01-05 |      45 | NULL
          2 |        101 | 2026-01-05 |      90 | NULL
(2 rows)

"JavaScript loops" and "Java streams" are gone for good. PostgreSQL also won't drop a column that other tables point to: ALTER TABLE students DROP COLUMN student_id fails because enrollments and payments reference it.

Changing a Column's Type

SQL
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_type;

PostgreSQL converts every existing value to the new type, and the change happens only if all of them fit. A longer limit always works:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
ALTER TABLE study_sessions ALTER COLUMN topic TYPE VARCHAR(120);
 
SELECT column_name, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'study_sessions' AND column_name = 'topic';
Output
CREATE TABLE
 
INSERT 0 2
 
ALTER TABLE
 
 column_name | character_maximum_length
-------------+--------------------------
 topic       |                      120
(1 row)

A shorter limit fails if any value is too long, and the whole change is canceled, so no row is cut short:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
ALTER TABLE study_sessions ALTER COLUMN topic TYPE VARCHAR(10);
Output
CREATE TABLE
 
INSERT 0 2
 
ERROR:  value too long for type character varying(10)

And text can't become a number automatically. The hint shows the way: USING says how to convert each value, as in USING minutes_text::integer, which works only if every value really is a number:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
ALTER TABLE study_sessions ALTER COLUMN topic TYPE INTEGER;
Output
CREATE TABLE
 
INSERT 0 2
 
ERROR:  column "topic" cannot be cast automatically to type integer
HINT:  You might need to specify "USING topic::integer".
VARCHAR(120) works; VARCHAR(10) and INTEGER fail and change nothing

The wording differs by database: MySQL writes MODIFY COLUMN topic VARCHAR(120), and SQL Server writes ALTER COLUMN topic VARCHAR(120) without the word TYPE. Renaming a table is ALTER TABLE study_sessions RENAME TO study_log; in PostgreSQL; SQL Server uses EXEC sp_rename instead (SQL Server only). Tabs showing each database's version are coming to this course.

TRUNCATE TABLE: Empty It

TRUNCATE TABLE removes every row at once and keeps the empty table, ready for new rows:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
TRUNCATE TABLE study_sessions;
 
SELECT count(*) AS sessions FROM study_sessions;
Output
CREATE TABLE
 
INSERT 0 2
 
TRUNCATE TABLE
 
 sessions
----------
        0
(1 row)

It does the same job as DELETE FROM study_sessions; with no WHERE, but much faster on a big table, because it doesn't look at the rows one by one. It can't filter, though: it's all rows or nothing. And PostgreSQL won't truncate a table that other tables point to:

SQL▶ Try it
TRUNCATE TABLE students;
Output
ERROR:  cannot truncate a table referenced in a foreign key constraint
DETAIL:  Table "enrollments" references "students".
HINT:  Truncate table "enrollments" at the same time, or use TRUNCATE ... CASCADE.

DROP TABLE: Delete It

DROP TABLE deletes the table: its rows, its columns and the table itself.

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
DROP TABLE study_sessions;
 
SELECT * FROM study_sessions;
Output
CREATE TABLE
 
INSERT 0 2
 
DROP TABLE
 
ERROR:  relation "study_sessions" does not exist

DROP TABLE IF EXISTS study_sessions; doesn't complain when the table is already gone. Like TRUNCATE, DROP TABLE students is refused while other tables reference it; DROP TABLE students CASCADE would also remove those foreign keys, so think twice before using it.

DELETE FROM tTRUNCATE TABLE tDROP TABLE t
RemovesThe rows you choose (WHERE)Every rowThe whole table
Table afterwardStill thereStill there, emptyGone
Speed on big tablesRow by rowFastFast
DELETE removes chosen rows, TRUNCATE empties the table, DROP TABLE removes it

Undoing a Change

In PostgreSQL, ALTER, TRUNCATE and DROP TABLE can run inside a transaction, so ROLLBACK undoes them:

SQL▶ Try it
CREATE TABLE study_sessions (session_id INTEGER, student_id INTEGER, studied_on DATE, minutes INTEGER, topic VARCHAR(60));
INSERT INTO study_sessions VALUES (1, 103, '2026-01-05', 45, 'JavaScript loops'), (2, 101, '2026-01-05', 90, 'Java streams');
 
BEGIN;
DROP TABLE study_sessions;
ROLLBACK;
 
SELECT count(*) AS sessions FROM study_sessions;
Output
CREATE TABLE
 
INSERT 0 2
 
BEGIN
 
DROP TABLE
 
ROLLBACK
 
 sessions
----------
        2
(1 row)

The table and both rows are back. That's a PostgreSQL strength: in MySQL and Oracle, statements that change a table's structure commit immediately, so a ROLLBACK afterward can't bring a dropped table back.

Common Mistakes

Adding a NOT NULL column without a DEFAULT. Existing rows would be NULL, so it fails. Give a default, or add the column, fill it, then add NOT NULL.

Expecting DROP COLUMN to be reversible by adding the column back. The new column is empty.

Renaming a column other queries use. They break. Search for uses first.

Using TRUNCATE when you meant to delete some rows. It has no WHERE. Use DELETE ... WHERE.

Assuming ROLLBACK works everywhere. It works for these statements in PostgreSQL, not in MySQL or Oracle.

Interview Corner

What's the difference between DELETE, TRUNCATE and DROP?

DELETE removes the rows matching a WHERE (or all rows), one by one. TRUNCATE removes every row at once and keeps the empty table. DROP TABLE removes the table itself, structure included.

How do you add a NOT NULL column to a table that already has rows?

Give it a default: ALTER TABLE t ADD COLUMN c type NOT NULL DEFAULT value;. Otherwise the existing rows would hold NULL, and the statement fails.

Can you roll back a DROP TABLE?

In PostgreSQL, yes, if it ran inside a transaction that hasn't been committed. In MySQL and Oracle, no: it commits immediately.

Practice

Challenge (module checkpoint): Create a teachers_notes table with note_id INTEGER and note VARCHAR(40). Add a written_on DATE column, change note to TEXT, list the table's columns and types, and then drop the table.

Show solution
SQL▶ Try it
CREATE TABLE teachers_notes (
  note_id INTEGER,
  note    VARCHAR(40)
);
 
ALTER TABLE teachers_notes ADD COLUMN written_on DATE;
ALTER TABLE teachers_notes ALTER COLUMN note TYPE TEXT;
 
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'teachers_notes'
ORDER BY ordinal_position;
 
DROP TABLE teachers_notes;
Output
CREATE TABLE
 
ALTER TABLE
 
ALTER TABLE
 
 column_name | data_type
-------------+-----------
 note_id     | integer
 note        | text
 written_on  | date
(3 rows)
 
DROP TABLE

Three columns, with note now text, and then the table is gone. That completes the chapter: you can create, change and remove tables. Next, Constraints lets a table refuse bad data.

Quick check: ALTER TABLE and DROP TABLE

3 questions
1You run ALTER TABLE study_sessions ADD COLUMN notes TEXT; on a table with two rows. What's in notes?
2Which statement empties a table but keeps it?
3topic holds 'JavaScript loops'. What does ALTER COLUMN topic TYPE VARCHAR(10) do?

Key Takeaways

You want toWrite
Add a columnALTER TABLE study_sessions ADD COLUMN notes TEXT;
Rename a columnALTER TABLE study_sessions RENAME COLUMN minutes TO duration_minutes;
Drop a columnALTER TABLE study_sessions DROP COLUMN notes;
Change a typeALTER TABLE study_sessions ALTER COLUMN topic TYPE VARCHAR(120);
Empty a tableTRUNCATE TABLE study_sessions;
Delete a tableDROP TABLE IF EXISTS study_sessions;

Was this tutorial helpful?

Lesson 30 of 115