- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements