如何创建一个存储过程来从 MySQL 表格中删除值?
我们可以创建一个带有 IN 运算符的存储过程,以从 MySQL 表格中删除值。为了帮助大家理解,我们以一个名为“student_info”的表格为例,其中包含以下数据 −
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)
现在,通过创建一个名为“delete_studentinfo”的过程如下,我们可以从“student_info”表格中删除值 −
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 ; //
现在,使用我们希望从表格中删除的值调用该过程,如下所示 −
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)
以上结果集显示 id = 100 的记录已从表格中删除。
广告