Mini-Antipattern: Storing Hash Strings in VARCHAR
“What data type should I use to store a hash?”
The result of a hash is commonly stored as a string of hexadecimal digits, and most developers default to using VARCHAR to store any string. If they don’t know how much space the string requires, they choose VARCHAR(255) because that is greatest length supported by the most implementations of SQL.
Each hash algorithm is different, but they have the property that no matter what the length of the input, all inputs return the same length result for a given hash algorithm. This determines the length of storage you need to store the hash. You don’t need to use a VARCHAR, since the length is fixed. Instead, use a fixed-length CHAR column.
A string ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access