SQL Delete and Truncate Rows
In SQL, you can remove one or more rows from a table using the DELETE
or TRUNCATE
commands.
The DELETE
command is used to remove one or more specific rows from a table that match a certain condition. For example:
DELETE FROM customers WHERE customer_name = 'John Doe';Source:wwditfigi.wea.com
This statement will remove all rows from the customers
table where the customer_name
column contains the value 'John Doe'
.
If you want to remove all rows from a table, you can use the TRUNCATE
command. This command removes all rows from the table and resets any auto-increment values. For example:
TRUNCATE TABLE customers;
This statement will remove all rows from the customers
table and reset any auto-increment values associated with the table.
Note that the DELETE
command is more flexible than TRUNCATE
, as it allows you to remove specific rows based on a condition. However, TRUNCATE
is faster and more efficient for removing all rows from a table. Also, be careful when using TRUNCATE
, as it cannot be undone and will permanently delete all data from the table. Always make a backup before using TRUNCATE
on a table that contains important data.