SQL CREATE TABLE, ALTER TABLE and DROP TABLE

CREATE TABLE defines a new table, ALTER TABLE changes an existing definition, and DROP TABLE removes the table object and its stored rows.

Lesson 51 of 102Intermediate10 min read

CREATE TABLE defines a new table, ALTER TABLE changes an existing definition, and DROP TABLE removes the table object and its stored rows.

A table definition is the agreement about which fields a record has and which values are allowed. Schema changes revise that agreement. Learn each operation separately so adding an optional note column does not turn into recreating or removing valuable data.

This reference adds detail to the main course. You can read it independently: the small database below supplies every table and row used in the examples. Start with the question each query answers, then compare the returned values with the data rather than memorising a line of syntax.

Set up the practice database

Run this setup once in a new practice database, before the examples. It uses four customers and six sales. Ana has two sales, Ben has one, Cara has two, and Dev has none.

Sale 106 has no customer link; sale 104 has an unknown amount. These deliberate test cases expose missing information and unmatched relationships.

SQL
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name VARCHAR(40) NOT NULL,
  city VARCHAR(40),
  manager_id INTEGER
);
CREATE TABLE sales (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER,
  amount DECIMAL(10,2),
  status VARCHAR(20) NOT NULL,
  created_on DATE NOT NULL
);
INSERT INTO customers (id,name,city,manager_id) VALUES
  (1,'Ana','Leeds',NULL),(2,'Ben','Leeds',1),
  (3,'Cara','Porto',1),(4,'Dev',NULL,2);
INSERT INTO sales (id,customer_id,amount,status,created_on) VALUES
  (101,1,10,'paid','2026-01-01'),
  (102,1,30,'pending','2026-01-02'),
  (103,2,20,'paid','2026-01-02'),
  (104,3,NULL,'pending','2026-02-01'),
  (105,3,20,'paid','2026-02-02'),
  (106,NULL,5,'paid','2026-02-03');

An amount of zero would be a known value. The NULL amount here means the value is unknown. A customer with no matching sale differs from a sale whose customer_id is NULL.

The primary keys identify individual records even when amounts or cities repeat. Dates use year-month-day format. The examples specify sorting whenever order matters; insertion order alone does not guarantee display order.

How CREATE, ALTER and DROP TABLE works

CREATE TABLE names columns and gives them types and constraints. A primary key identifies records, NOT NULL requires presence, and additional constraints express valid relationships or values. CREATE TABLE does not add business records unless the dialect uses a special create-as-query form. Explicit column names and a deliberate key make later INSERT and SELECT statements easier to maintain.

ALTER TABLE changes a definition without necessarily replacing every row manually. Common tasks include adding a column, renaming a column or adjusting a constraint. Syntax and supported operations vary widely. Adding a nullable notes column is a simpler migration than adding a required field to an already populated table, because existing rows need a valid value for any new NOT NULL requirement. Plan a backfill and validation step when the new rule needs data that older rows do not have.

DROP TABLE versus DELETE

DROP TABLE removes both the definition and its rows. DELETE FROM table removes rows but leaves the definition available for new records. Dropping a table can be blocked by dependencies, such as foreign keys or views, or can remove dependent objects when a dialect-specific cascade option is used. Read the dependency impact rather than assuming a similarly named command has the same effect in every engine.

DDL transaction and locking behaviour matter in real migrations. PostgreSQL can roll back many table DDL changes; MySQL commonly commits around DDL and can rebuild a table for some alterations. SQL Server and SQLite have their own locking and transactional rules. A schema migration can block concurrent readers or writers even when its SQL is short. Test the migration on representative data, verify the application can read both old and new states during deployment if necessary, and keep a recovery plan suited to the actual engine.

MySQL, PostgreSQL, SQL Server and SQLite

The displayed simple CREATE and ADD forms work across the four engines. Column type changes, constraint alterations and renames differ. SQLite supports a narrower direct ALTER TABLE feature set than server engines and sometimes requires a carefully planned table rebuild. Do not apply MySQL MODIFY or PostgreSQL ALTER COLUMN TYPE syntax in another engine.

Check the engine-specific syntax and version requirements in SQLite ALTER TABLE, PostgreSQL ALTER TABLE.

Practice task and worked solution

Create a separate practice table with a required label, insert one record and read it back. Use a new table name so the earlier example remains inspectable.

Try the task before reading its worked solution. The supplied rows make the expected answer checkable by hand.

SQL
CREATE TABLE lesson_labels (id INTEGER PRIMARY KEY,label VARCHAR(40) NOT NULL);
INSERT INTO lesson_labels (id,label) VALUES (1,'Beginner');
SELECT id,label FROM lesson_labels;
Output
id | label
1 | Beginner

The primary key gives the row a stable identity and NOT NULL requires a label to be present. The INSERT supplies both columns. The read verifies the creation and insert without requiring a database-specific metadata view or relying on client confirmation messages.

