Dropping a Table or Database
The last of the structure commands, and the shortest — which is unfortunate, because it is also the most destructive. drop does not empty a table. It removes the table itself, and everything that was in it, immediately.
1Dropping a table
temp1 is gone from the list. Not emptied — gone. Its columns, its data types, its primary key and every row it held no longer exist.
2Dropping a database
The same idea one level up. This removes the container and every table inside it:
Six databases, then five. There was no “are you sure?”, no warning, and nothing in a recycle bin. Had scratchdb held twenty tables and a year of records, the reply would have been the same.
drop cannot be reversed. The only protection is a backup taken beforehand. Read the name twice before pressing Enter — especially with drop database, where one wrong word costs every table at once.3drop is not delete
These two are confused constantly, and the difference is the whole point of this lesson: one removes rows, the other removes the table.
| delete from students; | drop table students; | |
|---|---|---|
| What goes | Every row | The rows AND the table itself |
| Does the table still exist? | Yes, empty | No — it is not in show tables any more |
| Can you insert into it afterwards? | Yes, straight away | No — you would have to create it again first |
| Which sub-language | DML — it changes data | DDL — it changes structure |
After delete, select * from students; gives you Empty set — the table is there and has nothing in it. After drop, the same query gives you ERROR 1146: Table 'lambdalab.students' doesn't exist.
delete, not drop. Watch for the words structure and rows in the question — they are what decides it.4Two errors you will meet
Creating something that is already there:
And opening something that is not:
Look closely at that second one — lambdlab. The n and the g are the wrong way round. The error printed the name exactly as typed, which is how you spot it.
5Recap
Removes the table and everything in it. DDL.
Removes the database and every table inside it.
Removes the rows; the empty table remains. DML.
No confirmation and no recycle bin. Back up first.
You want to remove all rows from a table but keep the table itself. Which command?
After drop table students;, what does select * from students; return?
drop table belongs to which sub-language of SQL?