MySQL supports Stored Procedures which can execute set of codes by applying relevant business logics. In this post we will see how to create a stored procedure and execute it.
Let us create the following tables
CREATE TABLE items(item_id INT, item_description VARCHAR(100));<br> CREATE TABLE sales(sales_id INT auto_increment KEY,item_id INT, sales_date DATETIME, sales_amount DECIMAL(12,2));<br> INSERT INTO items VALUES (1,'Television');<br> INSERT INTO items VALUES (2,'Mobile');<br> INSERT INTO items VALUES (3,'laptop');<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (1,'2014-01-01',1200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-01-02',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-01-09',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-01-29',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-02-11',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (1,'2014-02-16',1200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-16',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-20',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-20',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (2,'2014-02-22',200);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (3,'2014-02-24',1700);<br> INSERT INTO sales(item_id,sales_date,sales_amount) VALUES (1,'2014-02-24',1200);
Suppose you want to create a stored procedure which will accept item_description and returns the total sales_amount for theenireperiod, you can do it as shown below
DELIMITER $$<br> CREATE PROCEDURE Test.usp_get_sales<br> (<br> param_item_description VARCHAR(100)<br> )<br> BEGIN<br> SELECT item_description,SUM(sales_amount) AS sales_amount FROM items NATURAL<br> JOIN sales<br> WHERE item_description=param_item_description<br> GROUP BY item_description;<br> END;<br> $$<br> DELIMITER;<br>
Note that the creation of the stored procedure starts with setting the Delimiter $$. The default delimiter for MySQL statements are semicolon so in order to instruct the MySQL engine about the start and end of the stored procedure block, you need to use a different delimiter (which in this case $$ is used).
Now you can execute a stored procedure usingCALL keywordas shown below.
Execution 1
CALL usp_get_sales('Television');
When you execute the above code, the result is
Item_description sales_amountTelevision 3600.00
Execution 2
CALL usp_get_sales('laptop');
When you execute the above code, the result is
Item_description sales_amountlaptop 6800.00
Note:The parameters do not start with @ like we use in SQL Server. So in order todifferentiatebetween the actual column name and parameter name, the name param_item_description is used. You may need to use different naming conventions as you like.
I have previously written a similar article here: MySQL – How to Create Stored Procedure.
Reference:Pinal Dave (http://blog.sqlauthority.com)

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。

选择PostgreSQL而非MySQL的场景包括:1)需要复杂查询和高级SQL功能,2)要求严格的数据完整性和ACID遵从性,3)需要高级空间功能,4)处理大数据集时需要高性能。PostgreSQL在这些方面表现出色,适合需要复杂数据处理和高数据完整性的项目。

MySQL数据库的安全可以通过以下措施实现:1.用户权限管理:通过CREATEUSER和GRANT命令严格控制访问权限。2.加密传输:配置SSL/TLS确保数据传输安全。3.数据库备份和恢复:使用mysqldump或mysqlpump定期备份数据。4.高级安全策略:使用防火墙限制访问,并启用审计日志记录操作。5.性能优化与最佳实践:通过索引和查询优化以及定期维护兼顾安全和性能。

如何有效监控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。1.使用mysqladmin查看连接数。2.用SHOWGLOBALSTATUS查看查询数。3.PMM提供详细性能数据和图形化界面。4.MySQLEnterpriseMonitor提供丰富的监控功能和报警机制。

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显着差异,选择时需考虑项目规模和未来扩展性。

在需要高可用性、高级安全性和良好集成性的企业级应用场景下,应选择SQLServer而不是MySQL。1)SQLServer提供企业级功能,如高可用性和高级安全性。2)它与微软生态系统如VisualStudio和PowerBI紧密集成。3)SQLServer在性能优化方面表现出色,支持内存优化表和列存储索引。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),