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 droppedSafe drop.
Example 2 (sql)
DROP TABLE users CASCADE;Output
Table + dependent objects droppedCASCADE 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.
