YouTip LogoYouTip

Mysql Delete Query

# MySQL DELETE Statement You can use the DELETE FROM command to delete records from a MySQL table. You can execute this command at the mysql> command prompt or within a PHP script. ### Syntax Here is the general syntax for the DELETE statement to delete data from a MySQL table: DELETE FROM table_name WHERE condition; **Parameter Explanation:** * `table_name` is the name of the table from which you want to delete data. * `WHERE condition` is an optional clause used to specify which rows to delete. If the `WHERE` clause is omitted, all rows in the table will be deleted. **More Explanation:** * If the WHERE clause is not specified, all records in the MySQL table will be deleted. * You can specify any condition in the WHERE clause. * You can delete records from a single table in one go. The WHERE clause is very useful when you want to delete specific records from a table. ### Examples The following examples demonstrate how to use the DELETE statement. 1. Delete rows that meet a condition: DELETE FROM students WHERE graduation_year = 2021; The above SQL statement deletes all records from the `students` table where `graduation_year` is 2021. 2. Delete all rows: DELETE FROM orders; The above SQL statement deletes all records from the `orders` table, but the table structure remains unchanged. 3. Delete rows that meet a condition using a subquery: DELETE FROM customers WHERE customer_id IN ( SELECT customer_id FROM orders WHERE order_date **Note:** When using the DELETE statement, ensure you provide sufficient conditions to ensure only the rows you want to delete are removed. If you do not provide a WHERE clause, all rows in the table will be deleted, which may lead to unpredictable results. * * * ## Deleting Data from the Command Line Here we will use the WHERE clause in the DELETE command to delete selected data from the MySQL table `tutorial_tbl`. ### Example The following example will delete the record where `tutorial_id` is 3 from the `tutorial_tbl` table: ## DELETE Statement: mysql>use ; Database changed mysql>DELETE FROM tutorial_tbl WHERE tutorial_id=3; Query OK, 1 row affected(0.23 sec) * * * ## Deleting Data Using PHP Script PHP uses the `mysqli_query()` function to execute SQL statements. You can use the DELETE command with or without a WHERE clause. This function has the same effect as executing the SQL command at the mysql> command prompt. ### Example The following PHP example will delete the record where `tutorial_id` is 3 from the `tutorial_tbl` table: ## MySQL DELETE Clause Test:
← Mysql Like ClauseMysql Update Query β†’