Web www.tizag.com

SQL - Delete

Entire rows can be deleted from a table using the delete clause.

SQL Code:

DELETE FROM employees
WHERE id='4';

We could use the above code to delete an employee using their unique employee number that has been assigned by the auto_increment field.

SQL - Deleting Multiple Rows

Delete is not limited to one row at a time. We may use all of our known SQL operators as well as subqueries to delete any or all rows that apply to our conditional.

SQL Code:

DELETE FROM employees
WHERE id <= '999';

The above example would delete every row from our table since we had previously limited our "id" field when we set up the table. Predicates, expressions, subqueries, and operators can be used to delete any rows you would like to remove from your table.

SQL - Truncate

We can clear an entire table using a truncate statement. Truncate quickly clears all rows of a table without deleting the table itself. If you are following along, we don't recommend that you run this script because you will essentially undo all of your inserts and have only empty table field to show for it, but it is a handy statement to know and use when the time is right.

SQL Code:

TRUNCATE TABLE employees;




New - Tizag.com Forums!
Recent Forum Topics: