Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
MySQL Admin Cookbook LITE: Replication and Indexing

You're reading from  MySQL Admin Cookbook LITE: Replication and Indexing

Product type Book
Published in May 2011
Publisher
ISBN-13 9781849516143
Pages 104 pages
Edition 1st Edition
Languages

Adding indexes to tables

Over time, requirements for a software product usually change and affect the underlying database as well. Often the need for new types of queries arises, which makes it necessary to add one or more new indexes to perform these new queries fast enough.

In this recipe, we will add two new indexes to an existing table called books in the library schema. One will cover the author column, the other the title column. The schema and table can be created like this:

mysql> CREATE DATABASE library;
mysql> USE library;
mysql> CREATE TABLE books (
isbn char(13) NOT NULL,
author varchar(64) default NULL,
title varchar(64) NOT NULL,
PRIMARY KEY (isbn)
) ENGINE=InnoDB;

Getting ready

Connect to the database server with your administrative account.

How to do it...

  1. Change the default database to library:
    USE library;
    
  2. Create both indexes in one go using the following command:
    ALTER TABLE books ADD INDEX IDX_author(author), ADD INDEX IDX_title(title);
    

How it works...

The ALTER...

lock icon The rest of the chapter is locked
arrow left Previous Chapter
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}