SQL ยท Chapter 31 of 42

DROP TABLE

DROP TABLE completely removes a table and all its data. Cannot be undone (except from backups).

Add `IF EXISTS` to avoid an error if the table isn't there.

Example 1 (sql)
DROP TABLE IF EXISTS old_users;
Output
Table dropped

Safe drop.

Example 2 (sql)
DROP TABLE users CASCADE;
Output
Table + dependent objects dropped

CASCADE drops dependent objects (views, foreign keys) โ€” use with care.

Key points

  • Removes table and data.
  • IF EXISTS avoids errors.
  • CASCADE also drops dependent objects.
  • Cannot be undone.
๐Ÿ’ก Note: TRUNCATE keeps the table but wipes rows. DROP removes the table itself.

๐Ÿ“ Quick Quiz

1. DROP TABLE:

2. IF EXISTS is used to:

3. Difference: TRUNCATE vs DROP?