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!
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
Sean Flanagan33,234 Points
DELETE vs DROP vs TRUNCATE
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.
Steven Parker228,001 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.
Chung-Jun Wang3,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.