MySQL 支持存储例程(存储过程和函数)。存储例程是一组可以存储在服务器中的 SQL 语句。一旦存储完成,客户端无需继续重复发送各个语句,而是可以直接引用存储例程。MySQL supports stored routines (procedures and functions). A stored routine is a set of SQL statements that can be stored in the server. Once this has been done, clients don't need to keep reissuing the individual statements but can refer to the stored routine instead.
存储例程在某些情况下特别有用:Stored routines can be particularly useful in certain situations:
当多个客户端应用程序使用不同的语言编写或在不同的平台上运行,但需要执行相同的数据库操作时。When multiple client applications are written in different languages or work on different platforms, but need to perform the same database operations.
当安全性至关重要时。例如,银行对所有常规操作都使用存储过程和函数。这提供了一个一致且安全的环境,例程可以确保每个操作都被正确记录。在这种设置下,应用程序和用户将无法直接访问表,只能执行特定的存储例程。When security is paramount. Banks, for example, use stored procedures and functions for all common operations. This provides a consistent and secure environment, and routines can ensure that each operation is properly logged. In such a setup, applications and users would have no access to the database tables directly, but can only execute specific stored routines.
存储过程可以提供更高的性能,因为需要在服务器和客户端之间传输的信息较少。权衡在于,这确实会增加数据库服务器的负载,因为更多的工作是在服务器端完成的,而客户端(应用程序)端的工作较少。如果有许多客户端机器(例如 Web 服务器)仅由一个或少数数据库服务器提供服务,则需要考虑这一点。Stored routines can provide improved performance because less information needs to be sent between the server and the client. The tradeoff is that this does increase the load on the database server because more of the work is done on the server side and less is done on the client (application) side. Consider this if many client machines (such as Web servers) are serviced by only one or a few database servers.
存储过程还使您能够在数据库服务器中拥有函数库。这是现代应用程序语言也具备的功能,这些语言能够在内部实现这种设计(例如,通过使用类)。即便在数据库使用范围之外,使用这些客户端应用程序语言功能也对程序员有益。Stored routines also enable you to have libraries of functions in the database server. This is a feature shared by modern application languages that enable such design internally (for example, by using classes). Using these client application language features is beneficial for the programmer even outside the scope of database use.
MySQL 遵循 SQL:2003 的存储过程语法,这种语法也被 IBM 的 DB2 使用。此处描述的所有语法均受支持,并且任何限制和扩展均在适当地方进行了记录。MySQL follows the SQL:2003 syntax for stored routines, which is also used by IBM's DB2. All syntax described here is supported and any limitations and extensions are documented where appropriate.
附加资源Additional Resources
在使用存储过程和函数时,您可能会发现存储过程用户论坛 很有用。You may find the Stored Procedures User Forum of use when working with stored procedures and functions.
有关 MySQL 存储例程的一些常见问题的答案,请参见 A.4 节,“MySQL 8.4 FAQ:存储过程和函数”。For answers to some commonly asked questions regarding stored routines in MySQL, see Section A.4, “MySQL 8.4 FAQ: Stored Procedures and Functions”.
存储例程的使用存在一些限制。请参见 27.8 节,“存储程序的限制”。There are some restrictions on the use of stored routines. See Section 27.8, “Restrictions on Stored Programs”.
存储例程的二进制日志记录如 27.7 节,“存储程序的二进制日志记录” 所描述。Binary logging for stored routines takes place as described in Section 27.7, “Stored Program Binary Logging”.