BBampaa

Lessons › SQL › Chapter 7: Creating and changing tables

SQL lesson

DROP TABLE

DROP TABLE deletes a whole table: its columns, its rows, everything. Add IF EXISTS so it doesn't give an error when the table's already gone. If you only want the rows gone but want to keep the table, use TRUNCATE instead.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Remove a table you've finished with

DROP TABLE IF EXISTS old_targets

Result:

Step by step

  1. The table, its columns and its rows: all gone.
    DROP TABLE IF EXISTS old_targets
  2. Safe to run twice.
    DROP TABLE IF EXISTS old_targets
  3. Every row goes. The table and its columns stay.
    DROP TABLE IF EXISTS old_targets

Practice

Try each one in your head first, then open the answer.

1. Remove the table old_targets, with no error if it's already gone.
DROP TABLE IF EXISTS old_targets
2. Empty stock_counts, but keep the table for next month's count.
TRUNCATE TABLE stock_counts
3. Remove the table test_orders. Keep orders.
DROP TABLE test_orders
4. Remove the table march_snapshot, with no error if it's already gone.
DROP TABLE IF EXISTS march_snapshot
5. Empty stock_counts, but keep the table for the next count.
TRUNCATE TABLE stock_counts

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed