mysql存储过程编程 全_Mysql存储过程

此文仅作简单的说明

一、简介

我们常用的操作数据库语言后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它。

一个存储过程是一个可编程的函数,它在数据库中创建并保存。它可以有sql语句和一些特殊的控制结构组成。当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有用的。数据库中的存储过程可以看做是对编程中面向对象方法的模拟。它允许控制数据的访问方式。MysqL在5.0以前并不支持存储过程

二、创建存储过程

(一)修改结束符:

因为sql语句以分号";"结束,为了创建存储过程首先要修改一个结束符:

delimiter //

select * from tablename //

上边代码将结束符换成了"//"

(二)创建存储过程:

现在可以创建一个存储过程了:

create procedure finduser (out username varchar(255))

reads sql databegin

select name into username from userinfo where id=1;end

//

procedure是程序步骤的意思,可以理解为存储过程.其中finduser就是这个存储过程的名字

括号中是存储过程的参数,out表示是输出参数,in表示输入参数。MysqL存储过程的参数用在存储过程的定义,共有三种参数类型,IN,OUT,INOUT,形式如:

CREATE PROCEDURE([[IN |OUT |INOUT ] 参数名 数据类形...])

IN 输入参数:表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值

OUT 输出参数:该值可在存储过程内部被改变,并可返回

INOUT 输入输出参数:调用时指定,并且可被改变和返回

username就是参数名 而varchar(255)则是参数类型。参数类型有:int,float,date,varchar(length)

reads sql data 表明这个存储过程下面要用sql读取数据。

begin和end代表开始和结束

(三)调用存储过程:

call finduser(@n)//

select @n//

第一句敲回车执行后,会提示执行成功。第二步执行后或获取到结果。

在这里@后面即一个字符串,表明一个会话变量,在MysqL中会话变量不用声明即可使用,会话变量在整个过程中都

有效,在这里,调用了存储过程,然后把结果存入了会话变量中,然后查询会话变量,即可查询到结果

(四)查询存储过程

1、查询数据库下有哪些存储过程:

select name from MysqL.proc where db=’数据库名’;

select routine_name from information_schema.routines where routine_schema='数据库名';

show procedure status where db='数据库名';

2、查询存储过程详细:

show create procedure  数据库.存储过程名;

(五)修改存储过程

ALTER PROCEDURE

更改用CREATE PROCEDURE 建立的预先指定的存储过程,其不会影响相关存储过程或存储功能。

(六)删除存储过程

删除一个存储过程比较简单,和删除表一样:

DROP PROCEDURE

从MysqL的表格中删除一个或多个存储过程。

总结

以上是编程之家为你收集整理的Mysql存储过程全部内容,希望文章能够帮你解决Mysql存储过程所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

  • 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、付费专栏及课程。

余额充值