New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

MySQL Essential Training
Illustration by Don Barnett

Creating a stored procedure


From:

MySQL Essential Training

with Bill Weinman

Video: Creating a stored procedure

A stored procedure is defined with the create procedure statement. And I changed delimiter to something other than a semicolon like that.
Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
MySQL Essential Training
4h 24m Beginner May 14, 2014

Viewers: in countries Watching now:

MySQL is by far the most popular database management system for small- to medium-sized web projects. In this course, Bill Weinman provides clear, concise tutorials that guide you through creating and maintaining a MySQL database of your own. Bill explores the basic syntax, using SQL statements to create, insert, update, and delete data from your tables. He also covers creating a new database from scratch, as well as data types, transactions, subselects, views, and stored routines. Plus, learn about the multi-platform PHP PDO interface that will help you connect your database to web applications.

Topics include:
  • Writing queries
  • Creating and updating databases and tables
  • Using MySQL built-in functions
  • Sorting and filtering data
  • Updating tables with triggers
  • Working with subselects and views
  • Creating and using a stored function
Subjects:
Developer Databases
Software:
MySQL
Author:
Bill Weinman

Creating a stored procedure

A stored procedure is defined with the create procedure statement. For this lesson I'm going to use the album database. And we're going to start by creating a simple procedure. Now, a procedure may have multiple lines in it. And these multiple lines may be delimited with semicolons. And in order for that entire block of. Text. That entire block of code to be sent to the server all at once. We need to actually change the delimiter for a short time, temporarily, to something else, so that that entire block can be sent to the server as a unit.

So that looks like this. I use the DELIMITER command. And I changed delimiter to something other than a semicolon like that. I'll change it back when we're done and then, I type CREATE PROCEDURE and I name the procedure and I have BEGIN and END and, after the end, I put in that double-slash and then, this entire block along with the statements. I'm just going to put in a SELECT statement here for now. That entire block from create procedure all the way down to the end all gets sent to the server as one unit, and that's important.

If it were delimited with the semicolon, the client in this case, SID, would send that partial command to the server and it would be a syntax error and it wouldn't work. And so now I set my delimiter back. To a semicolon. And I can say CALL list albums, like that. And when I press Go, my procedure is defined, and my procedure is tested and it works. And so now all of this, can be deleted and CALL list albums will give us that exact same result.

So the delimiter is a MySQL specific thing. In fact it's implemented in the client and so I had to implement it in SID myself in PHP. But it works exactly the same way in the MySQL command line client, and this is just an example of a very simple procedure, so that we can test this process. Now let's do something a little bit more substantial I'm going to come out here to the exercise file this is the chapter nine. Exercise file and here under O3 stored procedures I'm going to grab this second one here.

I'm going to start with this DROP PROCEDURE IF EXISTS and, I'm going to come all the way down here to CALL list album. So I'm going to grab all of that, copy and paste it, and let's just take a look at it here in the text editor you can see this is. Just a more substantial SELECT statement. It's a JOIN statement and you'll notice that it also uses this track len function that we defined in the last lesson. So, that's a stored function that's being called from a stored procedure. You will also notice that this procedure has a parameter it's called a and it's of type VARCHAR and that is used here in the where clause so it's WHERE artist LIKE a and so we are going to pass it some thing when we call that we are going to pass it a string.

That can be used in that like clause. So I'll go ahead and I'll press Go, and we get this, all the tracks where the artist is like hendrix. Right there the way that we called this. So again it's fairly simple, you can do much more complex stuff. In a stored procedure. But I want to just show you one more example with an output parameter. And that is this next one here. Here we have a procedure with an input parameter, and they default to input.

I could have typed IN like that, but it's just not necessary because it defaults to that. And an output parameter called d, so in is a and out is d. And here I have a simple select. I'm selecting all the tracks and I'm actually summing the duration of the tracks. And I'm putting that result INTO the d variable. So I say, SELECT track length, the sum of duration INTO d. And that puts it in the d variable. And it's where album id IN.

And I have a subselect with the artist LIKE a. And so we call it with a set to the hendrix with the wildcards and a global variable with an @ sign, and a global variable with an sign and that global variable will be filled with that result from d. So the procedure stores the output value in this dur variable. By storing it in d inside of the procedure. And so, I'm just going to grab all of this from the DROP PROCEDURE IF EXISTS all the way down to the SELECT and paste this in and press Go.

And we get that result. And again, all of this is stored now on the server. So, I can just CALL duration with these parameters. Instead of hendrix, I could say Winter, for Johnny Winter, and I'll get a different result. And that's calling this function that's actually stored in the server. Stored functions and procedures are deleted with the drop function and drop procedures. So I can say DROP FUNCTION, track len.

And DROP PROCEDURE. And I've got two of them. Total duration and list albums. And I call that. And that drops all the functions and procedures that we've defined in this lesson. Leaving the data in the database intact. So stored procedures are used in a statement context whereas stored functions are used in an expression context. This is an important distinction. I suggest you experiment a lot with both functions and procedures to get a good feel for how and where you will use them in your own code.

There are currently no FAQs about MySQL Essential Training.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed MySQL Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.