Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Create Index in MySQL: MySQL Index Tutorial [2024]

Updated on 13 November, 2024

13.64K+ views
7 min read

A Data Scientist or a programmer will have to work with large quantities of data and needs to be able to deal with it efficiently for faster execution. They also need to know how the data is organised and the fastest methods to easily access the particular data needed.

Since MySQL is a relational database management system that has applications in various sectors such as web databases, e-commerce applications, social media etc. data scientists need to know all the techniques related to MySQL, one of which is indexes. The article describes how to create Index in MySQL.

Check out our free courses to get an edge over the competition.

What is an Index?

In MySQL, Indexes are used to sort data in an organised sequential way and retrieve the data efficiently and quickly. They are created by using one or more columns that will be used to filter data. Indexes are a type of table that keep a primary or index field and a pointer to each record in the main table.

For a better understanding, a book index can be used as an example. Instead of looking for a page by going through all the pages in a book, the book index can be checked to find the page one is looking for.

In most database systems, high and fast performance is needed. Many businesses invest huge sums of money on hardware for faster data retrievals and manipulations. But by optimizing the database, the costs can be reduced.

Response time is usually longer because records are stored randomly, and search queries have to find the needed data by looping through the entire stored records. Indexes make this search easier and faster.

Check out upGrad’s Advanced Certification in Cyber Security 

Read: Rename Column name in SQL

Create Index

You can create index in MySQL on a table by using the following basic syntax.

CREATE INDEX index_name table_name (column_name);

You can also create a unique index on a table. This means that the name is unique, and any two rows cannot have the same name. The following syntax can be used for this.

CREATE UNIQUE INDEX index_name ON table_name (column_name);

If a secondary index for a column is created, MySQL stores the values in a separate data structure such as B-Tree and Hash. In MySQL, if the columns are string columns, you can create an index for the leading part of the column values of the string column using the syntax:

column_name(length)

To create the column prefix key part when the table is created, the following syntax can be used:

CREATE TABLE table_name(

column_list,

INDEX(column_name(length))

);

And to add an index to an existing table:

CREATE INDEX index_name ON table_name (column_name(length));

The length here is the number of characters if it is a non-binary string type like CHAR, VARCHAR and TEXT and the number of bytes if it is binary string type like BINARY, VARBINARY and BLOB. You can also create column prefix key parts for CHAR, VARCHAR, BINARY, and VARBINARY columns in MySQL. It is necessary to specify the column prefix key parts if you have created indexes for BLOB and TEXT columns.

Check out upGrad’s Advanced Certification in Cloud Computing 

In case of FULLTEXT indexes that are only supported for InnoDB and MyISAM tables, they can include only CHAR, VARCHAR and TEXT columns. It does not support column prefix indexing and if any prefix length is specified, it is ignored.

Must Read: SQL Project Ideas

Altering tables to add or drop Index

To add indexes to a table, there are four types of statements that can be used:

1. To add a primary key:

ALTER TABLE table_name ADD PRIMARY KEY (column_list);

The indexed values here should be unique and cannot be NULL.

2. For an index with unique values:

ALTER TABLE table_name ADD UNIQUE index_name (column_list);

The values should be unique, except for NULL which can appear multiple times.

3. To add an ordinary index:

ALTER TABLE table_name ADD INDEX index_name (column_list);

Here the values can appear more than once.

4. To create special FULLTEXT index:

ALTER TABLE table_name ADD FULLTEXT index_name (column_list);

The FULLTEXT index can be used for text searching purposes.

In existing tables

To add index in any existing table, the following syntax can be used:

ALTER TABLE table_name ADD INDEX (index_name);

To delete an index in a table, the DROP INDEX statement is used:

ALTER TABLE table_name DROP INDEX (index_name);

Display Index Information

To list all the indexes associated with any table, the SHOW INDEX command is used.

SHOW INDEX FROM table_name\G

The ‘\G’ is used to create the list in a vertical format, this avoids the long line wraparound.

Also Read: SQL Interview Questions & Answers

Descending Indexes

InnoDB can store the entries in descending order when the index is a descending index. So, when the descending order is requested in the query, the optimizer chooses this index. This is more efficient for queries with ORDER BY clauses. These are only supported by the InnoDB storage engine.

The syntax for creating or adding descending indexes is like alter or create syntaxes used above.

 ALTER TABLE table_name ADD index_name (col1_name desc, col2_name asc);

CREATE INDEX index_name ON table_name (col1_name desc, col2_name asc);

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

Invisible Indexes

To mark indexes as unavailable for the query optimizer, invisible indexes can be used. MySQL updates invisible indexes when the data in columns associated with the index changes. Indexes are visible by default and to make them invisible, you have to explicitly declare the visibility at the time of creation or by using the ALTER TABLE command.

To create an invisible index, the following syntax is used:

CREATE INDEX index_name ON table_name (c1, c2…) INVISIBLE;

To change the visibility of existing indexes, the following syntax in used:

ALTER TABLE table_name ALTER INDEX index_name [VISIBLE | INVISIBLE];

Enrol in Online Software Development Courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Conclusion

The performance of MySQL search queries can be improved by using Indexes. These can be defined when a table is created or can be added later if the table is already created.

This article gave a brief description of what an index is and why it is used. The next section explains how to create index in MySQL, how it can be added to an existing table and how to create a unique index.

The different commands to alter tables and create or drop an index were also described along with displaying index information. Descending and invisible indexes were also briefly explained. 

All in all, learning and getting hands-on practice on all the databases mentioned in this article will add to your experience. Go through the course overview, learning, and job opportunities involved with it—platforms like upGrad offer power-packed courses designed by the highly reputed institutes like IIIT-B in Executive PG Program Full Stack Development.

Frequently Asked Questions (FAQs)

1. How are clustered and non-clustered indexes defined in My SQL?

MySQL has mainly two types of indexes: clustered index and a non-clustered index. Although both are involved primarily in data sorting, their approach is quite different. A clustered index is created when a primary key is clarified. A clustered index is unique and limited to one per table. This is because clustered indexes can only sort the data in ascending order. Hence, the rows in the table will be presented in one way only. At the same time, a non-clustered index sorts the data through specific pointers in a separate field distinct from the table. They are also not limited to one index per table, giving greater access to the data.

2. How is indexing different from hashing?

Indexing and hashing have widely different functions, although applied to the same data set. They mainly differ in their approach to locating data. Indexing involves reducing the query process. It helps procure desired data points from an extensive data set using different types of indexes. On the other hand, hashing doesn’t require an index structure to procure data. It uses mathematical functions with a hash prefix to pinpoint data in the sheet. In humongous data sheets where indexing might be tedious, hashing has a faster process. It assigns a short hash key to each data point and locates them instantly.

3. What are the limitations of indexing in MySQL?

Indexes reduce the hassle required to navigate databases and to locate specific data. They assist in browsing through large data sheets, resulting in quicker identification, increased time efficiency, and optimized queries. However, it has its limitations. When it comes to very large datasheets, indexing can become complicated and take up more memory space than a relatively more minor datasheet. Hence, as the data volume increases, maintaining and keeping track of the indexes becomes more complex. In case of any alterations within the sheet, the index key also has to be modified. Nevertheless, the convenience that indexing provides overtakes its drawbacks.

RELATED PROGRAMS