Possible Duplicate:
Should each and every table have a primary key?
If you never refer to the ID anywhere is it necessary to include one? Does a table need an ID or primary key?
Possible Duplicate:
Should each and every table have a primary key?
If you never refer to the ID anywhere is it necessary to include one? Does a table need an ID or primary key?
No you do not need a primary key to make a table work in MySQL. That said, a primary key allows for a unique value to refer to a row in a table from another table, or in any code using the table.
You do need a primary key to make a table work well in MySQL though. Indexes (which the primary key is one of) allow MySQL to search through small, highly optimized subsets of the table to process relationships and searches. In general, any fields that you use in a WHERE
clause or use to link two tables together should be indexed.
Actually, InnoDB uses its own row id as PK for the table in case you didn't create one, so it can use it for indexing. And that has some negative effects on performance.
See a very good explanation here: http://blog.johnjosephbachir.org/2006/10/22/everything-you-need-to-know-about-designing-mysql-innodb-primary-keys/
To sum it up, there are 3 rules:
As a side note: some SQL editors and tools may have issues if there is no PK on a table.
When you are manually editing result sets or table data in such a tool, the tool runs an UPDATE
command.
In case there is no unique key, several identical records may be inserted, and then there is no way to update only one/some of them. In an SQL editor you can manually edit one of those records, but when the update command is sent to the mysql - it will either fail, or update all identical records instead of that one record.
By default MySQL (InnoDB engine) uses primary key to determine the order in which the data is physically stored in the main data file. If there is no primary key MySQL will automatically add a hidden AUTOINCREMENT column to act as pkey. This might cause performance issues because during inserting the unique autoincrement value acts like a global lock for all inserts. Also primary keys are used to associate all of a table’s indexes with the main data file. So the primary key is replicated in every row of every index.
ID is not a requirement, and neither is primary key, but it is a fundamental concept of the relational database model. You wouldn't usually want to try and use name as a key, for example.
also found : ID fields in SQL tables: rule or law?
Its not a REQUIREMENT, but if you may ever in the future reference any data in this table, its a very good idea to have one as a primary key.
By "ID", I think you mean "identifier" - something that uniquely identifies a row in a database table. They are also called keys, which is the more concise technical term used in database design.
Implementing keys achieves at least two important things: keys prevent redundant and therefore potentially anomalous data from getting into your table; keys ensure that consumers of the data can accurately identify, use and update the information in the table. For these reasons, yes, you ought to create keys in your tables.