About primary keys and auto increment columns
Ideally, every table should have a Primary Key. It's very important that a table has a Primary Key. There are two good reasons why a table should have a Primary Key. First, a Primary Key uniquely identifies each record in a table so it helps to ensure against redundant data in that table. This also means that we can "point" to a specific record in the table. Second, it is also the instrument used to establish a relationship between tables. This will be of importance when you want to retrieve data from multiple tables in a query.Some guidelines for establishing a Primary Key:
- It’s value must be unique.
- It can never be null.
- It must directly identify each value of the remaining fields in a given record of a table.
- It should comprise the minimum number of fields to guarantee uniqueness.
A Primary Key may consist of multiple fields.
A lot of database developers add an identity or auto column to each of their tables to ensure uniqueness. This may sound like a good idea but, if the rows in a table are naturally unique, then the use of an identity column is not recommended and not needed. Using an (unnecessairy) auto increment or identity column can reduce the performance of your database because it may require the use of extra joins, which requires more I/O to read your table. Advantages of using identity columns are:
- Uniqueness is always guaranteed.
- This is a simple and flexible implementation.
Resources
Learn- DeZign for Databases: Learn more about DeZign for Databases.
- Database normalization: Learn how to normalize a data model (1NF, 2NF, 3NF).
- Getting started with DeZign for Databases: Start making a data model directly.
- Many-to-many relationships
- Build your next data model with DeZign for Databases trial software, available for download directly from Datanamic's download section.