Concepts / Modifying Table Structures with ALTER TABLE

Modifying Table Structures with ALTER TABLE

The pattern DROP TABLE IF EXISTS followed by CREATE TABLE removes an old table when necessary and then defines a new table structure. Its purpose is repeatable setup, not gradual modification of existing rows.

  • Programming

Why Rebuild a Table

A database setup script may need to be run many times: during testing, when resetting data, or when deploying to another environment. If a previous run already created the table, trying to create it again can cause an error. The usual solution in this topic is to remove the existing table first and then create a fresh one.

Key point

The pattern DROP TABLE IF EXISTS followed by CREATE TABLE removes an old table when necessary and then defines a new table structure. Its purpose is repeatable setup, not gradual modification of existing rows.

Track tabletitle and plays columnsTable datarowsDROP TABLETable absentstructure and data removedcontainsremoved byremoved byleaves
What does the database contain before and after DROP TABLE runs, and what data is permanently removed?

What DROP TABLE Removes

DROP TABLE removes the entire table structure and all of its data from the database. After it runs, the table is not available for future row insertion because the table itself no longer exists. This is different from DELETE: DELETE removes rows but leaves the table structure available.

CommandWhat it removesWhat remains
DROP TABLEThe entire table structure and all its dataThe table does not remain
DELETERows in a tableThe table structure remains

Choose DROP TABLE when the whole table must be removed; choose DELETE when the table should remain available.

Worked example

Tracing a Table Removal

A database contains a Track table with title and plays columns and some rows. What remains after DROP TABLE runs?

  1. 1
    Before removal

    The database contains the Track table, its column structure, and its stored rows.

  2. 2
    Run DROP TABLE

    The command removes the table itself rather than only selecting or clearing particular rows.

  3. 3
    After removal

    The Track table and all of its data have been removed. A new table must be created before rows can be inserted into that table name again.

Result

DROP TABLE removes both the table structure and its data.

How IF EXISTS Changes Control Flow

The IF EXISTS clause checks whether the named table is present before the removal is attempted. If the table exists, it is removed. If the table does not exist, the command completes without an error. This makes the command a safety mechanism for scripts that may encounter either database state.

Run scriptCheck tableIF EXISTSRemove tablestructure and dataContinue to CREATETABLENo removalno error1table existstable absentthenthen
What happens when the script runs while the table exists or while it has already been removed?
Table existsTable removedTable absentDatabase remainsabsentno errorIF EXISTS is trueIF EXISTS is false
How does the database's control flow differ when the named table exists versus when it does not exist?

Defining the Replacement Table

Definition

CREATE TABLE defines a new table structure by specifying the table name, the names of its columns, and the data type for each column.

Each column needs a name and a data type. The data type tells the database what kind of information that column stores. In the source example, the Track table has a title column for text data and a plays column for whole-number values. Rows inserted into that table therefore have a title and a plays count.

CREATE TABLEdefine a new tableTracktable nametitleTEXTplaysINTEGERTrack structuretitle and plays columnsnamescontainscontainsdefinesdefines
How do column names and data types in CREATE TABLE become the structure of the new table?
Worked example

Recreating Track

A setup process must create a Track table with a text title and a whole-number plays count after removing any earlier version.

  1. 1
    Remove the earlier table

    Use DROP TABLE IF EXISTS for Track so the operation succeeds whether an earlier Track table is present or absent.

  2. 2
    Define the new structure

    Use CREATE TABLE to define Track with title as a text column and plays as an integer column.

  3. 3
    Prepare for rows

    Once the new structure exists, rows can be inserted with a title and a plays count.

Result

The setup leaves a newly defined Track table with title and plays columns.

Running the Pattern Repeatedly

A typical setup combines DROP TABLE IF EXISTS with CREATE TABLE. On a first run, the named table may not exist, so IF EXISTS allows the removal step to complete without an error; CREATE TABLE then creates the structure. On a later run, the existing table is removed first, and CREATE TABLE creates it again. The same pattern supports repeated testing, data resets, and deployment to a new environment.

Setup scriptDROP TABLE IF EXISTSCREATE TABLEFresh table structure123run again
How does a drop-and-recreate sequence change the database state each time the script is executed?

Mistakes to Avoid

  • Common mistake

    Treating DROP TABLE as if it only removes rows

    What it looks like

    Expecting the table definition to remain after DROP TABLE

    Why it fails

    DROP TABLE removes the entire table structure as well as all its data.

    Do this instead

    Use DELETE when rows should be removed but the table should remain.

  • Common mistake

    Omitting IF EXISTS in a repeatable setup

    What it looks like

    Running the removal step when the named table has already been removed

    Why it fails

    The safety condition is what allows the command to complete without an error when the table does not exist.

    Do this instead

    Use DROP TABLE IF EXISTS when the script must handle both an existing and an absent table.

  • Common mistake

    Assuming CREATE TABLE alone can be run repeatedly

    What it looks like

    Trying to create a table again after a previous run already created it

    Why it fails

    Creating an already existing table can cause an error.

    Do this instead

    Remove the earlier table with DROP TABLE IF EXISTS before using CREATE TABLE when a reset is intended.

  • Common mistake

    Deleting important data without planning

    What it looks like

    Using DROP TABLE on a table whose data is still needed

    Why it fails

    The source describes DROP TABLE as permanent.

    Do this instead

    Back up important data before using DROP TABLE.

Check Your Understanding

MEDIUM

A setup script may run for the first time, after a previous table was created, or after that table has already been removed. Explain why DROP TABLE IF EXISTS is suitable for all three situations, and state what CREATE TABLE contributes after the removal step.

Hints
  • Consider separately what happens when the table exists and when it does not.
  • Remember that IF EXISTS prevents an error when the named table is absent.
  • CREATE TABLE defines the table name, columns, and data types.
EASY

Choose the appropriate operation for each goal: remove an entire table and all its data, or remove rows while preserving the table structure. Explain your choice.

Hints
  • One operation removes the table structure itself.
  • The other operation removes rows but leaves the structure available.

Key Takeaways

  1. 1
    DROP TABLE removes an entire table, including its structure and all stored data.
  2. 2
    IF EXISTS allows the removal command to complete without an error when the named table is absent.
  3. 3
    CREATE TABLE defines a new table by specifying its name, column names, and data types.
  4. 4
    Combining DROP TABLE IF EXISTS with CREATE TABLE supports repeatable setup and reset scripts.
  5. 5
    DROP TABLE is permanent, so important data should be backed up before the command is used.

At a glance

  • DROP TABLE removes both a table's structure and its data.
  • The IF EXISTS condition makes table removal safe when the table may already be absent.
  • CREATE TABLE defines columns and their data types for a new table.
  • The drop-and-recreate pattern lets setup scripts run repeatedly.
  • Because DROP TABLE is permanent, back up important data before using it.

Related concepts

More concepts starting with M