1

In a MySQL database, a table has been created as follows:

CREATE TABLE IF NOT EXISTS `sub` (
`s_id` int(3) NOT NULL AUTO_INCREMENT COMMENT 'Standard id',
`std` int(10) NOT NULL COMMENT 'Standard',
`sub_nm` varchar(25) NOT NULL COMMENT 'Subject Name',
 PRIMARY KEY (`s_id`),
 KEY `sub_nm` (`sub_nm`),
 KEY `sub_nm_2` (`sub_nm`),
 KEY `sub_nm_3` (`sub_nm`)
 )    ENGINE=InnoDB  DEFAULT CHARSET=latin1 COMMENT='All Subjects with
     corresponding standerds.' AUTO_INCREMENT=21 ;

My question is, what is meant by the last three KEY values? I mean: KEY sub_nm (sub_nm), KEY sub_nm_2 (sub_nm), and KEY sub_nm_3 (sub_nm)

3 Answers 3

1

With this instruction KEY sub_nm (sub_nm), KEY sub_nm_2 (sub_nm), KEY sub_nm_3 (sub_nm) you create 3 indexes for sub_nm column named sun_nm, sub_nm_2 and sub_nm_3

Sign up to request clarification or add additional context in comments.

2 Comments

what is the benefit of multiple index? @Taras Soroka
General, with multiple indexes (for example ... KEY ind1 (column_1), KEY ind2 (column2), ..... ) you can index combinations of column values and speed up queries such as SELECT * from TABLE WHERE column1=X AND column_2=Y but in your case I really do not know what is mean of creating 3 indexes for 1 field.
1

KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.

For more

Comments

0

Previous answers have already addressed your question that KEY and INDEX keywords are synonymous. In addition to that, here are a couple of examples.

Create an index by using INDEX or KEY keyword during table creation

CREATE TABLE IF NOT EXISTS `sub` (
  `s_id` int(3) NOT NULL AUTO_INCREMENT COMMENT 'Standard id',
  `std` int(10) NOT NULL COMMENT 'Standard',
  `sub_nm` varchar(25) NOT NULL COMMENT 'Subject Name',
   PRIMARY KEY (`s_id`),
   INDEX `sub_nm` (`sub_nm`),
   INDEX `sub_nm_2` (`sub_nm`),
   INDEX `sub_nm_3` (`sub_nm`)
 );

It is not wise to create 3 indexes on the same field. One index is enough on sub_nm.

Create an index after table creation

CREATE TABLE IF NOT EXISTS `sub` (
  `s_id` int(3) NOT NULL AUTO_INCREMENT COMMENT 'Standard id',
  `std` int(10) NOT NULL COMMENT 'Standard',
  `sub_nm` varchar(25) NOT NULL COMMENT 'Subject Name',
   PRIMARY KEY (`s_id`)
 );

 create index sub_nm on `sub`(sub_nm);
 -- create key sub_nm2 on `sub`(sub_nm); WILL ERROR OUT
 alter table `sub` add index sub_nm3 (sub_nm); 
 alter table `sub` add key sub_nm4 (sub_nm); 

Notice the different ways one can create index on the table. I generally use index name like this: idx_tablename_fieldname (e.g. idx_sub_sub_nm).

Index on sub_nm may improve performance of queries that utilize sub_nm in filtering, sorting and grouping. EXPLAIN helps in identifying if the database believes an index will be used. If database doesn't use a query that you very strongly believe it should, index hints can be provided in the query like this:

select s_id from sub use index (idx_sub_sub_nm) where sub_nm = 'test';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.