mysql存储过程编程题_MySQL存储过程编程教程读书笔记-第二章MySQL存储过程编程指南-存_MySQL...

bitsCN.com

MySQL存储过程编程时一个复杂的主题,我们将在本章中为你提供完成基本任务的内容,其中包括:

1、怎样创建存储过程

2、存储过程怎样进行输入输出

3、怎样和数据库交互

4、怎样用MySQL存储编程语言创建过程,函数和触发器

第一个存储过程实例:delimiter $$drop procedure if exists HelloWorld$$create procedure HelloWorld()begin select "Hello World";end $$变量

本地变量可以用declare语句进行声明。变量名称必须遵循MySQL的列明规则,并且可以使MySQL内建的任何数据类型。你可以用default字句给变量一个初始值,并且可以用SET语句给变量赋一个新值delimiter $$drop procedure if exists variable_demo$$begin declare my_integer int; declare my_big_integer bigint; declare my_currency numeric(8, 2); declare my_pi float default 3.1415926; declare my_text text; declare my_dob date default '1960-06-21'; declare my_varchar varchar(30) default "Hello World!"; set my_integer = 20; set my_big_integer = power(my_integer, 3);end $$delimiter;

参数

参数可以使我们的存储程序更为灵活,更为实用。参数包括IN(只读模式), INOUT(可读写模式)和OUT(只写模式)。IN模式作为缺省的参数模式。

IN:任何对于该参数的修改都不会返回给调用它的程序

OUT:这个模式意味着存储过程可以对参数赋值(修改参数的值),并且这个被修改的值会被返回给它的调用程序

INOUT:这个模式意味着存储过程即可读取传入的参数,而且任何对于该参数的修改对于它的调用程序而言都是可见的

对于存储函数而言,只能使用IN模式delimiter $$drop procedure if exists my_sqrt$$create procedure my_sqrt(input_number int, out out_number float)begin set out_number = SQRT(input_number);end $$delimiter;创建和执行使用OUT参数的存储过程call my_sqrt(12, @out_value) $$select @out_value $$条件执行

通过购买量的多少来计算出贴现率的存储程序,购买量超过$500可以返回20%,购买量超过$100可以返回10%。delimiter $$drop procedure if exists discounted_price$$create procedure discounted_price(normal_price NUMERIC(8, 2), out discount_price NUMBERIC(8, 2))begin if (normal_price > 500) then set discount_price = normal_price*.8; else if (normal_price > 100) then set discount_price = normal_price*.9; else set discount_price = normal_price; end if;end$$delimiter;循环

MySQL存储程序语言提供了三种类型的循环

使用LOOP和END LOOP字句的简单循环

当循环条件为真时继续执行的循环,使用WHILE和END WHILE字句

循环直至条件为真,使用REPEAT和UNTIL字句

在这三种循环中,你都可以使用LEAVE字句来终止循环DELIMITER $$DROP PROCEDURE IF EXISTS simple_loop$$CREATE PROCEDURE simple_loop()BEGIN DECLARE counter INT DEFAULT 0; my_simple_loop: LOOP SET counter = counter 1; IF counter = 10 THEN LEAVE my_simple_loop; END IF; END IF;END LOOP my_simple_loop;select "I can count to 10";END$$ DELIMITER;错误处理

1、如果你认为内嵌的SQL语句会返回空记录,或者你想用游标捕获SELECT语句所返回的记录,那么一个NO FOUND 错误处理可以防止存储过程过早的被终止

2、如果你认为SQL语句可能返回错误(比如违背约束条件),你可以创建一个错误处理来阻止程序终止。这个处理将代替你的默认错误处理并继续程序的执行。

和数据库交互

大多数存储过程包含了各种和数据库表的交互,它们包括四种主要的交互:

1、将一个SQL语句所返回的单个记录放入本地变量中。

2、创建一个“游标”来迭代SQL语句所返回的结果集

3、执行一个SQL语句,将执行后的结果集返回给它的调用程序

4、内嵌一个不反悔结果集的SQL语句,如INSERT,UPDATE, DELETE等

对本地变量使用SELECT INTO

当需要在单个记录数据中获取查询信息,你就可以使用SELECT INTO 语法(无论是使用单个记录,多个记录的混合数据,还是多个表连接)。在这种情况下,你可以再SELECT语句中跟随一个INTO子句,告诉MySQL得到的查询数据返回给谁DELIMITER $$ DROP PROCEDURE IF EXISTS customer_sales $$ CREATE PROCEDURE customer_sales(int_customer_id INT) READS SQL DATA BEGIN DECLARE total_sales NUMERIC(8, 2); SELECT SUM(sale_value) INTO total_sales FROM sales WHERE customer_id = in_customer_id; SELECT CONCAT('Total sales for ', in_customer_id, 'is', 'total_sales'); END; $$

使用游标

SELECT INTO 定义了单记录查询,但是很多应用程序要求查询多记录数据,你可以使用MySQL中的游标来实现这一切,游标允许你将一个或更多的SQL结果集放进存储程序变量中,通常用来执行结果集中各个单记录的处理。DELIMITER $$ DROP PROCEDURE cursor_example() READ SQL DATA BEGIN DECLARE l_employee_id INT; DECLARE l_salary NUMERIC(8, 2); DECLARE l_department_id INT; DECLARE done INT DEFAULT 0; DECLARE curl CURSOR FOR SELECT employee_id, salary, department_id FROM employees; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; OPEN curl; emp_loop : LOOP FETCH curl INTO l_employee_id, l_salary, l_department_id; IF done = 1 THEN LEAVE emp_loop; END IF; END LOOP emp_loop;CLOSE curl;

END;$$bitsCN.com

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值