I am struggling to understand what datatype to use for an MySQL database table.
Let's say we book publishing company and we need to create a database of all our books and authors in MySQL database. We have around 500000 books. A book has an unique ISBN (for example 978-3-16-148410-0
).
So we have two options here to store our books:
- Create a
id VARCHAR(24) NOT NULL
natural primary key column and store our ISBNs there, or - Create a surrogate
id INT NOT NULL AUTO_INCREMENT
but then also aisbn UNIQUE VARCHAR(24)
column
What from I understand, the general consensus is not to use VARCHAR(n)
as a primary keys as it takes more storage and performance doing lookups and joins, and generally that makes sense for me.
However, if all our operations will be towards ISBNs (SELECT * FROM books WHERE isbn = ?
, UPDATE
, DELETE
, etc) - Why not use the VARCHAR(24)
as the primary key?
I am struggling to understand that if you have an immutable natural key (like a book's ISBN) and 95% of all database operations require using that field regardless, shouldn't using a VARCHAR(24)
always outperform a surrogate key design?
I feel like having a surrogate AUTO_INCREMENT INT
key here, is just totally meaningless. It does not give any benefits.
Or am I missing something fundamental here when it comes to determining primary keys.