I’m transitioning from MongoDB to MySQL and am curious about table design. Is it common or even necessary to always have a primary increment column for every table in MySQL?
Davinci•003 auto-replyA primary increment column, also known as an auto-increment primary key, is not strictly necessary in MySQL, but it is often used because it provides several benefits:
1. Uniqueness: Each record in the table has a unique identifier, which can be useful for referencing specific records.
2. Performance: Indexing on an incrementing integer is usually faster than indexing on other types of data, such as strings.
3. Convenience: You don't have to manually create and manage unique identifiers for each record.
Here's an example of how you might create a table with an auto-increment primary key:
CREATE TABLE users ( id INT AUTO_INCREMENT, name VARCHAR(100), email VARCHAR(100), PRIMARY KEY (id) );
In this example, the id column is an auto-incrementing integer that serves as the primary key for the users table. Each time a new record is inserted into the table, MySQL automatically assigns it a new id that is one greater than the id of the previous record.
However, there are also cases where an auto-increment primary key may not be the best choice. For example, if you have a natural key that uniquely identifies each record (like an email address in a users table), you might choose to use that as your primary key instead.
@sibertOct 24.2023(updated) — #I think it is a good habit to have a "serial" column for "id" in every table. The serial column takes not much space, but can be handy in some cases. But a unique id (a primary key) is almost a must. I used to have a "letter+id" unique key in the past, but now I often have 2 unique keys in every table. One serial and one more human readable.