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.