# CREATE, ALTER & DROP TABLE

> Define tables, change them safely with ALTER TABLE, and understand what DROP does to the data and to the tables that depend on it.

Subject: SQL | Lesson 35 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/create-alter-drop-tables
Previous: [Locks & Deadlocks](https://codewithanimation.in/sql/locks-and-deadlocks.md) | Next: [Constraints & Defaults](https://codewithanimation.in/sql/constraints-and-defaults.md)

## The statements that shape the database

`CREATE TABLE` makes a table: you give it a name and, for each column, a name and a data type. `ALTER TABLE` changes a table that already exists: add, drop or rename a column, add or drop a rule, change a default. `DROP TABLE` removes a table and all its rows.

These are called **DDL** (data definition language) statements. They change the shape of the data, not the data inside it.

## Software changes, so tables must too

No design survives the first year unchanged. A new feature needs a new column, an old column is renamed, a rule is added. Doing it with `ALTER TABLE` keeps the existing rows instead of starting again.

## Add, rule, rename, drop

```sql
CREATE TABLE labels (
  id integer PRIMARY KEY,
  text text NOT NULL
);

ALTER TABLE products ADD COLUMN stock integer DEFAULT 0;
ALTER TABLE products ALTER COLUMN price SET NOT NULL;
ALTER TABLE products RENAME COLUMN name TO title;
ALTER TABLE products DROP COLUMN stock;
```

When you add a column, every existing row gets the default, or `NULL` if you gave none. The products table keeps its three rows, and each now has `stock = 0`.

A new rule is checked against the rows that are already there. `SET NOT NULL` fails if any row has a `NULL` in that column: PostgreSQL says `column "price" of relation "products" contains null values`. Fix the data first, then add the rule. `ADD UNIQUE` likewise fails if two rows already share a value.

## Common mistake: Drops are permanent, and tables depend on each other

`DROP COLUMN` makes the data in that column disappear. `DROP TABLE` removes the table and every row in it. Outside a transaction there is no undo, so back up first.

PostgreSQL will not drop something that others depend on. If `order_items` has a foreign key to `products`, then `DROP TABLE products` fails with `cannot drop table products because other objects depend on it`, and the DETAIL line names the constraint in the way. The default is called `RESTRICT`.

`DROP TABLE products CASCADE` drops the table and also the foreign key constraint that pointed at it. It does not drop `order_items`, only the constraint. To see what CASCADE would remove, run the plain DROP first and read the DETAIL.

Renaming a column keeps the data but breaks every query and program that still uses the old name. And `DROP TABLE` of a table that does not exist is an error, unless you write `DROP TABLE IF EXISTS`.

## Tip: Small, reversible steps

Add the new column first, copy or fill the data, switch your code over, and only then drop the old column. Keep every change in a numbered migration file, and run risky ones inside a transaction.

## Try it: Change the shop database

Run ADD COLUMN price, then SET NOT NULL on price. Then try to drop products in both ways and compare what is left.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/create-alter-drop-tables

## Check yourself

### Question 1

`products` has 3 rows. You run `ALTER TABLE products ADD COLUMN stock integer DEFAULT 0`. What is `stock` in the existing rows?

- A. 0 in every row
- B. NULL in every row
- C. The column is only added to new rows

Correct answer: A

The new column is filled with the default for the rows already in the table.

- Why not B: NULL is used only when you give no DEFAULT. Here a default was given.
- Why not C: A new column exists in every row. Existing rows get the default value.

### Question 2

Some rows have a NULL price. What happens with `ALTER TABLE products ALTER COLUMN price SET NOT NULL`?

- A. It fails, because the existing NULL values break the rule
- B. It changes the NULLs to 0 and succeeds
- C. It succeeds and the rule applies only to new rows

Correct answer: A

A new constraint must hold for the data that is already there, so fix the NULLs first.

- Why not B: PostgreSQL never changes your data to make a rule fit.
- Why not C: The rule is checked immediately against all existing rows.

### Question 3

`order_items` has a foreign key to `products`. You run `DROP TABLE products CASCADE`. What happens to `order_items`?

- A. It stays, with its rows, but loses the foreign key constraint
- B. It is dropped too
- C. Nothing happens, because the DROP is refused

Correct answer: A

The manual says CASCADE removes the dependent objects. Here the dependent object is only the constraint.

- Why not B: CASCADE removes the dependent constraint, not the table that held it.
- Why not C: Without CASCADE it is refused. With CASCADE it goes ahead.

## Remember this

- CREATE TABLE defines a table. ALTER TABLE changes it. DROP TABLE removes it and all its rows.
- A new column is filled with its default, or with NULL if there is none.
- A new rule is checked at once against the existing rows, so bad data must be fixed first.
- DROP is refused when other objects depend on the table. CASCADE removes the dependent constraints, not the other tables.
