Art AboutFAQWrite for usContact InfoRSS Feed
Search A/R/T:

Stored Procedure Programming for MySQL5 - Part 1

by Ligaya Turmelle — Page 1 of 5
Click to rate:    
216 votes / avg. rating 36.33%

With the release of MySQL5 a bold new world opens up to the PHP developer... the world of a database programmer. In this world the interaction with the data can be done right where the data is located - not in a script that is far far away in a distant server. In this article we will be taking you on a journey that will introduce you to MySQL's stored procedures.


 

We will go over some basic concepts involved, some background information you may or may not know and then break a couple of examples down line by line to help you understand what is happening. I won't bore you with basic programming information - that you should know already. However I will be going over what differentiates stored procedure programming from PHP programming. So sit back and relax as we go for a bit of a ride. And please remember to place your trays in the full upright position and to keep your hands and feet inside the vehicle at all times.

Basic Concepts

Basically, a stored procedure is a “routine” written in SQL statements that are saved on the database server. For example you can create a stored procedure specificaly for your application. A payroll application may have a stored procedure that may (for example) read the employee worktime table and calculate the gross and net pay, then insert that information into the accounting tables, and finally calculate an employee's vacation time and update the employee table to reflect it. Without a stored procedure this would require a number of database calls with data going to and from the database and your program. With a stored procedure - it can all be done at once only returning to the application when it is fully completed.

Stored procedures are useful for reducing the network traffic between the client and server. This is done by only passing the data needed to run the stored procedure and the results of the stored procedure, not all the individual SQL statements and their results needed in between. Of course there is a trade off for this - increased load on the database server because more work is being done on there.

There are a few situations where stored procedures in particular excel.

  • When you have multiple applications in different languages on different platforms all trying to interact with the same data. Stored procedures can form a unified API for accessing all the data.
  • When data security is absolutely essential. If the only way you can get to the data is through specific stored procedures, you can easily control it and log all interactions.
Note:MySQL follows the SQL::2003 syntax for stored procedures. This syntax is also followed by IBM's DB2.

 


 Tags:  , , , , ,
 Add tag(s) (comma separated):
[Tags beta] — [Add New][Help]  

Tags Help

Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.

To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.

New Comment
This form allows you to type in a new comment. Keep in mind the following:
  • The system accepts input in plain text format. Newlines will be converted to the HTML equivalent, and the system will try to catch most URLs and make them clickable.
  • Your e-mail address will never be displayed. We will use it only to notify you when new comments are posted to this page.
  • As a rule, we do not delete comments unless they are offensive, racist, spam or otherwise inappropriate.
  • Bold fields are required
Your Name:
Your e-mail:
Type this number:
Subject:
Comment:
Comments   New Comment
Re: Stored Procedure Programming for MySQL5 - Part 1 (#208)
By Kasper A. Svendsen on 2006-07-12 12:34:04

Very nice article, good with the examples and so on! :=)

[Reply to this]

Re: Re: Stored Procedure Programming for MySQL5 - Part 1 (#341)
By khalid husain on 2006-10-09 04:10:38

kindly give me example of stored procedure

thanks

[Reply to this]

Re: Re: Stored Procedure Programming for MySQL5 - Part 1 (#524)
By Abhinav on 2007-04-23 02:13:30

n i c e

A r T i c L e

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#209)
By jcerdan on 2006-07-12 14:14:40

Users should be careful as the exact command is:

GRANT CREATE ROUTINE ON 'database'.* TO 'user'@'host' IDENTIFIED BY 'password';

Don't forget the .* after the database name.

[Reply to this]

Print (#211)
By Sasa on 2006-07-13 13:57:50

Where is the print version?

[Reply to this]

Re: Print (#222)
By Michael on 2006-07-18 02:40:24

Yep, a print version would be very nice.

[Reply to this]

Re: Re: Print (#378)
By userbiasa on 2006-12-14 12:26:32

Yes, I think putting printed version is a fair play after users browsing each pages. You can put it on the latest installment of these pages.

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#257)
By Jad on 2006-07-29 06:50:46

mysql> CALL RateOfArticle("Well Done", @a);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @a;
-------------------------------
| @a |
-------------------------------
| Thank you Ligaya. |
-------------------------------
1 row IN SET (0.00 sec)

[Reply to this]

Very Very Thnx (#289)
By Sai Suman on 2006-08-17 02:52:56

Really Helps a lot for new Commers.. Thnx for u'r Support can u give u'r personal E-mail id so that i can clear my doubts in future

mysql> select @a;
---------------------
| @a |
---------------------
| Today is a Good Day |
---------------------
1 row in set (0.00 sec)

[Reply to this]

Re: Can you explani in brief (#558)
By priyank sharma on 2007-06-23 02:27:27

hi realy it is good blog but there is little bit of confusion for new commers . can you explain it(Stored Procedure Programming) in brief....

Many Thanks

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#476)
By Asmita on 2007-01-19 06:00:12

this page gives good and proper tutorial about stored procedure.but i think 2 examples are not sufficient and some more complex and realist examples should be added if possible

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#589)
By d on 2007-08-31 22:48:20

what web scripting languages support mysql5 stored procedures?

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#616)
By Patrick on 2007-12-08 08:57:17

Thank You!

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#623)
By php on 2008-01-13 17:54:50

how to use php to call stored procs in mysql 5? give example plz

[Reply to this]

Re: Re: Stored Procedure Programming for MySQL5 - Part 1 (#628)
By Sudhir Kumar Panda on 2008-03-14 07:11:47

Plz give me the reply

[Reply to this]

Re: Stored Procedure Programming for MySQL5 - Part 1 (#624)
By joseph mendez on 2008-02-13 20:43:57

i got error : ERROR 1064(42000): it says "You have an error in your syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PROCEDURE ....

[Reply to this]

Index