Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql table: index vs primary

Tags:

mysql

i am a newbie and am a bit concerned about the efficiency of mysql table. what are the pros and cons of making a field in mysql table as

  1. index
  2. primary

what is the followed practise in deciding on the attributes of a field in a mysql table? please share your insights, thanks.

like image 883
whatf Avatar asked Feb 03 '26 21:02

whatf


1 Answers

You can only have one primary key in a table. It should be a column that uniquely identifies each record in your table. A primary key is not a requirement; for example, you may have a table to implement a many-to-many relationship and a unique id is not needed for those records.

Indexes should be used whenever you must query a table and specify some criteria to filter records based on a column's value (e.g. WHERE clause). There is a lot to learn about indexes, for example: when to create an index on one column vs. multiple columns in the same index. Read the documentation on the MySQL website about indexes to learn more.

like image 119
AJ. Avatar answered Feb 05 '26 11:02

AJ.