MySQL Essential Training
Illustration by Don Barnett

Creating indexes


MySQL Essential Training

with Bill Weinman

Video: Creating indexes

An index is a data structure that Internally, the data base organizes its indexes as B-trees.
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

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course 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
Bill Weinman

Creating indexes

An index is a data structure that optimizes searches, sequential access, insertions, and deletions. Like many database systems MySQL uses indexes to improve performance, and to support features that require ordered data. An index keeps a logical order to a set of data. This is a cornerstone of relational database technology. If I want to find a particular row in a table, access to a fast lookup of a unique index key is going to make that very easy to do.

Internally, the data base organizes its indexes as B-trees. Most data base indexes, including those used by My SQL, use a form of B-tree to organize the data. As an example of how this works, here's a simple B-tree for looking up letters of an alphabet. Without a B-tree, if I just had a list of letters and I wanted to find the letter u, I would have to start at one end of the list, compare the first letter to the letter u. And then if it didn't match I would look at the next letter and compare it to the letter u.

And on and on until I find the letter I'm looking for. This could take up to 26 steps, assuming equal distribution of the letters it would average 13 steps. With a B-tree index, I start with the node at the top of the tree, and compare it to the letter u. If the letter u is less than the node I continue on the left side of the tree. If it's greater than the node, I continue to the right side of the tree. And with a B-Tree, I have a maximum of four steps before I find the letter I'm looking for. This is a very efficient structure for searching, as well for inserting and deleting nodes.

So, let's open up SID, and see how indexes are implemented in MySQL. We'll use the scratch database for this exercise. And I'm going to come over here to my text editor and from the chapter three exercise file, I'm going to copy and paste, you see this around line 43. I'm just going to copy and paste this whole from DROP TABLE all the way to SHOW INDEXES. And paste that into SID here. You notice I start with a DROP TABLE, that's because as we do this exercise over and over, and change a little thing here and there, I want to be able to create the table over and over again, and in order to do that I need to drop the table first.

The table is very simple, it has three columns, an INTEGER and two VARCHARs. And then I insert three rows into it. I select DESCRIBE the table and use this SHOW INDEXES statement to look at the indexes. Of course, at first we don't have any. So when I press Go, you'll see we don't actually have any indexes down there. So let's go ahead and add an index. I'm going to come up here after this b column and I need to put in a comma because these commas are separators in the create table syntax.

And then I'll type the keyword index. And in parentheses, I give it the name of the column that I want to index. And again, there's no comma after this one because it's the last item in the list. Be careful of those comma's, they will give you syntax errors. As a matter of fact, I'm just going to go ahead and put the comma in here so you see what happens. I press go, and you see we get a syntax error, and then everything else in the script doesn't work. So I need to take that comma out. And likewise if I forget to add the comma before it I'll get the same result.

It's just a different error message but it's the same result. The commas need to be exactly correct in order for this to work. And there it is. And you notice that now our last statement here, which is the SHOW INDEXES FROM test is now giving us a result. And so. We have one indexing test. It has a key name of a and a column name of a. It's a non-unique index and so, we have some other information there about it. You'll notice also, in the describe statement.

Now in that result, under key for column a, we have a multiple indication, which means that this allows multiple keys in this index. So we do have an index there. You can also create an index that operates on a combination of columns. So over here I can say a comma b and press Go. And now you notice it still just shows in the describe statement result the one multiple key index there.

But it shows two different indexes in the show index statement. And so this is an implementation detail. What we asked for, was one index that operates on both the a column and the b column, so we sort first on a. And within the a's, there would be the b's in order as well. But how that's implemented behind the scenes is with two separate axis and that's all that that means. I can name my index, and if I give it a name here, I just call this index ab like that.

And press Go. Now you notice that the key name is ab, and that's what I would use if I want to delete the index. So if I come down here and use the drop statement. DROP INDEX ab ON test and then I'll copy these two after it so we have another describe and another SHOW INDEXES after that and you can see what happens. So there is our describe and our show index is that has the index. And then query number nine, I drop the index.

And now you notice that our describe does not have the key, and our show indexes no longer has the indexes. It's also worth noting here that this SHOW INDEXES, you can just say SHOW INDEX and get exactly the same result. So there's that. My SQL has a few aliases like that just to make things convenient for you. So creating indexes is an important part of creating a table. There are other ways to create indexes, that also work with constraints, and I'll cover that in the lesson on constraints later in this chapter.

For now, we need to drop this table one more time to return our database to its original condition for the rest of the course.

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.

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.

* Estimated file size

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


You have completed MySQL Essential Training.

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

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 ?

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 page and choose Site preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about 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

Sign up and receive emails about 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.