
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I create a stored procedure to delete values from a MySQL table?
We can create a stored procedure with IN operator to delete values from a MySQL table. To make it understand we are taking an example of a table named ‘student_info’ having the following data −
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 100 | Aarav | Delhi | Computers | | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | +------+---------+------------+------------+ 4 rows in set (0.00 sec)
Now, by creating the procedure named ‘delete_studentinfo’ as follow, we can delete the values from ‘student_info’ table −
mysql> DELIMITER // ; mysql> Create Procedure Delete_studentinfo ( IN p_id INT) -> BEGIN -> DELETE FROM student_info -> WHERE ID=p_id; -> END // Query OK, 0 rows affected (0.11 sec) mysql> DELIMITER ; //
Now, invoke the procedure with the values we want to delete from the table as follows −
mysql> CALL Delete_studentinfo(100); Query OK, 1 row affected (1.09 sec) mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Bangalore | Computers | +------+---------+------------+------------+ 4 rows in set (0.01 sec)
The above result set shows that the record having id = 100 deleted from the table.
- Related Articles
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- How can I create a stored procedure to insert values in a MySQL table?
- How can I create a stored procedure to update values in a MySQL table?
- How can I create a stored procedure to select values on the basis of some conditions from a MySQL table?
- MySQL Stored Procedure to create a table?
- How can I create MySQL stored procedure with IN parameter?
- How can I create MySQL stored procedure with OUT parameter?
- How can I create MySQL stored procedure with INOUT parameter?
- MySQL stored procedure to execute SHOW CREATE TABLE?
- How can we write MySQL stored procedure to select all the data from a table?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Display table records from a stored procedure in MySQL
- How to delete the duplicate values stored in reverse order from MySQL table?
- Create a stored procedure to get the detail of a particular MySQL table stored in a database?
- Create a MySQL stored procedure which fetches the rows from a table by using a cursor?

Advertisements