0

I need to maintain 4 digits in a column in MySQL. E.g. 0003, 1234, 0059.

Currently I have it as INT. But other than VARCHAR is it possible to store the numbers as INT and still maintain 4 numbers? So basically if I add 0005 it should store as 0005? How can I do this?

Becky
  • 5,467
  • 9
  • 40
  • 73
  • 2
    What are you trying to achieve? Why is it imperative to _store_ the leading zeros _in the database_? – VolkerK Jul 06 '15 at 03:31
  • @VolkerK thanks. It's a serial code that I'm storing here. I feel it's better if the serial is stored as it should be rather than filling up the zeros by the code? – Becky Jul 06 '15 at 03:39
  • Are you going to do maths with the serial number? No. So don't store it as a number, store it as a string that happens to contain numbers. – DCoder Jul 06 '15 at 04:27
  • @DCoder thanks. It does not need to be maths to store / use INT? INT comparisons are faster than varchar comparisons. So posted this question incase if there are any better ways of doing this using INT. – Becky Jul 06 '15 at 04:49
  • 1
    You're doing premature optimization. If all you're doing 99% of the time is looking up an individual row by its s/n, the performance difference will not be noticeable. Make sure the column is indexed and forget about this. – DCoder Jul 06 '15 at 04:55
  • @DCoder I'm using a foreign key to do that. That's not an issue here. thanks anyway. :) +1 – Becky Jul 06 '15 at 10:04

0 Answers0