8

I have a table in mySQL where the 'id' column is the PRIMARY KEY:

CREATE TABLE `USERS` (
  `ID` mediumint(9) NOT NULL auto_increment,
  .....
  PRIMARY KEY  (`ID`),
  KEY `id_index` (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=267 DEFAULT CHARSET=latin1;

I've also added an index as follows:

CREATE INDEX id_index ON USERS (id);

Did I need to do this? Or is the primary key automatically indexed?

The end aim is here is to speed up queries which join on the id column of table USERS.

Thanks

1

3 Answers 3

18

No, you don't need do this.

Primary key is automatically indexed. What you need is to index column that is foreign key in other table.

1
4

You don't need to add additional index to PK.

Using Primary Keys

3

By default Primary key is working like INDEX KEY. you need not to create this to index key.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.