Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Development Tools Database Foundations Manipulating Schema with SQL Altering Tables

Sean Flanagan
Sean Flanagan
33,232 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
216,083 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,232 Points

Hi Steven.

DELETE comes across as flexible.

Thanks for the clarification. :-)

Sean

Chung-Jun Wang
Chung-Jun Wang
3,717 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
216,083 Points

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