I have question about the best solution to store number code
(example: 1234123412341234).
This code have always 16 chars and is numeric value!
I use InnoDB engine to store table
Currently i have type of column VarChar(16) but i have thoughts whether bigint is better for this.
Column code is not edited after added to database.
Bigint requires 8 Byte to store a number, while varchar(16) will require at minimum the double amount of Bytes. Depending on the character set used, even 4 times more. So type of bigint will save space on your disk but also reduce network traffic.
A varchar(16) will not be able to represent all bigint numbers. An unsigned bigint value of 0xFFFFFFFFFFFFFFFFFE can't be stored in a varchar(16) column.
On machine Level, a 16 Byte comparison usually happens in one cycle, while comparing strings is much more expensive, since varchar requires a byte to byte comparison.
You should choose the datatype based on the data you have.
For example a credit card number is a character string (even if it contains numbers), likewise a telephone number is a character string (as string can for example have leading zeros etc).
A number with numeric value should be stored as a number (a distance the to moon for example).
The key question to ask is what operations do you need to do on the data?
BigInt
can be used with operations like addition, subtraction, multiplication, division. Values will be normalised so that 1
, 001
, 1.0
, etc will all be stored the same.VarChar
can be used with operations like sub-string matches. It can store values like 0001
without normalising them.It is very rare that one piece of data needs both those types of operations. For instance, you might want to look for all phone numbers beginning "1800", but would never need to divide a phone number by 3.
So when you say your data is a "number code", you need to think about whether this value is actually a number, or just a string which is all digits.