Hook: ALTER is the SQL version of remodeling a house: you keep the structure, but change the rooms, wiring, or rules.
Question: What is ALTER in SQL?
Answer: ALTER is a DDL statement, meaning a command that changes the database schema, or structure. Most often you use it with ALTER TABLE to add, remove, rename, or modify columns and constraints. It does not change row values directly like UPDATE; it changes the definition of the table itself.
Interview-Ready Answer: I use ALTER when I need to change an existing database object without recreating it. In practice, that usually means ALTER TABLE to add a column, rename a column, change a data type, or add a constraint. A key detail is that some ALTER operations are just metadata changes and are very fast, while others may lock or rewrite a large table, so I always think about production impact too.
ALTER belongs to DDL, which stands for Data Definition Language. That means it changes the shape of the database, not the rows themselves. The shape is called the schema (the structure of tables, columns, indexes, and constraints). Databases keep this structure in internal metadata and a catalog (the database’s own record of objects and rules).
NOT NULL forces the database to verify that no row contains NULL.That mental model matters in interviews: ALTER is often not about touching every row. Sometimes it is just changing the blueprint. Other times the engine must physically reshape the table, which is why some ALTERs are cheap and some are expensive.
| Command | What it changes | Typical cost |
|---|---|---|
CREATE | Makes a new object | Usually straightforward |
ALTER | Changes an existing object | Ranges from instant to heavy |
UPDATE | Changes row data | Depends on rows touched |
DROP | Removes an object | Can be instant, but risky |
Inside ALTER TABLE, the most common actions are:
ADD COLUMN — add a new field.ALTER COLUMN — change type, default, nullability, or constraints.RENAME COLUMN or RENAME TABLE — change names without changing data.DROP COLUMN — remove a column definition.ADD CONSTRAINT / DROP CONSTRAINT — add rules like PRIMARY KEY, FOREIGN KEY, or CHECK.CHECK constraint stops bad values at the door.NOT NULL. That avoids breaking existing rows.Performance is the big interview trap. A change that looks tiny in SQL can be huge on a large table.
Rule of thumb: if the change can be proven safe from existing data, it is usually cheaper. If the engine must inspect or transform every row, it becomes expensive.
USING clause.Memory shortcut: think schema surgery. ALTER is not writing new patient data; it is changing the body plan of the table, and that can be quick or delicate depending on the operation.
Imagine a checkout service for an e-commerce site. Product wants a new order_status field and later a stricter rule that every order must have a status. The team uses ALTER TABLE to add the column first, backfills old orders, and only then adds NOT NULL and a CHECK constraint.
What goes wrong when someone skips that safety plan? The deploy adds NOT NULL directly on a table with millions of existing rows. The database must validate the whole table, takes a heavy lock, and the app starts timing out because checkout requests wait behind the schema change. In logs, you might see lock waits like waiting for AccessExclusiveLock, slower API responses, and a spike in abandoned carts. The bug is not the SQL syntax itself; it is misunderstanding the operational cost of the ALTER.
-- PostgreSQL example: ALTER TABLE used safely and with one expected failure path.
-- This script is runnable as-is in PostgreSQL.
CREATE TEMP TABLE customers (
customer_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
full_name text NOT NULL
);
INSERT INTO customers (full_name)
VALUES ('Ava Chen'),
('Ben Ortiz');
-- Add a new column. In PostgreSQL 11+, adding a constant default is often metadata-only.
ALTER TABLE customers
ADD COLUMN email text DEFAULT 'unknown@example.com';
-- Backfill existing rows before tightening the rule.
-- This is the safe production pattern: add, backfill, then constrain.
UPDATE customers
SET email = lower(replace(full_name, ' ', '.')) || '@example.com'
WHERE email = 'unknown@example.com';
-- Now the data is clean, so NOT NULL will succeed.
ALTER TABLE customers
ALTER COLUMN email SET NOT NULL;
-- Rename the column without changing the actual values.
ALTER TABLE customers
RENAME COLUMN full_name TO name;
-- Protect future inserts with a simple data-quality rule.
ALTER TABLE customers
ADD CONSTRAINT email_has_at_sign CHECK (position('@' in email) > 1);
-- Edge case / failure path: bad data is rejected by the new constraint.
DO $$
BEGIN
BEGIN
INSERT INTO customers (name, email)
VALUES ('Bad Email', 'not-an-email');
EXCEPTION WHEN others THEN
RAISE NOTICE 'Expected insert failure after ALTER: %', SQLERRM;
END;
END $$;
-- Show the final shape after the ALTER operations.
SELECT customer_id, name, email
FROM customers
ORDER BY customer_id;Follow-up & Tricky Questions:
ALTER different from UPDATE? ALTER changes the table definition, while UPDATE changes the data stored in rows. If you need a new rule or column, use ALTER; if you need to change values, use UPDATE.ALTER TABLE always lock the table? Yes, but the lock strength and duration vary by database and by operation. Small metadata-only changes may be brief, while validation or rewrites can block reads or writes longer.ALTER be rolled back? In PostgreSQL, many DDL statements are transactional, so a rollback can undo them. In other systems, DDL may auto-commit, so you cannot assume rollback support everywhere.NOT NULL in production? First add the column as nullable, backfill existing rows, verify no nulls remain, and then add NOT NULL. That avoids failing on old data and reduces lock risk.ALTER TABLE ... ADD COLUMN ... DEFAULT ... always expensive? No. In PostgreSQL 11+, a constant default is often fast and metadata-only. But older versions, volatile defaults, or other databases may still rewrite the table.DROP COLUMN immediately free disk space? Not always. The column may disappear from the schema right away, but physical space can be reclaimed later by the engine’s storage cleanup process or a rewrite.Common Mistakes:
ALTER changes row data. Correction: it changes schema; use UPDATE for row values.NOT NULL before backfilling existing rows. Correction: clean the data first, then tighten the constraint.Memory Hook: Create builds the house, ALTER renovates it, UPDATE changes the furniture, DROP bulldozes it.
Cheat Sheet:
ALTER = change an existing database object.ALTER TABLE.Practice Tasks:
ALTER TABLE to add a column and rename another column.NOT NULL and a CHECK constraint.