Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Database Foundations Manipulating Schema with SQL Altering Tables

Sean Flanagan
Sean Flanagan
33,235 Points

DELETE vs DROP vs TRUNCATE

Hi.

So have I got this right? DELETE deletes one table row, TRUNCATE deletes every row in a table, and DROP deletes the entire table?

Thanks in advance.

Sean

3 Answers

Steven Parker
Steven Parker
231,261 Points

Close. You're right about TRUNCATE and DROP. Now DELETE can remove a single row, it can also delete every row. For example:

DELETE FROM mytable WHERE id = 21;

...would remove just one row (assuming id is a unique key and there is a row with 21 in it). But just this:

DELETE FROM mytable;

...would remove every row. By using different WHERE conditions you could also delete more than one, but not all rows.

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Steven.

DELETE comes across as flexible.

Thanks for the clarification. :-)

Sean

Chung-Jun Wang
Chung-Jun Wang
3,718 Points

More details for these three keywords are:

DELETE will go into the table and delete row one by one, so if you have lots of rows like 1,000,000, it will take lots of time.

TRUNCATE will simply remove the table and re-create one empty table with same table name and same columns, so it will much faster if you want to remove all data from a big table and keep the empty table.

DROP will simply remove the table, so you will not have that table anymore.

Sam Donald
Sam Donald
36,305 Points

Steven Parker don't you need to use the * to reference everything?

DELETE * FROM mytable;
Steven Parker
Steven Parker
231,261 Points

The * is used in a SELECT to represent all columns. But since DELETE works on rows, you don't need to specify columns.