MySQL存储过程编程实战指南

4星 · 超过85%的资源 需积分: 17 75 下载量 184 浏览量 更新于2024-08-01 1 收藏 2.22MB PDF 举报
"MySQL存储过程编程,是MySQL数据库中一种重要的编程功能,用于创建和执行预编译的SQL语句集合。存储过程可以提高数据库的性能,简化复杂的操作,并提供更安全的数据管理。本书由Steven Feuerstein和Guy Harrison合著,针对MySQL 5.0版本,详细介绍了存储过程的各个方面,旨在帮助读者掌握存储过程的编写和优化技术。书中内容涵盖了基础语法、创建过程、事务处理、内置函数、触发器的使用,以及在不同应用程序中如PHP、Java、Perl、Python和.NET环境下的集成与调用。此外,还深入探讨了存储过程的安全性、调试和最佳实践,为读者提供了全面的指导。" MySQL存储过程编程是数据库管理的重要组成部分,尤其在MySQL 5.0及更高版本中,它的引入使得MySQL具备了更强大的企业级应用能力。存储过程允许开发者封装一系列SQL语句,这些语句可以在需要时一次性执行,提高了执行效率并减少了网络通信开销。 基础部分,包括了解存储过程的基本概念,学习如何编写和调用存储过程,以及如何处理在过程中可能出现的错误。这涉及到SQL语句的构造,例如BEGIN和END语句用于定义过程体,DECLARE用于声明变量,以及CALL命令来执行存储过程。 创建存储过程涉及更复杂的功能,如事务处理,确保数据的一致性和完整性。内建函数可以帮助开发者在过程中执行计算和其他操作,而存储过程函数和触发器则允许在特定事件发生时自动执行代码,增强了数据库的自动化处理能力。 在实际的应用程序中,如PHP、Java、Perl、Python和.NET平台,可以调用MySQL的存储过程,进行数据的读写和业务逻辑处理。这需要理解不同编程语言与MySQL的接口,如PHP的mysqli或PDO扩展,Java的JDBC,以及.NET环境下的ADO.NET。 优化存储过程是确保其高效运行的关键,涉及安全性设置以防止未授权访问,通过调试工具找出性能瓶颈,以及遵循最佳编程实践,如避免冗余查询,合理使用索引,以及适时地使用缓存。 MySQL存储过程编程是一门深度和技术性兼具的技能,对于开发和维护高性能、安全的数据库系统至关重要。通过深入学习和实践,开发者可以充分利用存储过程的优势,提升数据库应用的整体质量。
2011-05-07 上传
MySQL Stored Procedure Programming Advance Praise for MySQL Stored Procedure Programming Preface Objectives of This Book Structure of This Book What This Book Does Not Cover Conventions Used in This Book Which Version? Resources Available at the Book's Web Site Using Code Examples Safari® Enabled How to Contact Us Acknowledgments Part I: Stored Programming Fundamentals Chapter 1. Introduction to MySQL Stored Programs Section 1.1. What Is a Stored Program? Section 1.2. A Quick Tour Section 1.3. Resources for Developers Using Stored Programs Section 1.4. Some Words of Advice for Developers Section 1.5. Conclusion Chapter 2. MySQL Stored Programming Tutorial Section 2.1. What You Will Need Section 2.2. Our First Stored Procedure Section 2.3. Variables Section 2.4. Parameters Section 2.5. Conditional Execution Section 2.6. Loops Section 2.7. Dealing with Errors Section 2.8. Interacting with the Database Section 2.9. Calling Stored Programs from Stored Programs Section 2.10. Putting It All Together Section 2.11. Stored Functions Section 2.12. Triggers Section 2.13. Calling a Stored Procedure from PHP Section 2.14. Conclusion Chapter 3. Language Fundamentals Section 3.1. Variables, Literals, Parameters, and Comments Section 3.2. Operators Section 3.3. Expressions Section 3.4. Built-in Functions Section 3.5. Data Types Section 3.6. MySQL 5 "Strict" Mode Section 3.7. Conclusion Chapter 4. Blocks, Conditional Statements, and Iterative Programming Section 4.1. Block Structure of Stored Programs Section 4.2. Conditional Control Section 4.3. Iterative Processing with Loops Section 4.4. Conclusion Chapter 5. Using SQL in Stored Programming Section 5.1. Using Non-SELECT SQL in Stored Programs Section 5.2. Using SELECT Statements with an INTO Clause Section 5.3. Creating and Using Cursors Section 5.4. Using Unbounded SELECT Statements Section 5.5. Performing Dynamic SQL with Prepared Statements Section 5.6. Handling SQL Errors: A Preview Section 5.7. Conclusion Chapter 6. Error Handling Section 6.1. Introduction to Error Handling Section 6.2. Condition Handlers Section 6.3. Named Conditions Section 6.4. Missing SQL:2003 Features Section 6.5. Putting It All Together Section 6.6. Handling Stored Program Errors in the Calling Application Section 6.7. Conclusion Part II: Stored Program Construction Chapter 7. Creating and Maintaining Stored Programs Section 7.1. Creating Stored Programs Section 7.2. Editing an Existing Stored Program Section 7.3. SQL Statements for Managing Stored Programs Section 7.4. Getting Information About Stored Programs Section 7.5. Conclusion Chapter 8. Transaction Management Section 8.1. Transactional Support in MySQL Section 8.2. Defining a Transaction Section 8.3. Working with Savepoints Section 8.4. Transactions and Locks Section 8.5. Transaction Design Guidelines Section 8.6. Conclusion Chapter 9. MySQL Built-in Functions Section 9.1. String Functions Section 9.2. Numeric Functions Section 9.3. Date and Time Functions Section 9.4. Other Functions Section 9.5. Conclusion Chapter 10. Stored Functions Section 10.1. Creating Stored Functions Section 10.2. SQL Statements in Stored Functions Section 10.3. Calling Stored Functions Section 10.4. Using Stored Functions in SQL Section 10.5. Conclusion Chapter 11. Triggers Section 11.1. Creating Triggers Section 11.2. Using Triggers Section 11.3. Trigger Overhead Section 11.4. Conclusion Part III: Using MySQL Stored Programs in Applications Chapter 12. Using MySQL Stored Programs in Applications Section 12.1. The Pros and Cons of Stored Programs in Modern Applications Section 12.2. Advantages of Stored Programs Section 12.3. Disadvantages of Stored Programs Section 12.4. Calling Stored Programs from Application Code Section 12.5. Conclusion Chapter 13. Using MySQL Stored Programs with PHP Section 13.1. Options for Using MySQL with PHP Section 13.2. Using PHP with the mysqli Extension Section 13.3. Using MySQL with PHP Data Objects Section 13.4. Conclusion Chapter 14. Using MySQL Stored Programs with Java Section 14.1. Review of JDBC Basics Section 14.2. Using Stored Programs in JDBC Section 14.3. Stored Programs and J2EE Applications Section 14.4. Using Stored Procedures with Hibernate Section 14.5. Using Stored Procedures with Spring Section 14.6. Conclusion Chapter 15. Using MySQL Stored Programs with Perl Section 15.1. Review of Perl DBD::mysql Basics Section 15.2. Executing Stored Programs with DBD::mysql Section 15.3. Conclusion Chapter 16. Using MySQL Stored Programs with Python Section 16.1. Installing the MySQLdb Extension Section 16.2. MySQLdb Basics Section 16.3. Using Stored Programs with MySQLdb Section 16.4. A Complete Example Section 16.5. Conclusion Chapter 17. Using MySQL Stored Programs with .NET Section 17.1. Review of ADO.NET Basics Section 17.2. Using Stored Programs in ADO.NET Section 17.3. Using Stored Programs in ASP.NET Section 17.4. Conclusion Part IV: Optimizing Stored Programs Chapter 18. Stored Program Security Section 18.1. Permissions Required for Stored Programs Section 18.2. Execution Mode Options for Stored Programs Section 18.3. Stored Programs and Code Injection Section 18.4. Conclusion Chapter 19. Tuning Stored Programs and Their SQL Section 19.1. Why SQL Tuning Is So Important Section 19.2. How MySQL Processes SQL Section 19.3. SQL Tuning Statements and Practices Section 19.4. About the Upcoming Examples Section 19.5. Conclusion Chapter 20. Basic SQL Tuning Section 20.1. Tuning Table Access Section 20.2. Tuning Joins Section 20.3. Conclusion Chapter 21. Advanced SQL Tuning Section 21.1. Tuning Subqueries Section 21.2. Tuning "Anti-Joins" Using Subqueries Section 21.3. Tuning Subqueries in the FROM Clause Section 21.4. Tuning ORDER and GROUP BY Section 21.5. Tuning DML (INSERT, UPDATE, DELETE) Section 21.6. Conclusion Chapter 22. Optimizing Stored Program Code Section 22.1. Performance Characteristics of Stored Programs Section 22.2. How Fast Is the Stored Program Language? Section 22.3. Reducing Network Traffic with Stored Programs Section 22.4. Stored Programs as an Alternative to Expensive SQL Section 22.5. Optimizing Loops Section 22.6. IF and CASE Statements Section 22.7. Recursion Section 22.8. Cursors Section 22.9. Trigger Overhead Section 22.10. Conclusion Chapter 23. Best Practices in MySQL Stored Program Development Section 23.1. The Development Process Section 23.2. Coding Style and Conventions Section 23.3. Variables Section 23.4. Conditional Logic Section 23.5. Loop Processing Section 23.6. Exception Handling Section 23.7. SQL in Stored Programs Section 23.8. Dynamic SQL Section 23.9. Program Construction Section 23.10. Performance Section 23.11. Conclusion About the Author Colophon Index