3

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.

1 Answer 1

1

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.

2
  • 1
    thanks AJ for your answer, i did not intend to ask the definition of primary key or index. what i actually wanted to know was what are the best practises followed while deciding the primary key and/or index. some practical tips from some experienced user. hope you understand now.
    – whatf
    Apr 16, 2011 at 17:41
  • @gamebit07 – I believe my answer provides more than just a definition. “A primary key is not a requirement…” would be considered advice/best practices, not a definition. Mentioning single column vs. multi-column indexes is advice/best practices, not a definition. If you’re looking for a definition that would apply to all situations, it’s going to be VERY high-level and won’t solve your problem. Reason: every database will be different. If you were to provide an example of a database schema, I would be happy to give more specific advice on optimizing indexes for that specific scenario.
    – AJ.
    Apr 16, 2011 at 17:59

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.