An Introduction to Stored Procedures in MySQL 5
https://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843
MySQL 5 introduced a plethora of new features - stored procedures being one of the most significant. In this tutorial, we will focus on what they are, and how they can make your life easier.
If you work with MySQL a lot, you may want to check out the range of MySQL code scripts and plugins on Envato Market.
Introduction
“ A stored routine is a set of SQL statements that can be stored in the server.”
A stored procedure is a method to encapsulate repetitive tasks. They allow for variable declarations, flow control and other useful programming techniques.
The “academic” position on this is quite clear and supports the extensive use of stored procedures. On the other hand, when you consider the opinions of those who work with them day in, day out, you'll notice that reactions vary from complete, unwavering support to utter hate. Keep these in mind.
Pros
- Share logic with other applications. Stored procedures encapsulate functionality; this ensures that data access and manipulation are coherent between different applications.
- Isolate users from data tables. This gives you the ability to grant access to the stored procedures that manipulate the data but not directly to the tables.
- Provide a security mechanism. Considering the prior item, if you can only access the data using the stored procedures defined, no one else can execute a
DELETESQL statement and erase your data. - To improve performance because it reduces network traffic. With a stored procedure, multiple calls can be melded into one.
Cons
- Increased load on the database server -- most of the work is done on the server side, and less on the client side.
- There's a decent learning curve. You'll need to learn the syntax of MySQL statements in order to write stored procedures.
- You are repeating the logic of your application in two different places: your server code and the stored procedures code, making things a bit more difficult to maintain.
- Migrating to a different database management system (DB2, SQL Server, etc) may potentially be more difficult.
The tool that I am working with in this tutorial, MySQL Query Browser, is pretty standard for database interactions. The MySQL command line tool is another excellent choice. I make note of this because the popular phpMyAdmin doesn't support stored procedure execution.
Additionally, I'll be using very rudimentary table structures, strictly to ease the explanation. I'm showing off stored procedures, and they're complex enough without worrying about big tables.
Step 1 - Picking a Delimiter
The delimiter is the character or string of characters that you'll use to tell the mySQL client that you've finished typing in an SQL statement. For ages, the delimiter has always been a semicolon. That, however, causes problems, because, in a stored procedure, one can have many statements, and each must end with a semicolon. In this tutorial I will use “//”
Step 2 - How to Work with a Stored Procedure
Creating a Stored Procedure
|
01
02
03
04
05
06
07
08
09
10
|
DELIMITER //CREATE PROCEDURE `p2` ()LANGUAGE SQLDETERMINISTICSQL SECURITY DEFINERCOMMENT 'A procedure'BEGIN SELECT 'Hello World !';END// |
The first part of the statement creates the procedure. The next clauses defines the optional characteristics of the procedure. Then you have the name and finally the body or routine code.
Stored procedure names are case insensitive, and you cannot create procedures with the same name. Inside a procedure body, you can't put database-manipulation statements.
The four characteristics of a procedure are:
- Language : For portability purposes; the default value is SQL.
- Deterministic : If the procedure always returns the same results, given the same input. This is for replication and logging purposes. The default value is
NOT DETERMINISTIC. - SQL Security : At call time, check privileges of the user.
INVOKERis the user who calls the procedure.DEFINERis the creator of the procedure. The default value isDEFINER. - Comment : For documentation purposes; the default value is
""
Calling a Stored Procedure
To call a procedure, you only need to enter the word CALL, followed by the name of the procedure, and then the parentheses, including all the parameters between them (variables or values). Parentheses are compulsory.
CALL stored_procedure_name (param1, param2, ....) CALL procedure1(10 , 'string parameter' , @parameter_var);
Modify a Stored Procedure
MySQL provides an ALTER PROCEDURE statement to modify a routine, but only allows for the ability to change certain characteristics. If you need to alter the body or the parameters, you must drop and recreate the procedure.
Delete a Stored Procedure
|
1
|
DROP PROCEDURE IF EXISTS p2; |
This is a simple command. The IF EXISTS clause prevents an error in case the procedure does not exist.
Step 3 - Parameters
Let's examine how you can define parameters within a stored procedure.
CREATE PROCEDURE proc1 (): Parameter list is emptyCREATE PROCEDURE proc1 (IN varname DATA-TYPE): One input parameter. The wordINis optional because parameters areIN(input) by default.CREATE PROCEDURE proc1 (OUT varname DATA-TYPE): One output parameter.CREATE PROCEDURE proc1 (INOUT varname DATA-TYPE): One parameter which is both input and output.
Of course, you can define multiple parameters defined with different types.
IN example
|
1
2
3
4
5
6
|
DELIMITER //CREATE PROCEDURE `proc_IN` (IN var1 INT)BEGIN SELECT var1 + 2 AS result;END// |
OUT example
|
1
2
3
4
5
6
|
DELIMITER //CREATE PROCEDURE `proc_OUT` (OUT var1 VARCHAR(100))BEGIN SET var1 = 'This is a test';END // |
INOUT example
|
1
2
3
4
5
6
|
DELIMITER //CREATE PROCEDURE `proc_INOUT` (OUT var1 INT)BEGIN SET var1 = var1 * 2;END // |
Step 4 - Variables
The following step will teach you how to define variables, and store values inside a procedure. You must declare them explicitly at the start of the BEGIN/END block, along with their data types. Once you've declared a variable, you can use it anywhere that you could use a session variable, or literal, or column name.
Declare a variable using the following syntax:
DECLARE varname DATA-TYPE DEFAULT defaultvalue;
Let's declare a few variables:
|
1
2
3
4
5
6
7
|
DECLARE a, b INT DEFAULT 5;DECLARE str VARCHAR(50);DECLARE today TIMESTAMP DEFAULT CURRENT_DATE;DECLARE v1, v2, v3 TINYINT; |
Working with variables
Once the variables have been declared, you can assign them values using the SET or SELECT command:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
|
DELIMITER //CREATE PROCEDURE `var_proc` (IN paramstr VARCHAR(20))BEGIN DECLARE a, b INT DEFAULT 5; DECLARE str VARCHAR(50); DECLARE today TIMESTAMP DEFAULT CURRENT_DATE; DECLARE v1, v2, v3 TINYINT; INSERT INTO table1 VALUES (a); SET str = 'I am a string'; SELECT CONCAT(str,paramstr), today FROM table2 WHERE b >=5; END // |
Step 5 - Flow Control Structures
MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE and REPEAT constructs for flow control within stored programs. We're going to review how to use IF, CASE and WHILE specifically, since they happen to be the most commonly used statements in routines.
IF statement
With the IF statement, we can handle tasks which involves conditions:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
DELIMITER //CREATE PROCEDURE `proc_IF` (IN param1 INT)BEGIN DECLARE variable1 INT; SET variable1 = param1 + 1; IF variable1 = 0 THEN SELECT variable1; END IF; IF param1 = 0 THEN SELECT 'Parameter value = 0'; ELSE SELECT 'Parameter value <> 0'; END IF;END // |
CASE statement
The CASE statement is another way to check conditions and take the appropriate path. It's an excellent way to replace multiple IF statements. The statement can be written in two different ways, providing great flexibility to handle multiple conditions.
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
DELIMITER //CREATE PROCEDURE `proc_CASE` (IN param1 INT)BEGIN DECLARE variable1 INT; SET variable1 = param1 + 1; CASE variable1 WHEN 0 THEN INSERT INTO table1 VALUES (param1); WHEN 1 THEN INSERT INTO table1 VALUES (variable1); ELSE INSERT INTO table1 VALUES (99); END CASE;END // |
or:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
DELIMITER //CREATE PROCEDURE `proc_CASE` (IN param1 INT)BEGIN DECLARE variable1 INT; SET variable1 = param1 + 1; CASE WHEN variable1 = 0 THEN INSERT INTO table1 VALUES (param1); WHEN variable1 = 1 THEN INSERT INTO table1 VALUES (variable1); ELSE INSERT INTO table1 VALUES (99); END CASE;END // |
WHILE statement
There are technically three standard loops: WHILE loops, LOOP loops, and REPEAT loops. You also have the option of creating a loop using the “Darth Vader” of programming techniques: the GOTO statement. Check out this example of a loop in action:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
|
DELIMITER //CREATE PROCEDURE `proc_WHILE` (IN param1 INT)BEGIN DECLARE variable1, variable2 INT; SET variable1 = 0; WHILE variable1 < param1 DO INSERT INTO table1 VALUES (param1); SELECT COUNT(*) INTO variable2 FROM table1; SET variable1 = variable1 + 1; END WHILE;END // |
Step 6 - Cursors
Cursoris used to iterate through a set of rows returned by a query and process each row.
MySQL supports cursor in stored procedures. Here's a summary of the essential syntax to create and use a cursor.
DECLARE cursor-name CURSOR FOR SELECT ...; /*Declare and populate the cursor with a SELECT statement */
DECLARE CONTINUE HANDLER FOR NOT FOUND /*Specify what to do when no more records found*/
OPEN cursor-name; /*Open cursor for use*/
FETCH cursor-name INTO variable [, variable]; /*Assign variables with the current column values*/
CLOSE cursor-name; /*Close cursor after use*/
In this example, we'll perform some simple operations using a cursor:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
DELIMITER //CREATE PROCEDURE `proc_CURSOR` (OUT param1 INT)BEGIN DECLARE a, b, c INT; DECLARE cur1 CURSOR FOR SELECT col1 FROM table1; DECLARE CONTINUE HANDLER FOR NOT FOUND SET b = 1; OPEN cur1; SET b = 0; SET c = 0; WHILE b = 0 DO FETCH cur1 INTO a; IF b = 0 THEN SET c = c + a; END IF; END WHILE; CLOSE cur1; SET param1 = c;END // |
Cursor has three important properties that you need to be familiar with in order to avoid unexpected results:
- Asensitive : Once open, the cursor will not reflect changes in its source tables. In fact, MySQL does not guarantee the cursor will be updated, so you can't rely on it.
- Read Only : Cursors are not updatable.
- Not Scrollable : Cursors can be traversed only in one direction, forward, and you can't skip records from fetching.
Conclusion
In this lesson, we covered the fundamentals of stored procedures and some specific properties pertaining to them. Of course, you should continue your studies in areas like security, SQL statements, and performance before you can master MySQL routines.
You have to evaluate the advantages that stored procedures can potentially bring to your applications, and then make a reasonable implementation that fits your requirements. I generally use procedures; their benefits in terms of security, code maintenance and software design make them worthy of use, in my opinion. Additionally, remember that procedures in MySQL are still a work in progress. You should fully expect improvements, in terms of functionality and performance in the future.
Please don't hesitate to comment and share your ideas and opinions. And have a look at the MySQL code scripts and plugins on Envato Market to see if you find anything to help you there.
An Introduction to Stored Procedures in MySQL 5的更多相关文章
- Snippet: Fetching results after calling stored procedures using MySQL Connector/Python
https://geert.vanderkelen.org/2014/results-after-procedure-call/ Problem Using MySQL Connector/Pytho ...
- Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...
- [MySQL] Stored Procedures 【转载】
Stored routines (procedures and functions) can be particularly useful in certain situations: When mu ...
- Cursors in MySQL Stored Procedures
https://www.sitepoint.com/cursors-mysql-stored-procedures/ After my previous article on Stored Proce ...
- MySQL Error Handling in Stored Procedures 2
Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...
- Why Stored Procedures?
http://www.w3resource.com/mysql/mysql-procedure.php Stored procedures are fast. MySQL server takes s ...
- java调用存储过程(stored procedures)的HelloWorld例子
1.java调用存储过程(stored procedures)的HelloWorld程序 有点数据 库基础的人都知道.存储过程(stored procedures)和java没什么关系.它是一段纯粹的 ...
- Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement
Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...
- Good Practices to Write Stored Procedures in SQL Server
Reference to: http://www.c-sharpcorner.com/UploadFile/skumaar_mca/good-practices-to-write-the-stored ...
随机推荐
- Mysql跨表更新 多表update sql语句总结
Mysql跨表更新一直是大家所关心的话题,本文介绍mysql多表 update在实践中几种不同的写法 假定我们有两张表,一张表为Product表存放产品信息,其中有产品价格列Price:另外一张表是P ...
- javascript事件监听与事件委托
事件监听与事件委托 在js中,常用到element.addEventListener()来进行事件的监听.但是当页面中存在大量需要绑定事件的元素时,这种方式可能会带来性能影响.此时,我们可以用事件 ...
- 开发人员应该对IIS理论层的知识了解的多一些~第四讲 HttpModule中的几大事件
返回目录 本文主要介绍HttpModule,它在一个网页请求过程中是一个怎样的过程是我们要知道的,在网页加载过程中HttpModule在何时被执行也是我们要知道的,以及,HttpModule在网页请求 ...
- IOS笔记045-UIDatePicker和UIPickerView
这是两种可以上下滚动的控件. 这是UIDatePicker,可以显示日期和时间. 这个是UIPickerView,显示类似几个选择项的界面. 注意点:PickerView的高度不能改,默认162,Pi ...
- 执行SSIS Package的三种方式
1,使用SQL Server job 创建一个job用于执行package,可以制定一个schedule来定时执行job,也可以使用TSql 代码来执行job EXEC msdb.dbo.sp_sta ...
- Netgen mesh library : nglib
Netgen mesh library : nglib eryar@163.com 摘要Abstract:本文主是对Netgen的库nglib的用法进行介绍.主要参考资料是Netgen用户指南.最后给 ...
- JAVA_Collection容器
因为项目的需要,今天抽时间把JAVA中的容器复习了一下,为了以后的不时之需,现在把它记下来. 容器有其名,知其意,用来盛放数据的集合,JAVA中为我们提供了三种容器类:set.list.map,三种容 ...
- Chrome 控制台指南
转自:http://blog.jobbole.com/76985/ Chrome的开发者工具已经强大到没朋友的地步了,特别是其功能丰富界面友好的console,使用得当可以有如下功效: 更高「逼格」更 ...
- Spring MVC 学习总结(四)——视图与综合示例
一.表单标签库 1.1.简介 从Spring2.0起就提供了一组全面的自动数据绑定标签来处理表单元素.生成的标签兼容HTML 4.01与XHTML 1.0.表单标签库中包含了可以用在JSP页面中渲染H ...
- Admin Panel – 非常漂亮的后台管理系统模板
网站或者应用系统的管理后台的设计虽然不像前台界面那样要求设计精美,但是也需要有清晰的管理模块划分,下面分享的这个后台管理模板的设计非常漂亮,特别是导航部分,头部还有未读的短消息和提醒的条数显示.赶紧下 ...