Inspect lesson_notes after ALTER to confirm the added field exists, then insert a second record while omitting extra_info and predict NULL in that optional column. On a separate disposable table, delete its only row and verify the object remains queryable. Drop that disposable object afterwards and confirm the subsequent SELECT fails because its definition is gone.

SQL CREATE TABLE, ALTER TABLE and DROP TABLE Syntax

CREATE, ALTER and DROP TABLE

CREATE TABLE lesson_notes (
 id INTEGER PRIMARY KEY, note VARCHAR(100)
);
ALTER TABLE lesson_notes ADD extra_info VARCHAR(40);
INSERT INTO lesson_notes (id,note,extra_info) VALUES (1,'Check totals','Reviewed');
SELECT id,note,extra_info FROM lesson_notes;

SQL CREATE TABLE, ALTER TABLE and DROP TABLE Summary

  • CREATE TABLE defines columns, types and constraints; it doesn't add any rows.
  • ALTER TABLE ... ADD COLUMN extends an existing table; existing rows get NULL or the column's default.
  • Adding a NOT NULL column to a table with rows needs a default or a backfill first.
  • DROP TABLE removes the definition and all its rows; DELETE removes rows and keeps the table.
  • SQLite supports only some ALTER TABLE changes, so larger changes need a planned table rebuild.

SQL CREATE TABLE, ALTER TABLE and DROP TABLE Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Creating a table and adding a column

The first statement creates two fields, the second adds an optional third field, and the INSERT names all three destinations. The SELECT confirms the resulting schema can store and return the new value. Run this example once in the fresh practice database.

SQL
CREATE TABLE lesson_notes (
 id INTEGER PRIMARY KEY, note VARCHAR(100)
);
ALTER TABLE lesson_notes ADD extra_info VARCHAR(40);
INSERT INTO lesson_notes (id,note,extra_info) VALUES (1,'Check totals','Reviewed');
SELECT id,note,extra_info FROM lesson_notes;
Output
id | note | extra_info
1 | Check totals | Reviewed

Common SQL CREATE TABLE, ALTER TABLE and DROP TABLE Mistakes

Dropping a table to add a column

Dropping and recreating a populated table just to add an optional field can lose its rows, constraints and relationships. Prefer the documented ALTER operation when suitable. Also, adding a NOT NULL column to existing records without an acceptable default or backfill can fail or impose the wrong value on historical data.

SQL CREATE TABLE, ALTER TABLE and DROP TABLE in Real Projects

Adding a field through a migration

A support application can add a note field as a migration. Introduce the optional field, deploy code that uses it, and add stricter validation later if the product needs it. Keep the migration’s schema purpose distinct from a one-time data correction or a regular transaction update.

Plan a new required field as a data migration as well as a definition change. Historical records need meaningful values, and concurrent application versions may need a period in which the field is optional. Test the specific ALTER operation’s locking and transaction behaviour rather than assuming a short statement is instantly safe on a populated table.

SQL CREATE TABLE, ALTER TABLE and DROP TABLE FAQ

Is DROP TABLE the same as DELETE?

No. DROP TABLE removes the object itself, so later queries fail with "no such table". DELETE FROM table removes records from an object that remains, ready for new rows.

Can every ALTER operation be rolled back?

No universal promise exists. PostgreSQL and SQLite can roll back most schema changes inside a transaction, while MySQL commits many of them immediately. Check the engine and operation, and back up before risky changes.

Why can adding a field fail on an existing table?

Historical rows must satisfy the new definition. A NOT NULL column without a default has no value for existing rows, and a UNIQUE or foreign key rule may already be broken by old data. The operation may need a default, a backfill or a dependency adjustment first.

How do I copy a table's structure?

CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 = 0; copies column names and types without rows in most databases, but not constraints or indexes. MySQL's CREATE TABLE new_table LIKE old_table and PostgreSQL's CREATE TABLE new_table (LIKE old_table INCLUDING ALL) copy more of the definition.

How do I remove a column?

ALTER TABLE customers DROP COLUMN manager_id; works in PostgreSQL, MySQL, SQL Server and SQLite from version 3.35. The column's data is lost, and views, indexes or constraints that use it may block the change, so check dependencies and back up first.

SQL CREATE TABLE, ALTER TABLE and DROP TABLE Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choicemedium

Which fields exist in lesson_notes after its ADD operation?

SQL CREATE TABLE, ALTER TABLE and DROP TABLE Quiz

2 questions to check what you learned.

  1. Which command adds a column to an existing table?

    • ALTER TABLE
    • INSERT INTO
    • CREATE INDEX
  2. What remains after DELETE removes every row?

    • No table definition; DELETE drops the object when the last row is removed
    • The table definition remains
    • A table whose columns have all been converted to NULL